Use sprite sheet frame as object - help please.

Hello.

I created four versions of a flower (4 different .png’s). From these 4 pictures I created a spritesheet.

I want to be able to drop one of the flowers in my app , though I want to drop them in at random and when they are dropped in, to become a physical body…I want to do things this way so it saves me having to copy and paste a bunch of code…Im liking sprite sheets a lot and use them wherever I can.

So basically, my app is supposed to have random flowers dropping from the top of the screen. I need to make each frame an object…

I am still trying different things to get it to work, but so far - no luck. So Im positing here to see if anyone can help me…I only been using corona about a week now and Ive never done this before with sprites, so any help is appreciated. :slight_smile:

Thank you. [import]uid: 157993 topic_id: 30269 reply_id: 330269[/import]

Using this as code so far. It does not work. Im wondering if I have the logic right or not… >_>

local flowerSheet = sprite.newSpriteSheet(“flowerSheet.png”, 100, 100)

local flowerSet = sprite.newSpriteSet (flowerSheet, math.random(1,4))

local flowers = sprite.newSprite (flowerSet)
flowers.x = math.random ()
flowers.y = -100
flowers:play()

Thanks for any help! [import]uid: 157993 topic_id: 30269 reply_id: 121282[/import]

I`m not that good using sprites as I really did not use them much, but based in the code you posted above, try changing the .x of the object as following:

[lua]local flowerSheet = sprite.newSpriteSheet(“flowerSheet.png”, 100, 100)

local flowerSet = sprite.newSpriteSet (flowerSheet, math.random(1,4))

local flowers = sprite.newSprite (flowerSet)
flowers.x = math.random (0, display.contentWidth) – this line here.
flowers.y = -100
flowers:play()[/lua]

As you did not say why the code isn`t working am assuming that it would be an possible issue btw.
Cheers,
Rodrigo. [import]uid: 89165 topic_id: 30269 reply_id: 121284[/import]

Tried the code. No go. My sprite sheet is loaded but instead of a frame being randomly chosen, displayed, and made a physics body to be interact-able with the user, I just get it displayed with all frames playing… Thanks though. I appreciate any help. :wink:

I still am wondering if i have my logic right here…Basically right now i just wanna get the sprites to randomly appear on the screen. I know how to do everything else. :slight_smile: [import]uid: 157993 topic_id: 30269 reply_id: 121285[/import]

What about this:

[lua]local flowerSheet = sprite.newSpriteSheet(“flowerSheet.png”, 100, 100)

local flowerSet = sprite.newSpriteSet (flowerSheet)

– random frames
local randomData =
{
name=“flowers”,
frames= { math.random(1,4) } – frame indexes of animation, in image sheet
–time = 50, – Optional. In ms. If not supplied, then sprite is frame-based.
–loopCount = 0 – Optional. Default is 0.
}

local flowers = sprite.newSprite (flowerSet, randomData)
flowers.x = math.random (0, display.contentWidth) – this line here.
flowers.y = -100
flowers:play()[/lua]

PS: : Sorry but as I said you, I`ve not played with the sprites API that much.
I hope that helps,

Rodrigo. [import]uid: 89165 topic_id: 30269 reply_id: 121287[/import]

Hi @jgcthatsme,

It appears that you’re using the older sprite library, which has been “depreciated” In favor of the current one detailed here:

http://docs.coronalabs.com/api/type/SpriteObject/index.html

The old one still works, but if you’re building a new app, you should use the new API. It’s better, cleaner, easier, and more efficient. Try switching over to this one, and if you still encounter issues, please post them here and I’ll try to help you through it.

Keep us posted!
Brent Sorrentino

[import]uid: 9747 topic_id: 30269 reply_id: 121308[/import]

Thanks RSC but its not working, not sure why. I didnt know i was using the old way. Im still new to Lua and I learn from the tutorials out there so it aint my fault lol…thanks though :slight_smile:

@ Brent,

I see. Okay, Im checking it out now in the docs, I’ll update if i have a question about anything. Thank you everybody. :slight_smile: [import]uid: 157993 topic_id: 30269 reply_id: 121402[/import]

Using this as code so far. It does not work. Im wondering if I have the logic right or not… >_>

local flowerSheet = sprite.newSpriteSheet(“flowerSheet.png”, 100, 100)

local flowerSet = sprite.newSpriteSet (flowerSheet, math.random(1,4))

local flowers = sprite.newSprite (flowerSet)
flowers.x = math.random ()
flowers.y = -100
flowers:play()

Thanks for any help! [import]uid: 157993 topic_id: 30269 reply_id: 121282[/import]

I`m not that good using sprites as I really did not use them much, but based in the code you posted above, try changing the .x of the object as following:

[lua]local flowerSheet = sprite.newSpriteSheet(“flowerSheet.png”, 100, 100)

local flowerSet = sprite.newSpriteSet (flowerSheet, math.random(1,4))

local flowers = sprite.newSprite (flowerSet)
flowers.x = math.random (0, display.contentWidth) – this line here.
flowers.y = -100
flowers:play()[/lua]

As you did not say why the code isn`t working am assuming that it would be an possible issue btw.
Cheers,
Rodrigo. [import]uid: 89165 topic_id: 30269 reply_id: 121284[/import]

Tried the code. No go. My sprite sheet is loaded but instead of a frame being randomly chosen, displayed, and made a physics body to be interact-able with the user, I just get it displayed with all frames playing… Thanks though. I appreciate any help. :wink:

I still am wondering if i have my logic right here…Basically right now i just wanna get the sprites to randomly appear on the screen. I know how to do everything else. :slight_smile: [import]uid: 157993 topic_id: 30269 reply_id: 121285[/import]

What about this:

[lua]local flowerSheet = sprite.newSpriteSheet(“flowerSheet.png”, 100, 100)

local flowerSet = sprite.newSpriteSet (flowerSheet)

– random frames
local randomData =
{
name=“flowers”,
frames= { math.random(1,4) } – frame indexes of animation, in image sheet
–time = 50, – Optional. In ms. If not supplied, then sprite is frame-based.
–loopCount = 0 – Optional. Default is 0.
}

local flowers = sprite.newSprite (flowerSet, randomData)
flowers.x = math.random (0, display.contentWidth) – this line here.
flowers.y = -100
flowers:play()[/lua]

PS: : Sorry but as I said you, I`ve not played with the sprites API that much.
I hope that helps,

Rodrigo. [import]uid: 89165 topic_id: 30269 reply_id: 121287[/import]

Hi @jgcthatsme,

It appears that you’re using the older sprite library, which has been “depreciated” In favor of the current one detailed here:

http://docs.coronalabs.com/api/type/SpriteObject/index.html

The old one still works, but if you’re building a new app, you should use the new API. It’s better, cleaner, easier, and more efficient. Try switching over to this one, and if you still encounter issues, please post them here and I’ll try to help you through it.

Keep us posted!
Brent Sorrentino

[import]uid: 9747 topic_id: 30269 reply_id: 121308[/import]

Thanks RSC but its not working, not sure why. I didnt know i was using the old way. Im still new to Lua and I learn from the tutorials out there so it aint my fault lol…thanks though :slight_smile:

@ Brent,

I see. Okay, Im checking it out now in the docs, I’ll update if i have a question about anything. Thank you everybody. :slight_smile: [import]uid: 157993 topic_id: 30269 reply_id: 121402[/import]