I am creating a relatively complicated animation sequence. In it, a certain SKSpriteNode (shark) does two rotations. At the beginning of the animation, it rotates around a certain anchor point ap1, then later rotates around a different anchor point ap2. How should I change anchor points midway through an animation sequence?
Some initial thoughts:
I could change the anchor point outside of SKActions, in the update: loop perhaps.
I could use multiple SKSpriteNodes for the same shark sprite (with their respective anchor points), switching (hiding/showing) the sprite nodes when I need to change the anchor point.
Since changing a sprite's anchor point affects where it's rendered, you will likely need to make some sort of adjustment to prevent the sprite from appearing to suddenly move to a new location. Here's one way to do that:
Create action that changes the anchor point
SKAction *changeAnchorPoint = [SKAction runBlock:^{
[self updatePosition:sprite withAnchorPoint:CGPointMake(0, 0)];
}];
SKAction *rotate = [SKAction rotateByAngle:2*M_PI duration: 2];
Run action to rotate, change the anchor point, and rotate
[sprite runAction:[SKAction sequence:#[rotate,changeAnchorPoint,rotate]] completion:^{
// Restore the anchor point
[self updatePosition:sprite withAnchorPoint:CGPointMake(0.5, 0.5)];
}];
This method adjusts a sprite's position to compensate for the anchor point change
- (void) updatePosition:(SKSpriteNode *)node withAnchorPoint:(CGPoint)anchorPoint
{
CGFloat dx = (anchorPoint.x - node.anchorPoint.x) * node.size.width;
CGFloat dy = (anchorPoint.y - node.anchorPoint.y) * node.size.height;
node.position = CGPointMake(node.position.x+dx, node.position.y+dy);
node.anchorPoint = anchorPoint;
}
As Julio Montoya said, the easiest way to do this is to just "translate" code into an SKAction with the method [SKAction runBlock:myBlock].
Related
I'm running a rotateBy action that's repeated forever for a node, and occasionally I want to get the node's current rotation. But zRotation always return 0. How can I get the correct zRotation for this node?
Edit: so finally what I'm doing is creating a custom rotation action instead of using Sprite Kit's default action. A lot more complicated and hard to extend but it does what I want and can be encapsulated in each node's own class.
I believe the reason why you are getting 0 is because you are using animation. The properties of nodes undergoing animation are not updated at each step in the simulation. You will need to manually rotate the node in the update method or create a physics body and set the angular velocity. Using real-time motion will allow you to constantly monitor the properties of you nodes at each step in the simulation.
You can see an example of simulating rotation using real-time motion (albeit more complex) in my answer here.
If you paste the following into a new project, you should see the zRotation property change over time. With an 180 degree/second rotational rate and 60 FPS, the zRotation changes by ~3 degrees per update.
#implementation GameScene {
SKSpriteNode *sprite;
}
-(void)didMoveToView:(SKView *)view {
self.scaleMode = SKSceneScaleModeResizeFill;
sprite = [SKSpriteNode spriteNodeWithImageNamed:#"Spaceship"];
sprite.xScale = 0.5;
sprite.yScale = 0.5;
sprite.position = CGPointMake (CGRectGetMidX(view.frame),CGRectGetMidY(view.frame));
SKAction *action = [SKAction rotateByAngle:M_PI duration:1];
[sprite runAction:[SKAction repeatActionForever:action]];
[self addChild:sprite];
}
-(void)didEvaluateActions {
// Wrap at 360 degrees
CGFloat angle = fmod(sprite.zRotation*180/M_PI, 360.0);
NSLog(#"%g",angle);
}
#end
For the sake of simplicity, the scene has a circle sprite and a square sprite. The square sprite is the child of an SKNode that follows around the circle sprite so that rotation of the square always happens around the circle. However, when the node is rotated, the square randomly drifts upwards. This behavior stops once the rotation makes its way all the way around. Here is the code:
_square = [SKSpriteNode spriteNodeWithImageNamed:#"square"];
_circle = [SKSpriteNode spriteNodeWithImageNamed:#"circle"];
_circle.position = CGPointMake(-200, 300);
[self addChild:_circle];
_rotateNode = [SKNode node];
_rotateNode.position = CGPointMake(300, 300);
[self addChild:_rotateNode];
[_rotateNode addChild:_square];
SKAction *moveBall = [SKAction moveTo:CGPointMake(1200, 300) duration:12];
[_circle runAction:moveBall];
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
SKAction *rotate = [SKAction rotateByAngle:-M_PI_2 duration:0.2];
[_rotateNode runAction:rotate];
}
-(void)update:(CFTimeInterval)currentTime {
_rotateNode.position = _circle.position;
double xOffset = (300 -_circle.position.x);
double yOffset = (300 - _circle.position.y);
_square.position = CGPointMake(xOffset, yOffset);
}
Anyone know why this is happening?
If I understand the question, you are asking why the square moves in unexpected ways? The circle is moving, but on every frame you are setting the rotateNode's position to be the same as the circle, and they both have the same parent: self, so we can ignore circle for any debugging because it just executes its action every frame to get a new position.
The oddness, most likely, comes in because you are manually repositioning rotateNode, which would then move all of its children, including square. But in every frame you are also repositioning square manually. So rotateNode rotates, changes the position of square because it rotated, and then you reposition square to have a fixed offset. Further complicated depending on whether your goal is to position the square in world space or in parent space. But it's not clear what your goal is there.
I'm currently trying SpriteKit for first time and using some actions. I was under the impression that every node/sprite you create you would need to remove it but for some reason they are removed by themselfs as soon as they pass the screen height. Is this a normal behavior in SpriteKit?
Here is the code.
I'm basically using an action to animate a sprite across the screen (from bottom to top in protrait-mode), the funny thing is that the node count stays at 0 no matter how many sprites I create, it actually counts them but it subtracts them as soon as they pass the screen height.
-(void) shoot
{
bullet = [SKSpriteNode spriteNodeWithImageNamed:#"bullet"];
bullet.position = CGPointMake(airPlane.position.x, airPlane.position.y + 50);
[self addChild:bullet];
SKAction *moveBullet = [SKAction moveTo:CGPointMake(airPlane.position.x, self.size.height + 20) duration:0.5];
[bullet runAction:moveBullet];
}
If I change my action to where the sprite doesn't cross the screen height then the node count keeps the count.
Change this line...
SKAction *moveBullet = [SKAction moveTo:CGPointMake(airPlane.position.x, self.size.height + 20) duration:0.5];
... to this
SKAction *moveBullet = [SKAction moveTo:CGPointMake(airPlane.position.x, self.size.height) duration:0.5];
Again Is this a normal behavior in SpriteKit?
Thanks a lot
The node count displayed on the lower right corner is just an indication of the nodes which are being rendered on the screen. They are not an indication of the total nodes in the scene.
Use the following code
[skView setValue:#(YES) forKey:#"_showsCulledNodesInNodeCount"];
At the same place where you call
skView.showsFPS = YES;
skView.showsNodeCount = YES;
Also, please have a look at this answer.
that can be a memory leak because sprite kit holds textures in the memory for further use for better result remove it from the screen when its passing out of the screen and set it manually to nil
for eg
[self enumerateChildNodesWithName:name usingBlock:^(SKNode *node, BOOL *stop) {
if(node.x>900)
{
//remove all the action
[node removeAllActions];
//remove node from parent
[node removeFromParent];
//set nil//not required set automatically to nil when node is remove from parent
node=nil;
}
}];
i have a method to create sprites with an initial position, final position, height, image of the sprite and a name property to the sprite (to access the sprite later)
The problem is when i need to debug, because i can't display, for example, a number (of a position in this case...)
In this case, i want to know what sprite is disapearing sooner than expected, so i want to set a new parameter in my method which would be an NSString number(1,2,3..)so i could localize what sprite is making troubles... but i cant display this number or i don't know how. The other debugging technique i thought to do were to put an if(x.position> 600)callAMethod with a label, but... the coordinates are CGPoint and not float. So i'm a bit lost... Do anybody know a technique to do this??
here is my method to create sprites:
-(void)crearMontanaconLaPosicionFinal:(CGFloat)xFinal inicial:(CGFloat)xInicial altura:(CGFloat)altura imagen:(NSString*)imagen nombre:(NSString*)nombre
{
CGPoint posicionFinal = CGPointMake(xFinal, altura);
SKSpriteNode *montaña = [SKSpriteNode spriteNodeWithImageNamed:imagen];
montaña.name = nombre;
SKAction* parallaxMoveAction3 = [SKAction sequence:#[[SKAction moveTo:posicionFinal duration:20],[SKAction customActionWithDuration:0 actionBlock:^(SKNode* montaña, CGFloat elapsedTime){
[montaña setPosition:CGPointMake(xInicial,altura)];
}]]];
montaña.position = CGPointMake(xInicial, altura);
SKAction *repetir=[SKAction repeatActionForever:parallaxMoveAction3];
[montaña runAction:repetir];
[self addChild:montaña];
}
i want to make a parallax effect on my background title screen with mountains moving. The "trick" i want to make is when a mountain goes to the end of the screen, instead of destroying the node and create it again at the begining position, i just want to change it's position to the begining position and loop the rest of actions. For this i create my moutain sprite at the begining of the createSceneContents() function and i pass the sprite to a method animate() wich do allways the same combo of actions forever: animate to the right, then when it's at x position, change mountain.position.x to the begining...
-(void)createSceneContents{
//crear
SKSpriteNode *mountain = [SKSpriteNode spriteNodeWithImageNamed:#"mountain.png"];
mountain.name = #"mountain";
//initial position
mountain.position = CGPointMake(-161.5,15);
//animate
SKAction *animRight = [SKAction moveToX:801.5 duration:4];
SKAction *comboActions = [SKAction repeatActionForever:[SKAction performSelector:#selector(animate:mountain) onTarget:#"mountain"]];
And here i have my method declaration:
- (void) animate:(SKSpriteNode*)mountain
{
// code....
}
My problem is i'm allways having errors passing the SKSpriteNode *mountain in my method and
I'm going crazy trying everithing.
You can avoid performSelector action, using a custom action like this.
SKAction* parallaxMoveAction = [SKAction sequence:#[[SKAction moveToX:801.5 duration:4.0f],[SKAction customActionWithDuration:0 actionBlock:^(SKNode* node, CGFloat elapsedTime){
[node setPosition:CGPointMake(-161.5,15)];
}]]];
If you set custom action duration to 0 it will be performed only once.
On the other hand your perform selector action should look like this:
[SKAction performSelector:#selector(animate:) onTarget:self]
As far as i know you can't pass arguments to it.