close

Basic Motion and Transitions
基本運動和轉換


One of the most powerful things about the Corona SDK is that any display object can be animated. This is a testament to the flexible graphics model that Corona offers.
其中最強大的東西有關Corona SDK的是,任何顯示對象可以是動畫。這是一個證明了靈活的圖形模型,Corona 優惠。
Animations allow you to create visually-rich and engaging user experiences. Animations are accomplished by generating a sequence of frames that evolve smoothly from frame to frame. The term tween (short for inbetween) is a term describing the process in which such intermediate frames are generated. It is often used as shorthand to indicate that a property of an object will change during the animation, as in tweening the position.
動畫讓你創建視覺效果豐富,引人入勝的用戶體驗。動畫過程是通過產生一個序列平滑演進的幀幀與幀。這個詞之間(簡稱插圖中)是一個術語描述的過程中產生這些中間幀。它經常被用來作為縮寫,表示一個對象的屬性將改變在動畫,如在補間的立場。


Transitions 轉換


The transition library allows you to easily create animations with only a single line of code by allowing you to tween one or more properties of a display object. For example, you can fadeout a display object by tweening its alpha property (the alpha property transitions from 1.0 to 0).
轉換庫允許您輕鬆地創建動畫,只有一行的代碼,讓您一個或多個屬性之間的顯示對象。例如,您可以淡出顯示其α補間對象屬性(從 1.0 alpha屬性轉換為 0)。


The simplest way to do this is to use the transition.to method which takes a display object as its first argument and a table containing the control parameters as its second. The control parameters specify the duration of the animation, an optional delay for when to start the animation, and the final values of properties for the display object. The intermediate values for a property are determined by an easing function that is also specified as a control parameter. By default this is a linear function.
最簡單的方式做,這是使用方法,需要一個 transition.to顯示對象作為它的第一個參數,一個包含了控制參數作為其第二個。控制參數指定的期限動畫,可選延遲何時開始動畫,最終值屬性的顯示對象。中間值的屬性是由一個緩和功能,也被指定為控制參數。默認情況下這是一個線性函數。


Below are some examples of how to animate a square (see Transition2 in the sample code):
下面是一些例子如何動畫一個正方形(見 Transition2在示例代碼):


 

local square = display.newRect( 0, 0, 100, 100 )
square:setFillColor( 255,255,255 )
 
local w,h = display.contentWidth, display.contentHeight
 
local square = display.newRect( 0, 0, 100, 100 )
square:setFillColor( 255,255,255 )
 
local w,h = display.contentWidth, display.contentHeight
 
-- (1) move square to bottom right corner; subtract half side-length
--     b/c the local origin is at the square's center; fade out square
transition.to( square, { time=1500, alpha=0, x=(w-50), y=(h-50) } )
 
-- (2) fade square back in after 2.5 seconds
transition.to( square, { time=500, delay=2500, alpha=1.0 } )

 

In the first tween, notice that multiple values are changing: position and alpha. That’s because in the control parameters we specified the final values for the x, y, and alpha properties. 在第一個補間,發現多個值正在改變:位置和alpha。這是因為在我們的控制參數中指定的最終值的X,Y和alpha屬性。


For each property specified in the control parameters, the library looks at the current property value and gradually changes that property to the final value over the time period specified (1.5 seconds in this case). In the last tween, we use the delay control parameter to start the tween after the initial tween’s fadeout is complete.
對於每個屬性中指定的控制參數,library著眼於當前的屬性值,並逐步改變,到最後的屬性值超過指定的時間段(在這個例子是1.5秒)。在過去的補間,我們使用延遲控制參數,補間開始,最初之間的淡出已完成。


Note that the transition library operates in a time-based manner.
請注意,轉換庫工作在基於時間的方式。


transition.to( target, params )


Returns a tween that animates properties in the display object target over time. The final property values are specified in the params table. To customize the tween, you can optionally specify the following non-animating properties in params:
返回一個補間動畫的顯示對象中的屬性目標隨著時間的推移。最後一個屬性值中指定的參數表。要自補間,您可以選擇指定的下列非動畫特性參數:


*params.time specifies the duration of the transition in milliseconds. By default, the duration is 500 ms (0.5 seconds).
*params.transition is by default easing.linear . See Easing for more functions.
*params.delay specifies the delay (none by default) before the tween begins.
*params.delta is a boolean specifying whether non-control parameters are interpreted as final ending values or as changes in value. The default is nil meaning false.
*params.onStart is a function or table listener called before the tween begins. Table listeners must have an onStart method. When invoked, the listener is given target instead of an event.
*params.onComplete is a function or table listener called after the tween completes.


* params.time指定的時間以毫秒為單位的過渡。默認情況下,持續時間為 500毫秒(0.5秒)。
* params.transition默認 easing.linear。見放寬更多的功能。
* params.delay指定的延遲(無默認)補間開始之前。
* params.delta是一個布爾值,指定是否非控制參數被解釋為最終結束值或值的變化。默認值是零的意思假的。
* params.onStart是一個函數之前調用偵聽器或表補間開始。表聽眾必須有一個 OnStart方法。調用時,聽者是給定的目標,而不是一個事件。
* params.onComplete是一個函數或表後調用偵聽器之間完成。

例子:--------------------------------------------------------------

local liste = function(obj)
  print("liste:"..tostring(obj))
end
 
-- (1) move square to bottom right corner; subtract half side-length  -- 移動正方形右下角;減去一半邊長
--     b/c the local origin is at the square's center; fade out square --b/c 的地方起源是在方形的中心,方形淡出
transition.to( square, { time=2000, alpha=0, x=(w-50), y=(h-50),onStart=listener1 } )
--------------------------------------------------------------

transition.from( target, params )
transition.from(目標,參數)


Similar to transition.to except the starting property values are specified in the params table and the final values are the corresponding property values in target prior to the call.
除了類似 transition.to開始屬性值中指定的參數表和最終值是對應的屬性值在目標之前調用。


The transition library eliminates the need for manually animating objects. Compare the following code that causes a white rectangle to fade out in 1 second:
轉換庫不再需要手工動畫的對象。比較下面的代碼,導致一個白色矩形淡出在1秒鐘:

 

Manual animation Using transition
local rect = display.newRect(
  0, 0, 100, 100 )
rect:setFillColor(255,255,255)
 
local tMax =1000+system.getTimer()
local function fadeOut(event)
  local t = event.time
  local rect = event.target
  if t < tMax then
    rect.alpha = 1 - t/tMax
  else
    rect.alpha = 0
    -- done, so remove listener
    Runtime:removeEventListener(
         "enterFrame", fadeOut )
  end
end
 
-- Add listener to begin animation
Runtime:addEventListener(
   "enterFrame", fadeOut )
local rect = display.newRect(
  0, 0, 100, 100 )
rect:setFillColor(255,255,255)
 
transition.to(
  rect, {time=1000, alpha=0})
transition.cancel( tween )
Cancels the tween.
transition.dissolve( src, dst, duration, delayDuration )

Easing 緩和

The easing library is a collection of interpolation functions used by the transition library:
緩動庫是一家集插值函數所使用的轉變庫:


easing.linear( t, tMax, start, delta )


easing.inQuad( t, tMax, start, delta )


easing.outQuad( t, tMax, start, delta )


easing.inOutQuad( t, tMax, start, delta )


easing.inExpo( t, tMax, start, delta )


easing.outExpo( t, tMax, start, delta )


easing.inOutExpo( t, tMax, start, delta )


You can create your own easing function to interpolate between a start and a final value. The arguments of the function are defined as:
您可以創建自己的緩動函數插值之間的開始和最終值。該函數的參數定義為:
t:is the time since the transition started
tMax:is the duration of the transition
start:is the starting value
delta:is the change in value (final value = start + delta)


t:是時間,因為轉變開始
tMax:是的過渡期
start:是初始值
delta:是變化值 (final value = start + delta)




Movieclips影片剪輯


