当前位置:软件学习 > Director >>

Shockwave 预载技术(2)

Shockwave 预载技术
   原著:Lingoworkshop
   翻译:alphachi

   为了让程序拥有更强的适应性,我们还可以添加一个netDone()命令用来检测网络错误。“Preloader”剧本的下一个版本能够检测任何网络错误。一旦发生错误,则使用一个参数返回错误的描述信息;如果没有错误,那么此参数的取值为一个空字符串。

"Preloader" Parent Script (v.3)

property myNetID, myCallBackObj, myCompletionMsg

on new ( me , netAddress, CallBackObj, CompletionMsg)
   myCallBackObj = CallBackObj
   myCompletionMsg = CompletionMsg
   myNetID = preloadNetThing (netAddress)
   aTimerObj = timeout ( me . string ). new ( 100 , # Timer_CheckProgress, me )
end

on Timer_CheckProgress ( me , aTimer)
   finished = netDone (myNetID)
if finished then
     errorNotification = ""
     errorNum = netError (myNetID)
if integerP (errorNum) then
if errorNum then
         errorNotification = me ._GetError(errorNum)
end if
end if
     aTimer. forget ()
call (myCompletionMsg, myCallBackObj, errorNotification)
else
put "Still downloading"
end if
end

on _GetError ( me , errorCode)
case errorCode of
"OK" , "" , 0: return EMPTY
     4: ErrorMsg =( "Bad MOA class. The required Xtras are missing. " )
     5: ErrorMsg =( "The required Xtras are improperly installed or not installed at all." )
     6: ErrorMsg =( "Bad URL or the required Xtras are improperly installed. " )
     20: ErrorMsg =( "Internal error. The browser detected a network or internal error." )
     4146: ErrorMsg =( "Connection could not be established with the remote host." )
     4149: ErrorMsg =( "Data supplied by the server was in an unexpected format." )
     4150: ErrorMsg =( "Unexpected early closing of connection." )
     4154: ErrorMsg =( "Operation could not be completed due to timeout." )
     4155: ErrorMsg =( "Not enough memory available to complete the transaction." )
     4156: ErrorMsg =( "Protocol reply to request indicates an error in the reply." )
     4157: ErrorMsg =( "Transaction failed to be authenticated." )
     4159: ErrorMsg =( "Invalid URL." )
     4164: ErrorMsg =( "Could not create a socket." )
     4165: ErrorMsg =( "Requested object could not be found (URL may be incorrect)." )
     4166: ErrorMsg =( "Generic proxy failure." )
     4167: ErrorMsg =( "Transfer was intentionally interrupted by client." )
     4242: ErrorMsg =( "Download stopped by netAbort(url)." )
     4836: ErrorMsg =( "Download stopped for an unknown reason, possibly a network error, or the download was abandoned." )
otherwise
       ErrorMsg =( "Unknown error code" )
end case
return ErrorMsg
end


   要想看到具体返回了什么参数,可以像下面这样修改“PreloaderInte易做图ce”行为:

on beginSprite me
clearcache ()
   urlToLoad = "http://www.lingoworkshop.com/Tutorials/Preloader/Main.dcr"
script ( "Preloader" ). new (urlToLoad, me , # mHandlePreloadCompletion)
end

on mHandlePreloadCompletion ( me , errorMsg)
if errorMsg <> EMPTY then alert "Network Error!" & return & errorMsg
else alert "All Done"
end

   现在,”Preloader”剧本已经有能力预载一个URL并在预载完成时对另一个对象进行返回调用。最后一步则是让“Preloader”剧本可以报告其运行状态,以便我们制作一个进度条来给用户提供一些反馈信息。为了达到这一目的,我们可以使用getStreamStatus(myNetID)函数获取网络操作的当前状态。这个函数会返回一个属性列表,其中包含像#state(可能是“connecting”或“in progress”)、字节总数和当前已传递字节数这样的信息。在“Preloader”剧本的最终版本中,这些信息被用来确定URL已被下载的部分。此版本附加了一个起始参数“StatusMsg”——返回调用目标用于显示当前网络状况的程序名称。

"Preloader" Parent Script (v.4)

[1] [2] 下一页  

[page_break]


property myNetID, myCallBackObj, myCompletionMsg, myStatusMessage

on new ( me , netAddress, CallBackObj, CompletionMsg, StatusMsg)
   myCallBackObj = CallBackObj
   myCompletionMsg = CompletionMsg
   myStatusMessage = Statusmsg
   myNetID = preloadNetThing (netAddress)
   aTimerObj = timeout ( me . string ). new ( 100 , # Timer_CheckProgress, me )
end

on Timer_CheckProgress ( me , aTimer)
   finished = netDone (myNetID)
if finished then
     errorNotification = ""
     errorNum = netError (myNetID)
if integerP (errorNum) then
if errorNum then
         errorNotification = me ._GetError(errorNum)
end if
end if
     aTimer. forget ()
call (myCompletionMsg, myCallBackObj, errorNotification)
else
-- 仍在预载;检测是否需要报告当前状态
if myStatusMessage. ilk = #Symbol then
-- 已经得到一个返回调用消息;获取状态列表并将其发送给返回调用目标
       theStatus = getStreamStatus (myNetID)
       currentState = theStatus. state
-- 在发送状态列表前计算出已下载部分(变量“fractionDone”)
case (currentState) of
"InProgress" :
if theStatus.bytesSoFar > 0 then
if theStatus.bytesTotal > 0 then fractionDone = MIN ( 1 . 0 , float (theStatus.bytesSoFar) / theStatus.bytesTotal )
else fractionDone = 50
else
             fractionDone = 0
end if
"Complete" :
           fractionDone = 100
otherwise
           fractionDone = 0
end case
-- 将fractionDone作为一个属性添加到状态列表中
       theStatus. addProp ( # fractiondone, fractionDone )
-- 通知当前状态的返回调用对象和已传递的百分比
call (myStatusMessage, [myCallbackObj], theStatus)
end if

end if
end

on _GetError ( me , errorCode)
case errorCode of
"OK" , "" , 0: return EMPTY
     4: ErrorMsg =( "Bad MOA class. The required Xtras are missing. " )
     5: ErrorMsg =( "The required Xtras are improperly installed or not installed at all." )
     6: ErrorMsg =( "Bad URL or the required Xtras are improperly installed. " )
     20: ErrorMsg =( "Internal error. The browser detected a network or internal error." )
     4146: ErrorMsg =( "Connection could not be established with the remote host." )
     4149: ErrorMsg =( "Data supplied by the server was in an unexpected format." )
     4150: ErrorMsg =( "Unexpected early closing of connection." )
     4154: ErrorMsg =( "Operation could not be completed due to timeout." )
     4155: ErrorMsg =( "Not enough memory available to complete the transaction." )
     4156: ErrorMsg =( "Protocol reply to request indicates an error in the reply." )
     4157: ErrorMsg =( "Transaction failed to be authenticated." )
     4159: ErrorMsg =( "Invalid URL." )
     4164: ErrorMsg =( "Could not create a socket." )
     4165: ErrorMsg =( "Requested object could not be found (URL may be incorrect)." )
     4166: ErrorMsg =( "Generic proxy failure." )
     4167: ErrorMsg =( "Transfer was intentionally interrupted by client." )
     4242: ErrorMsg =( "Download stopped by netAbort(url)." )
     4836: ErrorMsg =( "Download stopped for an unknown reason, possibly a network error, or the download was
abandoned.")
otherwise
       ErrorMsg =( "Unknown error code" )
end case
return ErrorMsg
end

上一页  [1] [2] 

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,