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]]]];
Related
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.
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]
]]
];
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.
so I have a sprite that is created every second and that is moving at a random position (sprite1) and another sprite that has a fixed position (sprite2). I would like that when sprite1 collide with sprite2, sprite1 is like sticked to it (it stops moving and is sticked to it) . How can I do this please ? sorry for my english I'm french :/
p.s : sprite2 is rotating with accelerometer, so if sprite1 collide with it I would like that it rotate too :)
I think, you can try to use box2d to do this. It will help to detect collisions and to manage rotations, movement, etc.
I think, you can do it simply in Cocos2d.
1) First set the rect for sprite1 and sprite2 using CGRectMake(x,y,width,height)
2) As you told sprite1 is moving at random position and sprite2 is fixed to particular position, you can check them collide by using CGRectIntersectsRect([sprite1 bounds],[sprite2 bounds]).
3) if it intersects, set sprite1.position = sprite2.position
Note: you said sprite1 is rotating, rect can be fit only to the regular bodies. if you want exact collision or physical properties for sprite better you can go for box2d.
If you don't want to use Box2d (which can handle circle collisions), you can try something like this:
1.) Detect collision, is the distance between the two circles center point (x,y), less than the sum of the two circles radius.
2.) Make the Sprite1 stick to Sprite2, Stop the movement of Sprite1, and save the relative delta (x,y) to Sprite2, then whenever Sprite2 moves or rotates apply the same delta movement and rotation to Sprite1.
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.