SpriteGrabber class -SpriteSheets in 2 lines

@amigoni

Shouldn’t your config file read:
["@2x"] = 2

instead of
["@2"] = 2

??

Don’t you need the ‘x’ in your config file if your file is named “menuSprite@2x.png”? [import]uid: 22392 topic_id: 3197 reply_id: 24792[/import]

@noahm26

Nice vision there! [import]uid: 7356 topic_id: 3197 reply_id: 24872[/import]

I’m using SpriteGrabber but animations refuse to play within functions.

example:

foo = SpriteSheet.grabSprite("foo.png", true, {foonimate={1,3,500,0}})  
  
local function playFoo()  
 if stupidBirdCollisionWithFuelTanker == true then  
 foo:playClip("foonimate")  
 end  
end  
Runtime:addEventListener( "EnterFrame", playFoo );  

I’m new to lua, so could it be that I’m just doing the function incorrectly? [import]uid: 10622 topic_id: 3197 reply_id: 25243[/import]

@Snow_

Welcome to the club!
In your first lines you should have declared the spritesheet first:

[lua]grabber=require(“SpriteGrabber”)
local fooSheet=grabber.grabSheet(“pngname”)
local foo = fooSheet:grabSprite(“spritename”, true, {foonimate={1,3,500,0}})

local function playFoo()
if stupidBirdCollisionWithFuelTanker == true then
foo:playClip(“foonimate”)
end
end
Runtime:addEventListener( “EnterFrame”, playFoo );[/lua]

In the second line give the name that you see in the .png/.lua spritesheet files.

In the third line give the rootname of one of the sprites you have included in the spritesheet. This is basically the name that you see in your raw images that you used to build the spritesheet (e.g. if you have 16 images hero01.png, hero02.png … hero16.png you should give “hero” in the third line).
The first line should be somewhere in your main.lua (just once in your project).
The second line should be given each time you firstly use a new spritesheet (sprites collection).
The third line should be given each time you want to spawn a sprite from a declared spritesheet in your code.

Plz, tell us if you still have difficulties with this. [import]uid: 7356 topic_id: 3197 reply_id: 25253[/import]

“EnterFrame” ~= “enterFrame” [import]uid: 8850 topic_id: 3197 reply_id: 25278[/import]

Oh, sorry, yeah I have the first lines already to call the spritesheet and properly initiate the sprites. That works flawlessly.

The problem is simply that I can animate any sprite outside functions… they just don’t animate inside a function and I’m wondering if that is because of the function loop (as in I need to call it differently) or if it’s a corona bug or what.

Right now my code is set up just as you described

--These are the very first lines in my code.   
--I named my sheet "SpriteSheet.png" in Texture Packer  
--Following is still example code though...  
  
local grabber=require("SpriteGrabber")  
local fooSheet=grabber.grabSheet("SpriteSheet")  
  
--Then I have some variables  
local bird, fueltanker, foo  
local collision = false  
  
--Then I create my sprites  
bird = SpriteSheet:grabSprite("bird01.png", true)  
fuelTanker = SpriteSheet:grabSprite("truck.png", true)  
foo = SpriteSheet:grabSprite("spritename", true, {foonimate={1,3,500,0}})  
  
--Then I have a function where I want to play the foo animation  
--when the bird collides with the truck  
  
local function playFoo()  
 if collision == true then  
 --foo:show(bird.x, bird.y) if I had it hidden first,  
 --otherwise:  
 foo.x = bird.x  
 foo.y = bird.y  
 bird:hide()  
 bird.y = -40 --makes sure it's off stage  
 foo:playClip("foonimate") --doesn't work, even if  
 --collision was set to false  
 --and I made it happen after  
 --impact. The sprite shows  
 --but the animation sits on  
 --the first frame.  
 end  
end  
  
Runtime:addEventListener( "enterFrame", playFoo );  

So yeah, that’s all I need to figure out is why the animation stays on the first frame and doens’t animate. I’m wondering that if each time the function loops, it restarts the animation, thus freezing it on the first frame all the time.

Thanks for your time. :slight_smile:

Oh and I forgot to mention that in my game, my sprite has more than one clip. Another example to explain would be as follows…
Lets say I have a guy who slides down a hill and then runs everytime he touches solid ground:

--I've set up spritegrabber and variables and here I create my guy sprite:  
guy = SpriteSheet:grabSprite("guy01.png", true, { sliding={1,1,0,0}, running={2,12,50,0}})  
  