The external movieclip library allows you to create animated sprites (sometimes called “movieclips”) from sequences of images, which can then be moved around the screen using the same techniques as any other Corona display object. 外部影片剪輯庫允許你創建動畫精靈(有時稱為“影片剪輯“)從序列圖像,然後可以在屏幕上移動使用相同的技巧,任何其他Corona 顯示對象。


Functions are available to play these animation frames, or partial sequences of these frames, in either the forward or reverse direction; to jump to specified frames; to skip to the next or previous frame; to automatically delete the animation on completion of a sequence; and to make the animation draggable, complete with press, drag, and release events.
函數可用來播放這些動畫幀,或者部分序列,這些幀,無論是正向或反向的方向;跳轉到指定的框架;跳到下一個或前一幀;自動刪除上完成的動畫序列;並要動畫拖動,按著,拖動和釋放事件來完成。


This framework provides an quick and lightweight way to create animations, and also helps when porting existing Flash content to Corona.
這個框架提供了一個快速,輕量級的方法來創建動畫,也有助於在現有的Flash內容移植到Corona。


The movieclip library is an external module, movieclip.lua, that can be included with your projects and loaded using the require command (see Loading external libraries for further details on require).
該影片剪輯庫是一個外部模塊,movieclip.lua,可以包含在您的項目和使用需要加載命令(請參閱加載外部庫的進一步細節要求)。


Note that while this library is based on the older sprite library, the library name has been changed to "movieclip", because the name "sprite" is now reserved for the sprite-sheet feature in Corona Game Edition. A discussion of the differences is below.
請注意,雖然這個庫是基於舊的精靈庫,庫的名稱已更改為“影片剪輯“,因為這個名字“雪碧“現在是保留給精靈頁功能在Corona 遊戲版。一個討論的差異如下。


For a sample project using the movieclip library, see the Graphics/Movieclip project in the Sample Code directory of the Corona SDK, or in the Sample Code section of the Ansca Mobile website.
有關示例項目使用影片剪輯庫,請參閱圖形/影片剪輯的項目在示例代碼目錄中的Corona  SDK或在示例代碼部分的Ansca手機網站。


Movieclip functions影片剪輯功能


movieclip.newAnim( frames )
movieclip.newAnim(幀)


Creates an animated sprite using an array of image filenames provided in the frames table:
創建一個動畫精靈使用陣列的圖像文件名中提供的框架表:


myAnim = movieclip.newAnim{ "img1.png", "img2.png", "img3.png", "img4.png" }


object:play()


Starts the animated sprite playing in the forward direction. When the end of the image set is reached, it will cycle back to the first image and continue playing. If a specific frame sequence has already been constructed, calling play() with no parameters will simply resume playing within that sequence.
開始播放的動畫精靈在前進的方向。當最終的圖像設置是達成共識,這將循環回第一張圖片並繼續播放。如果一個特定的幀序列已經建成,調用play()不帶參數的簡單恢復播放將在該序列。


object:play{ startFrame=a, endFrame=b, loop=c, remove=shouldRemove }
Starts the animated sprite playing in the forward direction. When the frame of number given by endFrame is reached, it will cycle back to the frame number given by startFrame and continue playing.
開始播放的動畫精靈在前進的方向。當幀的數量達到給予 endFrame,這將循環回幀號給出 startFrame並繼續播放。


myAnim:play{ startFrame=1, endFrame=6, loop=3, remove=true }


The loop parameter accepts a number of times to loop the sequence, with zero (the default) indicating that it should loop forever.
循環參數接受一個次數為循環的順序,用0(默認),這表明它應該循環下去。


The remove parameter is a boolean flag, and if set to true, the movieclip will automatically delete itself when the given sequence is complete. This is useful for things like animated explosions and other single-use cases. Note that the object will only be garbage-collected if there are no other remaining references to it in your Lua code. The default value is false.
在刪除參數是一個布爾標誌,如果設置為 true,將自動刪除的MovieClip本身當給定的順序完成。這是非常有用的事情,比如動畫爆炸和其他單用例。請注意,該對象將只被垃圾收集的,如果沒有其他剩餘的引用,在你的Lua代碼。默認值是false。


All parameters are optional, but startFrame and endFrame will default to the first and last images provided in newAnim(), so it is a good practice to provide both values explicitly to avoid unexpected behavior.
所有參數都是可選的,但startFrame和endFrame將默認為第一個和最後一個圖像提供newAnim(),因此它是一個很好的做法,以提供明確的價值觀,以避免意外的行為。


Passing any parameters to play() indicates that a new sequence should be constructed, and any current sequence information will be reset. Call play() with no parameters if you simply want to resume the current sequence.
傳遞任何參數,以play()表示,一個新的序列應當建立,任何當前的序列信息將被復位。調用 play()不帶參數,如果你只是想恢復當前序列。




object:reverse()


Starts the animated sprite playing in the reverse direction. When the beginning of the image set is reached, it will cycle back to the last image and continue playing backwards. If a specific frame sequence has already been constructed, calling reverse() with no parameters will simply resume playing backwards within that sequence.
啟動動畫精靈播放方向相反。當開始時達到的圖像設置,它會循環回到最後一個形象,繼續發揮倒退。如果一個特定的幀序列已經建成,調用reverse()不帶參數的簡單恢復播放向後將在該序列。


object:reverse{ startFrame=a, endFrame=b, loop=c, remove=shouldRemove }


Starts the animated sprite playing in the reverse direction. When the frame of number given by endFrame is reached, it will cycle back to the frame number given by startFrame and continue playing.
啟動動畫精靈播放方向相反。當幀的數量給予 endFrame是到達,這將循環回幀號賦予 startFrame並繼續播放。


The loop parameter accepts a number of times to loop the sequence, with zero (the default) indicating that it should loop forever.
循環參數接受一個次數為循環的順序,用0(默認),這表明它應該循環下去。


The remove parameter is a boolean flag, and if set to true, the movieclip will automatically delete itself when the given sequence is complete. This is useful for things like animated explosions and other single-use cases. Note that the object will only be garbage-collected if there are no other remaining references to it in your Lua code. The default value is false.
在刪除參數是一個布爾標誌,如果設置為 true,將自動刪除的MovieClip本身當給定的順序完成。這是非常有用的事情,比如動畫爆炸和其他單用例。請注意,該對象將只被廢物收集的,如果沒有其他剩餘的引用,在你的Lua代碼。默認值是false。


All parameters are optional, but startFrame and endFrame will default to the last and first images provided in newAnim(), so it is a good practice to always provide both values explicitly to avoid unexpected behavior.
所有參數都是可選的,但startFrame和endFrame將默認為最後和第一組圖片提供newAnim(),因此這是一個很好的習慣做法,同時提供明確的價值觀,以避免意外的行為。


Passing any parameters to reverse() indicates that a new sequence should be constructed, and any current sequence information will be reset. Call reverse() with no parameters if you simply want to resume the current sequence.
傳遞任何參數,以reverse()表示,一個新的序列應當建立,任何當前的序列信息將被復位。呼叫reverse()不帶參數,如果你只是想恢復當前序列。


object:nextFrame()
Resets any animation sequence in progress, moves the animation to the next image in the total sequence, and stops. This can be used with a two-frame movieclip to make an image toggle between two states
重置序列進行過程中任何動畫,移動到下一個動畫圖像在全序列,並停止。這可以使用兩幀圖像的MovieClip,使兩種狀態之間切換


object:previousFrame()
Resets any animation sequence in progress, moves the animation to the previous image in the total sequence, and stops.
重置序列進行過程中任何動畫,移動上一個動畫圖像在全序列,並停止。


object:stop()
Stops the animation of the sprite at its current frame.
停止動畫的精靈在其當前幀。


object:stopAtFrame( frame )


Jumps the animation to the specified frame, given either as a frame number or by an optional frame label (see setLabels function below).
跳轉到指定的動畫畫面,因為無論是作為一幀編號或幀標籤可選(見 setLabels功能如下)。


