Crazycriss
Crazycriss

Reputation: 315

Moving Sprite Up and Down Constantly Cocos2d 3.0

Im trying to move my sprite up and down constantly

This is the code I have but the app keeps crashing when it gets to this point

// Add a sprite
_player = [CCSprite spriteWithImageNamed:@"player.png"];
_player.position  = ccp(self.contentSize.width/30,self.contentSize.height/2);
_player.physicsBody = [CCPhysicsBody bodyWithRect:(CGRect){CGPointZero, _player.contentSize} cornerRadius:0]; // 1
_player.physicsBody.collisionGroup = @"playerGroup"; //
CCActionProgressFromTo *upanddown = [CCActionProgressFromTo actionWithDuration:1.0 from:.8f to:.1f];
[_player runAction:[CCActionRepeatForever actionWithAction:upanddown]];
[_physicsWorld addChild:_player];

Thanks for any advice :D

Upvotes: 0

Views: 1613

Answers (1)

Allen S
Allen S

Reputation: 1042

If you are trying to move it up and down, try either:

[CCActionMoveBy actionWithDuration:1.0f position:ccp(moveX, moveY)];

Or

[CCActionMoveTo actionWithDuration:1.0f position:ccp(moveX, moveY)];

Move-by will move it by an offset whereas move-to will move it to a specific location. For example to move it up and down using the move-by offset you could do:

CCActionMoveBy* moveUp = [CCActionMoveBy actionWithDuration:1.0f position:ccp(0.0f, 100.0f)];
CCActionMoveBy* moveDown = [CCActionMoveBy actionWithDuration:1.0f position:ccp(0.0f, -100.0f)];
CCActionSequence* upAndDown = [CCActionSequence actions:moveUp, moveDown, nil];

[_player runAction:[CCActionRepeatForever actionWithAction:upAndDown]];

Upvotes: 3

Related Questions