random sprite spawning

Ok,

I can spawn images easily and move them about using transitions etc but I seem to have hit a small problem
I need to spawn animated sprites in the same manner.
My problem is I would like to use the same method as below which I have been using for other objects.

math.randomseed( os.time() )  
  
local images = {}  
   
images[#images + 1] = "leafs.png"  
images[#images + 1] = "leafs1.png"  
images[#images + 1] = "leafs2.png"  
images[#images + 1] = "leafs3.png"  
   
local function leafSpawn()  
   
 local index = math.random( 1, #images )  
   
 local leaf = display.newImage( images[index] )  
  
 leaf.myName = "leaf"  
 leaf:addEventListener("collision", leaf)  
 leaf.x = -100  
 leaf.y = math.random(20,250)  
 transition.from(leaf, {time = math.random(2400,4500), delay = 0, x = leaf.x +580, onComplete=function() end})  
  

At the moment I have this, it will spawn a single random dandelion each time, so I guess I need to add a timer function to it in order to spawn a certain number of dandelions like the leafSpawn functions i have above.
Any help would be appreciated .

module(..., package.seeall)  
local physics = require "physics"  
physics.start()  
physics.setGravity(0,0)  
--physics.setDrawMode( "hybrid" )  
  
  
math.randomseed( os.time() )  
  
   
function new()  
  
local sheet = graphics.newImageSheet( "dandelion.png", { width=44.19, height=45.34, numFrames=99 } )  
local dandelion = display.newSprite( sheet, { name="dandelion", start=1, count=99, time=250 } )  
local w,h = display.viewableContentWidth, display.viewableContentWidth  
dandelion:play()   
   
   
 dandelion.myName = "dandelion"  
 dandelion:addEventListener("collision", dandelion)  
 dandelion.x = -100  
 dandelion.y = math.random(20,250)  
 transition.from(dandelion, {time = math.random(2400,4500), delay = 0, x = dandelion.x +580, onComplete=function() end})  
  
 end  
  

I have been making all my image objects into modules and then importing to main.lua.
[import]uid: 127675 topic_id: 30559 reply_id: 330559[/import]

Anyone?

ok, if I have explained it badly, I would like to in a hypothetical world just add my sprite dandelion etc to the table.
i.e. where we have “leafs.png” leafs1.png" etc I would like to be able to add “sprite”(my dandelion sprite) “sprite1” etc

what would the process be to do this please?
just a pointer will do guys, I am not asking for code. [import]uid: 127675 topic_id: 30559 reply_id: 122457[/import]

Damn, you only waited 4 hours to ask again… haha

Can’t you save your:
display.newSprite( sheet, { name=“dandelion”, start=1, count=99, time=250 } )

into the table?

images[#images + 1] = display.newSprite( sheet, { name=“dandelion”, start=1, count=99, time=250 } )

After this you can use: images[#images + 1].myName = “dandelion”, if you want.

I think i didn’t understand the whole question, sorry. It seems like i understood two different questions from the first and the second post… :frowning: [import]uid: 116842 topic_id: 30559 reply_id: 122462[/import]

To be honest I didn’t think that was possible.
I will try it, but it seems a little over engineered, I thought I might enable to keep it simpler?

Anyone else with a suggestion ? [import]uid: 127675 topic_id: 30559 reply_id: 122483[/import]

Simpler than using tables?

Have you tried what i said? it worked? I never used spritesheets, but i suppose it will work.
PS: a table is like a lot of variables inside a BIG variable… [import]uid: 116842 topic_id: 30559 reply_id: 122606[/import]

Anybody?

I just want to now how to add my sprite to the table and call it in the same way as the leafs?
I am really struggling as I can find no real resource to do this.
Is this the way to do this? maybe not
[import]uid: 127675 topic_id: 30559 reply_id: 122607[/import]

Sorry correo,

I just posted as you did.

I am not sure I get you, would you kindly explain it a little more clearly please.
thankyou for replying too [import]uid: 127675 topic_id: 30559 reply_id: 122608[/import]

Damn.
Declare a table.
And use it like all tables.

Why I can imagine, that you didn’t make any code nor try?

Try, search and ask.

PS: Try something, ( just ) 2 or 3 times, and if you can’t, I’ll explain. :slight_smile: [import]uid: 116842 topic_id: 30559 reply_id: 122609[/import]

Haha, i understand your name now!!! [import]uid: 116842 topic_id: 30559 reply_id: 122610[/import]

@correo,

hi, I didnt mean to come across as not being thankful.
I have tried messing with the tables for an hour or two this morning; I would never dream of not trying anything that someone has suggested. Infact its pretty exciting to get replies with suggestions.
I apologise if Ive offended you.

My issue is that I am learning, I have sprite sheets and animation down no problem, I can modulise code and bring it into the main.lua.
As for searching I have, both here and on the internet but nothing relates to sprites within a table.

Thanks anyway, I will keep looking. [import]uid: 127675 topic_id: 30559 reply_id: 122611[/import]

I’m not angry, i’m just an asshole.

Is there any error saving the sprites in a table? or the error is other?

Can i see the new code with tables?
I was thinking it was as easy as:
images = { }
images[#images + 1] = display.newSprite( sheet, { name=“dandelion”, start=1, count=99, time=250 } )
images[#images + 1].myName = “dandelion”
images[#images + 1]:addEventListener(“collision”, ondandelionCollision) [import]uid: 116842 topic_id: 30559 reply_id: 122612[/import]

And one thing i saw:
dandelion:addEventListener(“collision”, dandelion)

You should care naming the variable same as a function. If you name the function like something what he is doing like ondandelionCollision would be better ( that’s what i see everywhere hehe ) [import]uid: 116842 topic_id: 30559 reply_id: 122613[/import]

@correo,

I see what your getting at ( not the a*****e thing)lol

I was just adding the spritesheet reference to the table and calling the external dandelion.lua.
I will give it another go.

thank you [import]uid: 127675 topic_id: 30559 reply_id: 122614[/import]

Sorry, but i didn’t understand:
I see what your getting at ( not the a*****e thing)lol

and the asterisk are not helping :frowning:
( I’m spanish and that doesn’t help )

I couldn’t understand, too:
I was just adding the spritesheet reference to the table and calling the external dandelion.lua.

i imagine that you are saying that you were mixing the two codes, or something like that. [import]uid: 116842 topic_id: 30559 reply_id: 122616[/import]

Okok, you missed an “e” in your. Google Translator told me ;). [import]uid: 116842 topic_id: 30559 reply_id: 122617[/import]

*UPDATE*

I have eventually solved my problem.
Thanks go out to my Spanish friend for suggestions which guided me to the correct path.
I am a happy man

Rock on! [import]uid: 127675 topic_id: 30559 reply_id: 122624[/import]

I have eventually solved my problem.

What did u do? and why eventually? [import]uid: 116842 topic_id: 30559 reply_id: 122625[/import]

Anyone?

ok, if I have explained it badly, I would like to in a hypothetical world just add my sprite dandelion etc to the table.
i.e. where we have “leafs.png” leafs1.png" etc I would like to be able to add “sprite”(my dandelion sprite) “sprite1” etc

what would the process be to do this please?
just a pointer will do guys, I am not asking for code. [import]uid: 127675 topic_id: 30559 reply_id: 122457[/import]

Damn, you only waited 4 hours to ask again… haha

Can’t you save your:
display.newSprite( sheet, { name=“dandelion”, start=1, count=99, time=250 } )

into the table?

images[#images + 1] = display.newSprite( sheet, { name=“dandelion”, start=1, count=99, time=250 } )

After this you can use: images[#images + 1].myName = “dandelion”, if you want.

I think i didn’t understand the whole question, sorry. It seems like i understood two different questions from the first and the second post… :frowning: [import]uid: 116842 topic_id: 30559 reply_id: 122462[/import]

To be honest I didn’t think that was possible.
I will try it, but it seems a little over engineered, I thought I might enable to keep it simpler?

Anyone else with a suggestion ? [import]uid: 127675 topic_id: 30559 reply_id: 122483[/import]