Using SpriteKit, I have created a sprite, and would like to display the sprite, pause for a second, then begin the animation using a new sprite.
Currently, I am using
self.neko = [SKSpriteNode spriteNodeWithImageNamed:#"Awake.ico"];
[self addChild:self.neko];
sleep(2);
[self.neko removeFromParent];
Based on the code provided, you would think the sprite would be drawn, then the thread would sleep for a couple of seconds, and then remove the sprite from the parent.... but the sprite is not being shown.
If I remove the removeFromParent line, the sprite will remain on the screen.
So the question is this, am i doing it correctly?
Is there a way to force the sprite to be updated on the scene before the sleep timer is executed?
You could use an SKAction like this to display the sprite for two seconds and then remove it:
[self.neko runAction:
[SKAction sequence:#[
[SKAction waitForDuration:2.0],
[SKAction removeFromParent]
]]
];
Related
Is there a way to enlarge a sprite node without moving the position?
I would like to animate a node to 'swell up' to a six * 6, then bring it back down to normal size in an animation loop?
I have it hard coded in a state block, where i do not move the image, but simply change the size of the sprite to simulate blowing up, then reducing the size back to normal - like when a creature was being pumped up in dig-dug....
But i was hoping there was a way to accomplish this by using something like moveTo and the animateWithTextures routine for movement.
is easy to do with the method scaleTo:duration: of SKAction
for example:
SKAction *big = [SKAction scaleTo:3 duration:0.5];
SKAction *small = [SKAction scaleTo:1 duration:0.5];
[node runAction:
[SKAction repeatActionForever:
[SKAction sequence:#[big, small]]]];
I am using SpriteKit, and my sprite is slowing as it approaches the touched point.
I would like the sprite to remain at a fixed speed as it moved from initial point to the touched point on the screen.
Currently, i have the duration set to 3.0
In the touchesEnded event, i gather the new point of the touch and save the value to a property.
In the scene update event, i perform a
float realMoveDuration = 3.0;
SKAction *actionMove = [SKAction moveTo:self.newPoint duration:realMoveDuration];
[self.player runAction:[SKAction sequence:#[actionMove]]];
So when the player sprite starts getting closer to the touch point, the movement slows down, and gradually reaches the touch point.
This is not the action I am looking for, i would like the player sprite to continue at a constant rate of travel to the touched point.
You should just run your SKAction in the touchesEnded event.
If you have that code in the update event, it's starting a new SKAction 60 times per second. You only ever need to run a new SKAction when there is a change in destination.
If there is a change in destination before you reach the original destination, you should remove the action from that node and then add a new one.
Also, not certain about the specifics of what you are doing, but how quickly an object moves is based on the distance and the time you have specified for it to get there. So if you want a node to move at a consistent speed, regardless of distance, you need to make a calculation to determine the right duration to set for your SKAction.
For example if speed represents pixels/units per second you might do this to calculate your duration :
duration = distance / speed;
If you are using an SKAction to move the sprite, use the timingMode property. The default is SKActionTimingLinear. So if you haven't changed it, the node should move at a constant speed.
Here's a link to the available Action Timing Modes.
SKAction *ghostAnimationAction =
[SKAction animateWithTextures:ghostFrames timePerFrame:0.1];
SKAction *ghostDelayAction =
[SKAction animateWithTextures:#[[SKTexture textureWithImageNamed:#"Ghost_"]]
timePerFrame:1.0];
SKAction *ghostAnimationSequence =
[SKAction sequence:#[ghostAnimationAction, ghostDelayAction]];
SKAction *repeatGhostAnimationSequence =
[SKAction repeatActionForever:ghostAnimationSequence];
I have an animated sequence of frames which is made up of an NSMutableArray of SKTexture objects. Once that sequence has played I want to fold on a still frame for a second before repeating everything. The code above works, but the only way I can find to specify holding on a frame for a duration is to use animateWithTextures:timePerFrame: and supply a single texture array. Is there another way to get an SKAction to display a single image for a duration that I am missing.
You could create a new SKAction that encloses both of these two actions. See the Sprite Kit Programming Guide section called 'Creating Actions That Run Other Actions' here Adding Actions To Nodes
Take a look at the SKAction member waitForDuration: too, you may be able to avoid passing in the single texture that way. Have the first SKAction run your animation, then have the second SKAction waitForDuration. Then your enclosing SKAction runs through these two actions forever or as long as needed.
I have a CADisplayLink as my main gameloop for a game, and a NSTimer that spawns enemies every ten seconds. I use...
-(void)togglePause{
displayLink.paused = !displayLink.paused;
if (displayLink.paused) {
[self.view addSubview:pauseOverlay];
}else {
[pauseOverlay removeFromSuperview];
}
...to pause the gameloop, but the timer that spawns enemies will continue to go on even after the games paused, if I destroy the timers and then make another one couldn't p just exploit the pause button and just hit it before the 10 seconds go off causing enemies never to spawn?
Is there an easy solution to this?
Ditch the NSTimer and just use a counter that you increment each time the CADisplayLink fires. Once it reaches n spawn your enemies and zero the counter. If the user pauses, when they resume the counter will be the same as it was.
Right now, in my game, I am spawning a sprite every second or so at the top of the screen (using a sceduler) using this code:
The init method:
[self schedule:#selector(addMeteor:) interval:1];
The scheduler method:
- (void)addMeteor:(ccTime)dt
{
CCTexture2D *meteor = [[CCTextureCache sharedTextureCache] addImage:#"Frame3.png"];
target = [CCSprite spriteWithTexture:meteor rect:CGRectMake(0, 0, 53, 56)];
//Rest of positioning code was here
}
Doing it this way causes a stutter in the frame rate every second or so (Whenever another sprite is spawned). Is there a way to eliminate that?
Thanks in advance!
Tate
I'm guessing the stutter is more likely coming from other parts of the code. Do you explicitly call removeChild on meteors? That might cause a hiccup, especially with many meteors.
My advice: create N meteor sprites up front. When you need one, make it visible and change its position. When you're done with it, set it to visible = NO to make it disappear.