Make an object appear in frame N and not frame 0 in SpriteBuilder

93 views Asked by At

In SpriteBuilder, every object starts at frame 0.

I have some objects that will fall from the sky, but I want their falling animation (physics with gravity) to begin later in the scene, and not at 0.

Can I either have the objects exist at a certain frame? (i.e. insert empty frames before the object's first keyframe), or can I have the objects queue the physics gravity after a certain frame?

tia

2

There are 2 answers

2
Pontus Armini On BEST ANSWER

What you want to do can be done with a mix of Spritebuilder and code. It's not possible to animate/toggle any of the physics attributes in the Spritebuilder timeline, but you can set your own callbacks to achieve it. To add a callback in the timeline, you hold alt/option+left click in the callback field in the SB timeline and then double click the little square to set the selector. To hide an object until you want it to show up, you can add a 'Visible' keyframe: set the cursor at the time you want the object to show and press 'v'.

So, to do what you want I'd uncheck the 'Affected by gravity" checkbox in SBs "Item physics"-tab. (Obviously, this will make the object to not fall down) Then at a certain point make the object visible (if you've hidden it, that is) and make a callback to something like this:

func toggleGravityCallBack() {
  yourObject.physicsBody.affectedByGravity = true
}

Hope this helps!

4
Almo On
int frameNum = 5;

CCSpriteFrame* theframe = ((CCAnimationFrame*)[animation.frames objectAtIndex:frameNum]).spriteFrame;

CCSprite* newSprite = [CCSprite spriteWithSpriteFrame:theframe];

This creates a new sprite starting at frame five of the animation.