--Collision function here  
  
--then a simple function that makes the guy sprite slide or run:  
local function guyMove()  
 if groundCollision == true then   
 guy:playClip("running") --if this was my code, won't work for me.  
 else  
 guy:playClip("sliding")  
 end  
end  
  
--and finally the event listeners:  
Runtime:addEventListener( "enterFrame", collisionFunction );  
Runtime:addEventListener( "enterFrame", guyMove );  

edit: fixed “enterFrame” Funny to have to debug sample code lol, thanks for the heads up dmrev. [import]uid: 10622 topic_id: 3197 reply_id: 25277[/import]

@Snow_

You have set running to last for 50 milliseconds, which is barely visible. Try to set it to one second (1000) and see what happens. [import]uid: 7356 topic_id: 3197 reply_id: 25281[/import]

Sorry that was just an example, I was cutting and pasting the code. But, even if I have it set to 1000 and say the animation was 12 frames - it will just hang on the first frame and never animate (within a function).

One of my animations for a character is a 2 frame animation which can run at 50 ms. I tested the animation and it looks awesome (well again, it works outside of a function, not in one). [import]uid: 10622 topic_id: 3197 reply_id: 25282[/import]

Is “guy01.png” intentionally set this way? Shouldn’t it be “guy.png” ? [import]uid: 7356 topic_id: 3197 reply_id: 25283[/import]

That’s how I’ve intentionally made my sprite images. They’re all numbered, so that when I make a clip I can just go to that number, then select number of frames, etc, etc. I also like to have my spritesheets organized, so all all images of one sprite for either single poses or animations are grouped together.

In the example above with the guy… guy01.png would be the regular sliding sprite (which is the default sprite that is on screen most of the time) and then the running sprite would be made up of guy images: guy02.png, guy03.png, guy04.png, etc, etc. Doing this this way makes for a nice tidy spritesheet and this method works great for animating sprites and keeping track. [import]uid: 10622 topic_id: 3197 reply_id: 25286[/import]

From what you are saying, I think you should indeed set your code to:

[lua]guy = SpriteSheet:grabSprite(“guy.png”, true, { sliding={1,1,0,0}, running={2,12,50,0}})[/lua]

The sprite name is the root name. Think of it like the prefix name you use for all the images that form your clips for a character. The “guy01.png” is just the frames of the first clip. It’s not the sprite, it is just the first part of the (one) sprite that.

You can have other characters in your spritesheet too. Just give a different prefix name to their images (e.g. enemy01.png,enemy02.png…) and tell SpriteGrabber to make sprites from the rootname (e.g. grabSprite(“enemy.png”) …)

Feel free to continue this discussion if the issue insists! [import]uid: 7356 topic_id: 3197 reply_id: 25291[/import]

It won’t really matter what I name the files. The way I have it set up is that blah01.png is the default sprite I want to show most of the time. The index is thus 1. So it would start like this: default={1,1,0,0}. If I want an animation that’s 3 frames and start’s with blah03.png, then I’d enter animation={3,3,1000,0} for example. If renaming my default sprite were to work, that would be interesting.

Here is a demo I whipped up that should hopefully recreate my problem in your simulator. (If it works - both punchy sprites animate - for you, then there’s a problem with either my simulator or computer).

http://www.filefactory.com/file/cabe33b/n/foo_demo.zip

There are 2 punchy the ghost sprites. Each with 3 images: punchy01, punchy02 and punchy03. One sprite is animated and sits up at the top left showing what the punching animation should look like. It’s clip is “p2” --> p2={2,2,500,0}.

The other one moves down the screen and should start punching the sad man, when he collides with it. If the problem occurs on your simulator as well, that the clip indeed changes from the default to the animated clip, but the animation freezes on the first frame of the animation (punchy02).

So hopefully, we can figure this out.

My inexperience with lua is the eventlisteners. I did do flash coding before, but it was pretty simple AS2 at the time and I’m usually using other languages.

Thank you for your patience and helping me thus far. :slight_smile: [import]uid: 10622 topic_id: 3197 reply_id: 25299[/import]

@Snow

The problem in your code was that you was beginning to play p2 animation at every collision frame so the second frame could never be played.

Try this as your main.lua code:

