How to changing action in SpriteKit - ios

Currently my character having this to make action:
SKAction* action1 = [SKAction repeatActionForever:[SKAction animateWithTextures:textureArray timePerFrame:0.2]];
[_character runAction:action1];
then during the game, at a condition, I need to change the character's texture as well as his action:
SKAction* action2 = [SKAction repeatActionForever:[SKAction animateWithTextures:textureArray2 timePerFrame:0.2]];
SKAction* changeTexture2 = [SKAction setTexture:[SKTexture textureWithImageNamed:#"newtexture"]];
[_character runAction:[SKAction sequence:#[changeTexture2,action2]]];
I can see the character with the old textture and action 1, then at condition satisfied, he changed texture, but no action shows.
I tried remove first action, and many different change but I can not get he change both appearance and action. I really don't know how they change the action in the game? I tried to google but I can't really find any right place ...
Thank you!

try with this changes:
SKAction *action1 = ...
[_character runAction:action1 withKey:#"action1"];
after:
[_character removeActionForKey:#"action1"];
[_character setTexture:[SKTexture textureWithImageNamed:#"newtexture"]];
SKAction *action2 = [SKAction animateWithTextures:textureArray2
timePerFrame:0.2]];
[_character runAction:[SKAction repeatActionForever:action2]];

Related

Modify SKActions during the game

I am developing a game where all the enemies are SKNodes in their own classes. Within my SKScene I am spawning the mobs via allocing them and calling a specific method for spawning.
However, when they are spawned each mob are defined with a set of actions they run during their lifetime. One example are for one specific mob is:
SKAction *moveLeft = [SKAction moveToX:0 - (fragment.size.width/2) + (width / 2) duration:1.0];
SKAction *moveRight = [SKAction moveToX:(fragment.size.width / 2) - (width / 2) duration:1.0];
SKAction *sequence = [SKAction sequence:#[moveLeft, moveRight]];
SKAction *bounceOnWalls = [SKAction repeatActionForever:sequence];
[enemy runAction:bounceOnWalls];
So, back to my question. How can I modify a specific SKAction after its been created? I would like to change lets say the speed of moveLeft for all enemies that have this SKAction.
You can run action with key :
SKAction *moveLeft = [SKAction moveToX:0 - (fragment.size.width/2) + (width / 2) duration:1.0];
SKAction *moveRight = [SKAction moveToX:(fragment.size.width / 2) - (width / 2) duration:1.0];
SKAction *sequence = [SKAction sequence:#[moveLeft, moveRight]];
SKAction *bounceOnWalls = [SKAction repeatActionForever:sequence];
[enemy runAction:bounceOnWalls withKey:#"moving"]; //Run action with key
And when you need to change the speed on all nodes which running that action, you can use enumerateChildNodesWithName method. Like this:
[parentNode enumerateChildNodesWithName:name usingBlock:^(SKNode *node, BOOL *stop){
if([node actionForKey:#"moving"]){
SKAction* action = [node actionForKey:#"moving"];
action.speed = 1.5f;
}
}];
You could probably change dynamically duration of actions and affect in that way on speed of moving nodes, but I think that changing speed of an action directly is better choice. Take a look at both answers in this example on how you can change duration parameter dynamically.

SKAction sequence temporary delay (initial delay?)

So in the game I'm building I want to repeat an action, but I want it to have an initial delay. So for example, the action would execute three seconds after the user started the game, but after it executes for the first time, there's no longer a three second delay. What can I do to solve this?
Thanks in advance!
You could use an SKAction to make a delay, then put it at the beginning of your sequence.
Apple gives some sample code on sequences:
SKAction *moveUp = [SKAction moveByX:0 y:100.0 duration:1.0];
SKAction *zoom = [SKAction scaleTo:2.0 duration:0.25];
SKAction *wait = [SKAction waitForDuration: 0.5];
SKAction *fadeAway = [SKAction fadeOutWithDuration:0.25];
SKAction *removeNode = [SKAction removeFromParent];
SKAction *sequence = [SKAction sequence:#[moveUp, zoom, wait, fadeAway, removeNode]];
[node runAction: sequence];
You can use SKAction waitForDuration to make a delay.

RunAction: WithKey: Not working in sprite kit

I am using following code to fire a missile.
SKAction *missileMoveAction = [SKAction moveTo:destination duration:1.0];
SKAction *moveMissileActionWithDone = [SKAction sequence:#[_missileShootSound, missileMoveAction]];
[_cannonMissile runAction:moveMissileActionWithDone];
This is working fine but as soon as i use
[_cannonMissile runAction:moveMissileActionWithDone withKey:#"Canon_Fired"];
Nothing happen the run action do not move the object to destination. What is the problem with using RunAction: WithKey:?

iOS 7 Sprite Kit speed change for animation

I have a runAction which is animating a SKSpriteNode. I have the node moving up and down in a repeatActionForever. I would like the node to slow down as the node is moving up and speed up as the node is moving down.
[node runAction:[SKAction repeatActionForever:
[SKAction sequence:#
[[SKAction speedTo:0.1 duration:0.5],
[SKAction moveToY:2 * node.size.height / 3 duration:0.5],
[SKAction speedTo:1 duration:0.5],
[SKAction moveToY:node.size.height / 2 duration:0.5],
[SKAction moveToY:node.size.height duration:1],
[SKAction moveToY:node.size.height / 2 duration:1]]]]];
When I add the line [SKAction speedTo:0 duration:0.5], the rest of the code is run at speed of 0 after the 0.5 seconds even though I added a second speedTo action which would increase the speed to 1.
The problem: How do I change the speed of a node as the node is moving rather than having a stagnant speed for each direction.
Thanks in advance.
Look up the various types of SKActionTimingMode and apply the ones as needed to your situation. This will remove the need for anything like [SKAction speedTo:0.1 duration:0.5].
https://developer.apple.com/library/mac/documentation/SpriteKit/Reference/SKAction_Ref/Reference/Reference.html#//apple_ref/c/tdef/SKActionTimingMode
You can use the SKActionTimingEaseOut for the action that makes the node move up and SKActionTimingEaseIn for the action that makes the node move down.
SKAction *actionMoveUp = [SKAction moveToY:2 * node.size.height / 3 duration:0.5];
actionMoveUp.timingMode = SKActionTimingEaseOut;
SKAction *actionMoveDown = [SKAction moveToY:node.size.height / 2 duration:0.5];
actionMoveDown.timingMode = SKActionTimingEaseIn;
SKAction *actionMoveUpHalf = [SKAction moveToY:node.size.height duration:1];
actionMoveUp.timingMode = SKActionTimingEaseOut;
[node runAction:[SKAction repeatActionForever:
[SKAction sequence:#
[actionMoveUp,
actionMoveDown,
actionMoveUpHalf,
actionMoveDown]]]];

SKAction scaleBy: duration: SpriteKit

I have a trouble trying to make one circle big and small using [SKAction scaleBy: duration:]
SKAction *scaleDown = [SKAction scaleBy:0.2 duration:1.8];
SKAction *scaleUp= [scaleDown reversedAction];
SKAction *fullScale = [SKAction sequence:#[scaleDown, scaleUp, scaleDown, scaleUp]];
[_circleChanging runAction:fullScale];
What I get is the circle becoming so small that disappears and then doesn't come back. It has to become small and then come back to his original size doing it 2 times.
Try:
SKAction *scaleDown = [SKAction scaleTo:0.2 duration:0.75];
SKAction *scaleUp= [SKAction scaleTo:1.0 duration:0.75];
SKAction *fullScale = [SKAction repeatActionForever:[SKAction sequence:#[scaleDown, scaleUp, scaleDown, scaleUp]]];
[_circleChanging runAction:fullScale];
Not all actions are reversible, and the reverse sometimes doesn't mean "go back to the original value".
If you check the documentation, the reverse action of scaleBy is actually scaling to -0.2 in your case. Just create a new scale action instead of reversing.
Also try making a copy of the actions for the 2nd use:
SKAction *fullScale = [SKAction sequence:
#[scaleDown, scaleUp, [scaleDown copy], [scaleUp copy]]];

Resources