Note: to “go to and play” from a specified frame, simply issue a stopAtFrame command followed by a play or reverse command. Both commands will execute before the display updates again, and so the transition will be immediate:
注意:“go to and play“從指定幀,只需發出一個 stopAtFrame命令後面加上一個play或reverse命令。這兩個命令將執行顯示前再次更新,所以將是立即變換:
-------------------------------------
myAnim:stopAtFrame(4)
myAnim:play()
 
myAnim:stopAtFrame("label")
myAnim:reverse()

-------------------------------------

object:setLabels( labels )

Adds optional labels to an Anim object previously created, using a table to assign label names to selected frame numbers:
添加可選的標籤,一Anim物件以前創建,使用一個表來分配到選定的幀標籤名稱號碼:

myAnim:setLabels{ firstLabel=1, anotherLabel=3 }

object:setDrag


Turns any movieclip into a draggable object when drag is set as true. The limitX and limitY parameters limit the dragging to either the x or y axis, and the bounds parameter can be used to specify drag boundaries for the object as {left, top, width, height}.
將任何影片剪輯成一個可拖動對象時,拖動設置為真。該 limitX和limitY參數限制拖動到X或Y軸,以及邊界參數可用於指定拖動邊界的對象為 {左,上,寬度,高度}。


The onPress, onDrag and onRelease parameters take the names of functions to be called when those events occur. All parameters are optional.
該 onPress,onDrag和onRelease參數的函數的名稱參加時要調用這些事件的發生。所有參數都是可選的。

myAnim:setDrag{ drag=true, limitX=false, limitY=false, onPress=myPressFunction, onDrag=myDragFunction, 
                onRelease=myReleaseFunction, bounds={ 10, 10, 200, 50 }}


To turn off the draggable property again, set "drag" to false:

要關閉拖動屬性再次,將“拖“為 false:

myAnim:setDrag{ drag=false }


Note 
that the examples above with curly brackets are using the Lua “shorthand” notation for passing a table of named values directly to a function. In other words, the function is actually receiving a single value that happens to be a Lua table, like this: function( {values } ). The shorthand notation allows the outer parentheses to be eliminated, to make the code more readable.
注意:
上面的例子是使用大括號中的Lua“速記“符號傳遞表值直接命名的一個函數。換句話說,功能是實際接收的單個值恰好是一個 Lua表所示:function({值})。速記符號允許外括號被淘汰,​​使代碼更具可讀性。


Differences between movieclips and sprite sheets影片剪輯之間的差異和精靈表

Corona Game Edition includes a “sprite sheet” feature for constructing animated sprites by copying rectangular areas from a single large texture in memory.
Corona遊戲版包括一個“精靈表“功能為建設動畫精靈矩形區域複製從一個單一的大紋理在內存中。

Sprite sheets are a much more efficient use of texture memory, since OpenGL-ES allocates all textures in dimensions equal to the next power-of-two, no matter what the size of the source image is. For example, a single image with dimensions of 80x300 will require a texture memory allocation of 128x512 pixels.
精靈的表是一個更有效的利用紋理內存,因為的OpenGL- ES的分配所有的紋理尺寸等於在未來冪二個,不管是什麼尺寸的源圖像。例如,一個單一的圖像尺寸為80x300,需要一個紋理內存分配的128x512像素。

Therefore, sprite sheets are recommended for complex character animation, or any case in which there are a large number of animation states.
因此精靈片被推薦為複雜的角色動畫,或任何案件中,有大量的動畫狀態。

However, sprite sheets will require more coding and are more complex to set up; for example, you must first construct a large sheet of animation frames. The movieclip library is easier to get started with, and can be more rapidly used to port Flash content, since movieclip frames can be exported from Flash as PNG sequences.
然而精靈表將需要更多的編碼和更複雜的設置,例如,你必須首先構造一個大張的動畫幀。該影片剪輯庫是比較容易上手,並且可以更迅速地用於港口Flash內容,因為 MovieClip的框架可以從 Flash導出為 PNG序列。

For more information about sprite sheets, see Game Edition: Sprite Sheets.
如需有關精靈介紹,看到遊戲版:精靈表。

Custom/Programmatic Animations自定義 /編程動畫