[lua]–Title: Punchy the Ghost
–This is a broken animation demo by Snow.
–The crappy mouse drawn images and code are completely free
–to use if you actually wanted to use them. I am not responsible
–if your simulator or test device literally starts to cry.

–Set up spritegrabber
local grabber = require(“SpriteGrabber”)
local SpriteSheet=grabber.grabSheet(“SpriteSheet”)

–display my background

–set up my variables
local punchy, sadman
local collision = false
–create my sprites
–Note that I have 2 punchy sprites, one is displayed top left showing what the punching animation “p2”
–should look like.

–The first punchy sprite spawns above screen. P1 is the default sprite, P2 is the punching animation.
punchy = SpriteSheet:grabSprite(“punchy”, false, { p1={1,1,1,0}, p2={2,2,500,0} })
punchy:show(160,-40)
–This is the punchy sprite that is displayed top left.
staticPunchy = SpriteSheet:grabSprite(“punchy”, false, { p1={1,1,1,0}, p2={2,2,500,0} })
staticPunchy:show(80,100)
–This animation works perfectly here
staticPunchy:playClip(“p2”)
–This is the stupid sadman sprite who just stands there and waits to get punched
sadman = SpriteSheet:grabSprite(“sadman.png”, true)
sadman:show(190, 420)

–A single function that moves punchy, handles collision and plays appropriate clip
local function movePunchy()
punchy.y = punchy.y + 3
–an ultra simple way of doing collision, if punchy is just above to just below sadman - collision == true.
if punchy.y >= 270 and punchy.y <=470 then
collision = true
else
–reset collision when not true
collision = false
end

–here was a logical bug
if collision == true then
if punchy.animating==false then
punchy:playClip(“p2”) --animation
end
else
punchy:prepare(“p1”) --go back to the default clip when collision is false
end
if punchy.y >= 500 then
punchy.y = -40
end
end

Runtime:addEventListener( “enterFrame”, movePunchy );[/lua]

I had an issue with your background image (it is shown as a white background here) so I have removed it from the code.

Is this ok now?

PS: Take a look at line 23. You don’t have to give numbered image names -just the root prefix. Actually, giving numbered names may cause issues. [import]uid: 7356 topic_id: 3197 reply_id: 25312[/import]

Oh. I forgot, I know why png’s go white like that. If you use TP on windows, it put’s out a specific encoded png or something. The way to fix it, is to load it in Pixen on Mac and then resave it as png. It works excellent then.

Anyway. Thank you so much for your help today Magenda. That works great… and now I understand about the naming thing. I forgot about prefixes. I also had a brainfart about the sprite:show() function… :stuck_out_tongue:

I knew it was something that I was missing. I just assumed that if you called playClip, it would just run the animation by default. Now I kind of feel silly for not looking at the docs a bit more about Corona’s sprite animation. :confused:

Now I can fix my game finally. :smiley: I really appreciate all your help and thank you for making spritegrabber. [import]uid: 10622 topic_id: 3197 reply_id: 25319[/import]

You are welcome! [import]uid: 7356 topic_id: 3197 reply_id: 25320[/import]

@Snow_ can you please send me one of these white pngs? How do they look if you open them in firefox or some other program? -> support at code-and-web.de

I am not doing anything else than under MacOS…
[import]uid: 9611 topic_id: 3197 reply_id: 25373[/import]

Hello,

@Magenda its possible to use SG with physics for a simple extracted images ?

Regards.
Francisco. [import]uid: 11749 topic_id: 3197 reply_id: 27929[/import]

@ffm

Yes you can use SpriteGrabber to visualize sprites that become physics objects too. However, you will encounter some known issues that exist because the immaturity of the SDK regarding the sprites engine. For example, read posts 24 to 30 of this thread.

http://developer.anscamobile.com/forum/2010/10/29/spritegrabber-class-spritesheets-2-lines#comment-10735 [import]uid: 7356 topic_id: 3197 reply_id: 27957[/import]

Hello,

@Magenda I am useing now SG and this rock. Grate work.

I have a question about if would be possible to assing the sprite to a group how param when you use “grabber:grabSprite” ?

Regards.
Francisco. [import]uid: 11749 topic_id: 3197 reply_id: 28238[/import]

Plz provide more info on this. What exactly do you want to achieve? You want to pass the SG arguments to an other function?

You can post your code here and I’ll help you on this. [import]uid: 7356 topic_id: 3197 reply_id: 28244[/import]