Need help with this code, please.

I got the following code from a different web site -

function spawnEnemy()     local enemypics = {"beetleship.png","octopus.png", "rocketship.png"}     local enemy = display.newImage(enemypics[math.random (#enemypics)])     enemy:addEventListener ( "tap", shipSmash )          if math.random(2) == 1 then         enemy.x = math.random ( -100, -10 )     else         enemy.x = math.random ( display.contentWidth + 10, display.contentWidth + 100 )         enemy.xScale = -1     end     enemy.y = math.random (display.contentHeight)     enemy.trans = transition.to ( enemy, { x=centerX, y=centerY, time=math.random(2500-speedBump, 4500-speedBump), onComplete=hitPlanet } )     speedBump = speedBump + 100 end  

enemypics is a set of images that randomly spawn and move/fly towards the center of the screen.

My question is how do I change from still images to sprite sheets? i would like to use the same code as close to its original form as possible. This is learning/study code for me so I am experimenting.

I appreciate any help. Thanks.

 

Hi @jgcthatsme,

Do you need help setting up the sprite sheets? If so, a walkthrough can be found here:

http://www.coronalabs.com/blog/2012/10/02/animated-sprites-and-methods/

http://www.coronalabs.com/blog/2013/05/14/sprites-and-image-sheets-for-beginners/

In terms of using them in this scenario, will each “variety” be a static, non-animated sprite frame? Or do you want them to each animate, as in “octopus” would have 10 animation frames, “beetleship” would have 8 frames, etc.? This dictates whether you should set up a random selection of the sequences, or just a random selection of one frame from one sequence.

Regards,

Brent

Hello Brent,

I already have the sprites up and working in a different file (for testing reasons). Each sprite has 3 to 5 frames and needs to animate as it comes onto the screen.

Basically I want instead of still images flying on the screen i want my sprites that are animated…to fly out on the screen and be clickable just like the still images. Hope I make sense and thanks for any help! :slight_smile:

Hi @jgcthatsme,

In this case, you’ll need to create sprite sequences for each item you want animated. Then, you can figure out how to select the sequence you need (either pick one at random, or do it some other way). Sprite sequences and all the rest is discussed in the links I provided earlier in this thread.

Best regards,

Brent

Hi @jgcthatsme,

Do you need help setting up the sprite sheets? If so, a walkthrough can be found here:

http://www.coronalabs.com/blog/2012/10/02/animated-sprites-and-methods/

http://www.coronalabs.com/blog/2013/05/14/sprites-and-image-sheets-for-beginners/

In terms of using them in this scenario, will each “variety” be a static, non-animated sprite frame? Or do you want them to each animate, as in “octopus” would have 10 animation frames, “beetleship” would have 8 frames, etc.? This dictates whether you should set up a random selection of the sequences, or just a random selection of one frame from one sequence.

Regards,

Brent

Hello Brent,

I already have the sprites up and working in a different file (for testing reasons). Each sprite has 3 to 5 frames and needs to animate as it comes onto the screen.

Basically I want instead of still images flying on the screen i want my sprites that are animated…to fly out on the screen and be clickable just like the still images. Hope I make sense and thanks for any help! :slight_smile:

Hi @jgcthatsme,

In this case, you’ll need to create sprite sequences for each item you want animated. Then, you can figure out how to select the sequence you need (either pick one at random, or do it some other way). Sprite sequences and all the rest is discussed in the links I provided earlier in this thread.

Best regards,

Brent