Often you will need to create your own custom animations that are not feasible using the transition library. This is known as programmatic animation because you have to write custom code to produce the animation sequence.
通常你需要創建自己的自定義動畫是不可行的使用過渡庫。這就是所謂的綱領性動畫,因為你必須編寫自定義代碼來生成動畫序列。

To create such animations, you need to change the contents of the screen over time. In some environments, it’s natural to do this by changing properties of an object in loops such as a for or while loop. However, in Corona, you cannot produce animations using such loops because the screen is never updated within a block of code (see Screen Updates).
要創建這樣的動畫,你需要改變的內容在屏幕上隨著時間的推移。在某些環境中,自然要做到這一點,通過改變一個對象的屬性,例如在循環中for或while循環。然而,在Corona,你不能使用這些循環產生的動畫畫面,因為從來沒有一個塊內更新的代碼(見屏幕更新)。

Instead, animations are produced by repeatedly calling listeners. These listeners modify the display objects on the screen and then exit, thus allowing the screen to be updated. Such listeners are known as "enterFrame" listeners because you register these listeners with the "enterFrame" event.
相反,動畫是由重複調用的聽眾。這些聽眾修改顯示在屏幕上的物體,然後退出,從而使屏幕更新。這樣的偵聽被稱為“enterFrame“聽,因為你註冊這些聽眾與“enterFrame“事件。

In the drawing cycle, "enterFrame" events are dispatched before the screen is updated providing your code the opportunity to modify the contents of the screen (see Drawing Cycle). With "enterFrame" events, you can produce all kinds of animations. In fact, the transition library is built on top of these events.
在繪圖週期,“enterFrame“事件分派屏幕前更新您的代碼提供了機會,修改的內容在屏幕上(如圖週期)。隨著“enterFrame“事件,你就可以生產所有種類的動畫。事實上,過渡庫是建立在這些事件上。


Below is an example of how to animate a bouncing ball:
下面是一個例子,如何進行動畫一個彈跳球:

local xdirection,ydirection = 1,1
local xpos,ypos = display.contentWidth*0.5,display.contentHeight*0.5
local circle = display.newCircle( xpos, ypos, 20 );
circle:setFillColor(255,0,0,255);
 
local function animate(event)
        xpos = xpos + ( 2.8 * xdirection );
        ypos = ypos + ( 2.2 * ydirection );
 
        if (xpos > display.contentWidth - 20 or xpos < 20) then
                xdirection = xdirection * -1;
        end
        if (ypos > display.contentHeight - 20 or ypos < 20) then
                ydirection = ydirection * -1;
        end
 
        circle:translate( xpos - circle.x, ypos - circle.y)
end
 
Runtime:addEventListener( "enterFrame", animate );

 

The listener function animate is called every time an "enterFrame" event occurs. It is responsible for changing the position of the ball and for ensuring that the ball “bounces” when it hits the edge of the screen.
動畫的偵聽器函數調用每一次的“enterFrame“事件發生。它負責改變球的位置,並確保球“彈“,當它撞擊屏幕邊緣。

Because "enterFrame" events occur at the global level, you register listeners for those events with the global Runtime object.
因為“enterFrame“事件發生在全球範圍內,您註冊聽眾為這些事件與全球運行的對象。

Frame Rate 幀頻
The "enterFrame" event occurs at a regular interval known as the frame rate, so your listeners will be called at the frame rate. However, if your listeners take too long to exit, then the actual frame rate will be less than the desired frame rate.
在“enterFrame“事件發生在定期間隔稱為幀的速度,使你的聽眾將被調用的幀速率。不過,如果你的聽眾需要很長時間才能退出,那麼實際的幀頻將低於預期的幀速率。


Time-based vs Frame-based基於時間比基於幀

In the above example, the animation was done in a frame-based manner. If the actual frame rate were to slow down, the ball would appear to move more slowly as each and every intermediate frame got rendered; no intermediate frames would be skipped. If you were trying to synchronize the animation with sound, then this behavior would be extremely problematic.
在上面的例子,做動畫的一幀為基礎的方式。如果實際的幀速率是慢了下來,球會出現移動更慢每個中間幀渲染了,沒有中間幀將被跳過。如果你想同步動畫與聲音,那麼這種行為將是極為困難。


