Advertisement
cancelpc

FrameAnimation1 的簡單範例

Sep 8th, 2013
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.09 KB | None | 0 0
  1. local background = display.newImage( "grass.png" )
  2.  
  3. local radius = 40
  4.  
  5. local xdirection = 1
  6. local ydirection = 1
  7.  
  8. local xspeed = 7.5
  9. local yspeed = 6.4
  10.  
  11. local xpos = display.contentWidth * 0.5
  12. local ypos = display.contentHeight * 0.5
  13. local fruit = display.newImage( "fruit.png", xpos, ypos )
  14.  
  15. -- Get current edges of visible screen (accounting for the areas cropped by "zoomEven" scaling mode in config.lua)
  16. local screenTop = display.screenOriginY
  17. local screenBottom = display.viewableContentHeight + display.screenOriginY
  18. local screenLeft = display.screenOriginX
  19. local screenRight = display.viewableContentWidth + display.screenOriginX
  20.  
  21. local function animate(event)
  22.  xpos = xpos + ( xspeed * xdirection );
  23.  ypos = ypos + ( yspeed * ydirection );
  24.  
  25.  if ( xpos > screenRight - radius or xpos < screenLeft + radius ) then
  26.   xdirection = xdirection * -1;
  27.  end
  28.  if ( ypos > screenBottom - radius or ypos < screenTop + radius ) then
  29.   ydirection = ydirection * -1;
  30.  end
  31.  
  32.  fruit:translate( xpos - fruit.x, ypos - fruit.y)
  33. end
  34.  
  35. Runtime:addEventListener( "enterFrame", animate );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement