Reputation: 55
I would like to run two actions at exactly same time. I would like to move object in X way and Y way at the same time. I am trying running actions like this:
sprite.position = CGPointMake(300, 300);
SKAction *action3 = [SKAction moveTo:CGPointMake(sprite.position.x,100) duration:0.5];
SKAction *action2 = [SKAction moveTo:CGPointMake(50,300) duration:1];
SKAction *group = [SKAction group:@[action2,action3]];
[sprite runAction:group];
But this is making first action3 and then action2. What i am trying to make is that object(node) is moving up and down on Y coordinate and at the same time node have to move by X coordinate.
Upvotes: 2
Views: 2242
Reputation: 64477
SKAction *action3 = [SKAction moveTo:CGPointMake(sprite.position.x,100) duration:1];
SKAction *action2 = [SKAction moveTo:CGPointMake(50, 300) duration:1];
The problem is that you can't run two move actions simultaneously. One will override the other's behavior, so effectively only one action's results will show up on screen.
You have to ask yourself what your goal here is, how exactly should the sprite move? Assuming you want to move the sprite on the X axis to point 50 (because the other action leave's the X coord unchanged) but where should it go on the Y axis, 100 or 300? The sprite can't do both at the same time.
If you want a more complex movement behavior, such as a continuous up/down movement while moving horizontally, you'll have to use moveToX: and moveToY: independently and time them differently. For example:
SKAction *moveLeft = [SKAction moveToX:50 duration:4];
SKAction *moveUp = [SKAction moveToY:300 duration:2];
SKAction *moveDown = [SKAction moveToY:100 duration:2];
[sprite runAction:moveLeft];
[sprite runAction:[SKAction sequence:@[moveUp, moveDown]]];
The sprite takes 4 seconds to move to the left, while it takes 2 seconds to move up to 200 and another 2 seconds to move down to 100. At the end it will be at 50,100.
Btw using a group action is equivalent to simply calling runAction: once for each action in the group.
Upvotes: 1
Reputation: 2130
It sounds like you're wanting to basically have it bounce vertically and move in the X direction while doing that, if I'm understanding correctly.
If so, this should work:
SKAction *up = [SKAction moveByX:0 y: 100 duration:1];
SKAction *down = [SKAction moveByX:0 y:-100 duration:1];
SKAction *action1 = [SKAction repeatActionForever:[SKAction sequence:@[up, down]]];
SKAction *action2 = [SKAction moveByX:200 y:0 duration:5];
SKAction *group = [SKAction group:@[action1, action2]];
Upvotes: 3