Is there a way to change the image of a sprite which has already been initialized with another image?
I tried:
if ([node.name isEqualToString:#"NameX"]) {
SKAction *fadeOut = [SKAction fadeOutWithDuration:0.3];
SKAction *fadeIn = [SKAction fadeInWithDuration:0.3];
[self.sprite runAction:fadeOut];
[self runAction:fadeOut completion:^{
self.sprite = [SKSpriteNode spriteNodeWithImageNamed:#"NameY"];
[self.sprite runAction:fadeIn]
}];
}
There is. Internally, the spriteNodeWithImageNamed: class method just uses the image name you pass it to set the node's texture property. That being said, if at any point you want to arbitrarily change the node's texture, you can just set it directly.
[self.sprite setTexture:[SKTexture textureWithImageNamed:#"someOtherImage"]];
There are also some SKActions for doing this, in case you want the node to resize or animate between different textures.
[self.sprite runAction:[SKAction setTexture:[SKTexture textureWithImageNamed:#"someOtherImage"] resize:YES]];
[self.sprite runAction:[SKAction animateWithTextures:#[tex1,tex2,tex3] timePerFrame:0.5 resize:YES restore:YES]];
You must create texture array such as this:
[SKAction animateWithTextures:[NSArray arrayWithObjects:
[SKTexture textureWithImageNamed:#"im1.png"],
[SKTexture textureWithImageNamed:#"im2.png"],
[SKTexture textureWithImageNamed:#"im3.png"],
[SKTexture textureWithImageNamed:#"im4.png"], nil] timePerFrame:0.5 resize:YES restore:YES];
Related
So. A (hopefully) simple question. I have a spritenode in a game I am making. I want the character to animate its legs while it is moving. I can get it to animate its legs then move, on key press. But I cant get it to do both at the same time.
Here is my code (naturally I also have the other 3 directions laid out prior.):
‘’’
case 0x7C /* LEFT */:
TextureChangeIceMan1 = [SKAction animateWithTextures:[IcedMan returnMovementArrayWithKey: #"0x7C"] timePerFrame:0.1];
MoveIceMan1 = [SKAction moveByX:[IcedMan returnCharacterSpeed] y:0.0 duration:0.4];
break;
default:
NSLog(#"keyDown:'%#' keyCode: 0x%02X", theEvent.characters, theEvent.keyCode);
break;
}
TextureChangeIceMan1 = [SKAction repeatActionForever: TextureChangeIceMan1]; //make it unending
[IceMan1 runAction: MoveIceMan1];
[IceMan1 runAction: TextureChangeIceMan1];
‘’’
I'm trying determine why slide is happened. I have created 2 nodes, and one of them is moving using SKAction. But when one of them above on other, it does not moving with node under them.
It's a little bit hard to explain, so I have created the video:
https://www.youtube.com/watch?v=F4OuTBVd5sM&feature=youtube_gdata_player
Thanks for your replies!
Parts of code:
CGPoint positionPoint = [valuePoint CGPointValue];
SKSpriteNode *slab = [SKSpriteNode spriteNodeWithTexture:plitaTexture];
slab.name = #"plita";
slab.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:plitaTexture.size];
slab.position = positionPoint;
slab.physicsBody.dynamic = NO;
CGFloat duration = 3.0;
CGFloat firstDuration = duration/(self.size.width/slab.position.x);
SKAction *moveLeftFirstTime = [SKAction moveTo:CGPointMake(0.0 + plitaTexture.size.width/2, positionPoint.y) duration:firstDuration];
SKAction *moveLeft = [SKAction moveTo:CGPointMake(0.0 + plitaTexture.size.width/2, positionPoint.y) duration:5];
SKAction *moveRight = [SKAction moveTo:CGPointMake(self.size.width - plitaTexture.size.width/2, positionPoint.y) duration:5];
SKAction *movingRightLeft = [SKAction sequence:#[moveRight, moveLeft]];
SKAction *moving = [SKAction sequence:#[moveLeftFirstTime,[SKAction repeatActionForever:movingRightLeft]]];
[slab runAction:moving];
[self addChild:slab];
Rabbit init code:
-(instancetype)init {
if (self = [super initWithImageNamed:#"eating-rabbit1"]) {
self.physicsBody = [SKPhysicsBody bodyWithTexture:self.texture size:self.texture.size];
self.physicsBody.allowsRotation = NO;
self.physicsBody.dynamic = YES;
self.physicsBody.friction = 1;
}
return self;
}
The main character is sliding because you are moving the slab by changing its position over time. Imagine you are standing on top of a train that is moving, friction will cause you to move along in the direction of the train. However, if the train magically moved from point A to point B by disappearing and reappearing 1 cm at a time, you will remain at point A. That is what's happening to your main character.
To correct this issue, you will need to move the slabs using physics. Here is an example of how to do that:
slab.physicsBody.affectedByGravity = NO;
slab.physicsBody.dynamic = YES;
// These will help prevent the slab from twisting due to the weight of the main character
slab.physicsBody.angularDamping = 1.0;
slab.physicsBody.mass = 1e6;
// Air resistance will slow slab's movement, set the damping to 0
slab.physicsBody.linearDamping = 0;
Define a set of SKActions that will move the slabs left and right. Adjust the speed and duration variables to achieve the desired motion.
SKAction *moveLeft = [SKAction customActionWithDuration:0 actionBlock:^(SKNode *node, CGFloat elapsedTime){
node.physicsBody.velocity = CGVectorMake(-speed, 0);
}];
SKAction *moveRight = [SKAction customActionWithDuration:0 actionBlock:^(SKNode *node, CGFloat elapsedTime){
node.physicsBody.velocity = CGVectorMake(speed, 0);
}];
SKAction *wait = [SKAction waitForDuration:duration];
SKAction *action = [SKAction sequence:#[moveRight, wait, moveLeft, wait]];
[slab runAction:[SKAction repeatActionForever:action]];
You need to set the slab's physicsBody to be dynamic.
slab.physicsBody.dynamic = YES;
slab.physicsBody.affectedByGravity = NO;
A non-dynamic physicsBody is rendered static and ignores any forces and impulses applied on it. Look up the property in the documentation here.
I'm making a game and i'm currently stumped on how to have a method run for a certain amount of time, turn off, then to have another method start running. Currently i have:
[self spawn];
when the scene sets up. Here is the spawn method:
-(void)spawn {
int xMin = 0;
int xMax = 460;
CGPoint startPoint = CGPointMake(xMin + arc4random_uniform(xMax - xMin),320);
[self performSelector:#selector(spawn) withObject:nil afterDelay:.20];
SKSpriteNode *blue = [SKSpriteNode spriteNodeWithImageNamed:#"whitecircle"];
blue.position = CGPointMake(startPoint.x,startPoint.y);
blue.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:21.5];
blue.physicsBody.usesPreciseCollisionDetection = NO;
blue.physicsBody.categoryBitMask = gainCategory;
blue.physicsBody.contactTestBitMask = playerCategory;
blue.physicsBody.dynamic = NO;
[self addChild:blue];
SKAction *runBlue = [SKAction moveToY:0 duration:1.5];
SKAction *remove = [SKAction removeFromParent];
[blue runAction:[SKAction sequence:#[runBlue,remove]]];
}
What i want to know is how can i make this method run for a certain amount of time, then how can i start another method after this one finishes. Thanks
I think this is what you are looking to do :
// set duration to seconds you want to wait between spawns
float duration = 1;
SKAction *actionWait = [SKAction waitForDuration:duration];
SKAction *actionSpawn = [SKAction runBlock:^(void)
{
[self spawn];
}];
SKAction *actionSequence = [SKAction sequence:#[actionWait, actionSpawn]];
SKAction *actionRepeat = [SKAction repeatActionForever:actionSequence];
[self runAction:actionRepeat];
It's worthwhile to give the Class Reference for SKAction a good reading at least once, so you know it's capabilities.
SKAction Class Reference
I am trying to move a Sprite ("Spaceship") in a circle, where the click was performed.
Here is the code that is triggered when a click is performed:
- (void)newSpaceShipAt: (CGPoint)location
{
SKSpriteNode *hull = [[SKSpriteNode alloc]initWithImageNamed:#"Spaceship"];
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddArc(path, NULL, location.x, location.y, self.size.width / 5.0, 0, 360, YES);
SKAction *move = [SKAction sequence:#[
[SKAction followPath:path duration:1],
]];
[hull runAction:[SKAction repeatActionForever:move]];
hull.name = #"Spaceship";
hull.scale = 0.5;
hull.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:MAX(hull.size.width / 2.0, hull.size.height / 2.0)];
hull.physicsBody.dynamic = NO;
[self addChild:hull];
}
I was the Spaceship to move in the circle indefinitely about the point "location", which is where the click was performed. However, what happens is that after one successful revolution, the spaceship moves "location" relative from the click. For example, if the click was performed on (50, 100) then after one revolution it will move to (100, 200), relative to the scene, and it will carry on.
What can I do to fix this problem? Or if I my approach is totally wrong, what would be a better one?
Also, what is the best way to draw a trail (a simple line), to the movement of the sprite?
From the documentation of followPath:duration:...
Discussion
Calling this method is equivalent to calling the
followPath:asOffset:orientToPath:duration: method, passing in YES to
both the offset and orient parameters.
The behaviour of this method will tell the hull to orient to the path and also tell it to use the path as an offset. i.e. it will move 50,100 each time it runs.
Use this method instead...
[SKAction followPath:path asOffset:NO orientToPath:YES duration:1.0];
That should solve your problem.
Also, get rid of that sequence.
Do this...
SKAction *moveAction = [SKAction followPath:path asOffset:NO orientToPath:YES duration:1.0];
[hull runAction:[SKAction repeatActionForever:moveAction]];
I am simply trying to add a sprite "light" on top of "spaceship". So as you can see below that I added light as the child of the spaceship, however, the light is appearing below the spaceship, as seen in the picture below. Can anyone tell me why this is happening, and how can I fix it?
- (void)newSpaceshipAtLocation:(CGPoint)location{
SKSpriteNode *hull = [[SKSpriteNode alloc]initWithImageNamed:#"Spaceship"];
hull.position = location;
hull.name = #"Spaceship";
hull.scale = 0.5;
SKSpriteNode *light = [self lights];
light.position = CGPointMake(hull.size.width / 5.0, hull.size.height/5.0);
[hull addChild:light];
[self addChild:hull];
}
- (SKSpriteNode *)lights{
SKSpriteNode *light = [[SKSpriteNode alloc]initWithColor:[NSColor yellowColor] size:CGSizeMake(50.0, 50.0)];
SKAction *blink = [SKAction sequence:#[
[SKAction fadeOutWithDuration:0.5],
[SKAction fadeInWithDuration:0.5],
]];
[light runAction:[SKAction repeatActionForever:blink]];
light.name = #"light";
return light;
}
Try setting the zPosition property on light. If you set it to anything higher than the hull sprite, it should work.