The solution is time-based animation. We can transform the above example to be time-based by calculating how much time had passed between calls to our listener and changing the velocities appropriately. This would result in the following changes:
該解決方案是基於時間的動畫。我們一定可以將上面的例子是基於時間的計算時間已經過去了多少調用之間改變我們的聽眾和適當的速度。這將導致以下變化:

local xdirection,ydirection = 1,1
local xpos,ypos = display.contentWidth*0.5,display.contentHeight*0.5
local circle = display.newCircle( xpos, ypos, 20 );
circle:setFillColor(255,0,0,255);
 
local tPrevious = system.getTimer()
local function animate(event)
        local tDelta = event.time - tPrevious
        tPrevious = event.time
        xpos = xpos + ( 0.084*xdirection*tDelta );
        ypos = ypos + ( 0.066*ydirection*tDelta );
 
        if (xpos > display.contentWidth - 20 or xpos < 20) then
                xdirection = xdirection * -1;
        end
        if (ypos > display.contentHeight - 20 or ypos < 20) then
                ydirection = ydirection * -1;
        end
 
        circle:translate( xpos - circle.x, ypos - circle.y)
end
 
Runtime:addEventListener( "enterFrame", animate );

 

Notice 注意
how we leverage the fact that the "enterFrame" event contains a property storing the time in milliseconds. We compare that with the previous time to determine how far the ball should travel. In addition, our old x,y velocities (2.8, 2.2) implicitly assumed that time was measured in frames. The equivalent time in milliseconds is simply the frame rate. By default, that’s set to 30 fps or 33.3 milliseconds. So we can multiply the old velocities by (30/1000) to get the new time-based velocities.
我們如何利用一個事實,即“enterFrame“事件包含一個屬性存儲的時間以毫秒為單位。我們比較,與以前的時間來確定多遠球應該旅行。此外,我們的老的x,y速度(2.8,2.2)含蓄地假定時間是衡量框架。等效時間以毫秒為單位僅僅是幀速率。默認情況下,這設置為 30 fps或333毫秒。因此,我們可以多舊的速度由(一千分之三十),以獲得新的基於時間的速度。


Lost or Missing Time滅失或者失踪的時間

The one problem to watch out for when doing time-based animation is that when the device suspends the app, you need to account for the “lost” time. In the simulator, you can simulate a suspend by using the keyboard shortcut ⌘↓ (command-down arrow) which corresponds to the Suspend/Resume menu item under Hardware. (Note a known issue is that clicking on the menu causes the app to pause as if it were suspended but doesn’t generate a suspend event).
一個需要注意的問題時,做基於時間的動畫是當設備暫停後,你就需要考慮到“丟失“的時間。在模擬器,可以模擬一個暫停使用鍵盤快捷鍵⌘↓(命令向下箭頭),對應於暫停/恢復菜單項下的硬件。 (注一已知的問題是,點擊菜單上的原因暫停的應用程序,就好像它被停職,但並不會產生暫停事件)。


In the bouncing ball animation, if the app is suspended for half a second, the ball may appear to jump across the screen. In the example above, the solution is to adjust tPrevious to account for this missing time:
在彈跳球動畫,如果應用程序是暫停半秒鐘,球可能會出現在屏幕上跳躍。在上面的例子,解決的辦法是調整 tPrevious考慮到這一點失踪時間:

-- Add the following below the code in the previous example
 -- 下面添加以下代碼在前面的例子
  local tSuspend local function onSuspendResume( event )         if "applicationSuspend" == event.type then                 tSuspend = system.getTimer()         elseif "applicationResume" == event.type then                 -- add missing time to tPrevious 添加缺少時間 tPrevious                 tPrevious = tPrevious + ( system.getTimer() - tSuspend )         end end   Runtime:addEventListener( "system", onSuspendResume );

 

arrow
arrow
    全站熱搜

    createps 發表在 痞客邦 留言(0) 人氣()