Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

5. Animating our Game > Time for action – creating stars in the level

Time for action – creating stars in the level

We need to add the layout of the stars in the game and have them moving to add a little effect that they're active. A collision event will need to be applied so they are removed when the panda collides with them.

  1. Create a new function called createStars() and lay out the star objects in a for loop. Add in the "collision" event that will be called by onStarCollision() to remove the stars when they are hit by the panda. Rotate the stars forwards and backwards at 10 seconds and 1080 and -1080 degrees each. This will allow the stars to rotate three full intervals backwards and forwards. Create the walls for the left and right side of the screen.
    local createStars = function()
      
      local numOfRows = 4
      local numOfColumns = 12
      local starPlacement = {x = (display.contentWidth  * 0.5) - (starWidth * numOfColumns ) / 2  + 10, y = 50}
    for row = 0, numOfRows - 1 do
    for column = 0, numOfColumns - 1 do
        
          -- Create a star
          local star = display.newImage("star.png")
          star.name = "star"
          star.isHit = false
          star.x = starPlacement.x + (column * starWidth)
          star.y = starPlacement.y + (row * starHeight)
          physics.addBody(star, "static", {density = 1, friction = 0, bounce = 0})
          stars.insert(stars, star)
          
          star.collision = onStarCollision
          star:addEventListener( "collision", star )
          
          local function starAnimation()
            loc....

  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial