0

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.

4

2 回答 2

1

看起来你需要改变

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

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

看起来您在这里有两个不同的 CCSprite* faceeater,一个在 init 中是本地的,一个在您的实现中定义。一个在这里:

@implementation HelloWorldLayer
{
CCSprite* faceeater ;
}
于 2012-09-30T10:52:14.247 回答
0

nextFrame 方法是否正在运行?用断点检查。

ccTouchEnded 是否运行?用断点检查。

是否有任何其他代码设置了精灵的位置?您是否可能每帧都运行 CCMoveXX 方法?

注意:如果您在 nextFrame 中手动更新位置并同时运行 CCMoveTo 操作,则结果可能无法预测。

于 2012-09-29T19:51:45.687 回答