0
votes

I've been trying to get my sprit to move but it wont budge! Im using cocos2d v2.1 beta 2 and ios6 this is the code i've been using to move my sprite, whats wrong with it?

#import "CCTouchDispatcher.h"
 CCSprite *faceeater;

 -(id) init
 {

if( (self=[super init]) ) {
}


CCSprite* faceeater = [CCSprite spriteWithFile:@"fe.png"];
faceeater.position =  ccp( 200, 300 );
[self addChild:faceeater];
[self schedule:@selector(nextFrame:)];

[[CCDirector sharedDirector] touchDispatcher];

self.isTouchEnabled = YES;

  return self;

 }



 - (void) nextFrame:(ccTime)dt {
faceeater.position = ccp( faceeater.position.x + 100*dt, faceeater.position.y );
if (faceeater.position.x > 480+32) {
    faceeater.position = ccp( -32, faceeater.position.y );
}
}
- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
return YES;
}

- (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event {
CGPoint location = [self convertTouchToNodeSpace: touch];

[faceeater stopAllActions];
[faceeater runAction: [CCMoveTo actionWithDuration:1 position:location]];
 }

I can see the sprite but no matter what i don it doesn't move. Thanks.

2

2 Answers

1
votes

it looks like you need to change

CCSprite* faceeater = [CCSprite spriteWithFile:@"fe.png"];

to

faceeater = [CCSprite spriteWithFile:@"fe.png"];

It looks like you have two different CCSprite* faceeater here, a local one in init, and one defined in your implementation. one here:

@implementation HelloWorldLayer
{
CCSprite* faceeater ;
}
0
votes

Is the nextFrame method running? Check with a breakpoint.

Does ccTouchEnded run? Check with a breakpoint.

Does any other code set the position of the sprite? Are you perhaps running a CCMoveXX method every frame?

Note: if you update the position manually in nextFrame AND run a CCMoveTo action at the same time, the results can be unpredictable.