题
我一直试图让我的绅士移动,但它不会出现比赛!我使用Cocos2D v2.1 beta 2和ios6这是我一直在使用的代码来移动我的精灵,它有什么不对吗?
#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]];
}
.
我可以看到sprite,但无论我不动,都不会移动。谢谢。
解决方案
看起来你需要改变
CCSprite* faceeater = [CCSprite spriteWithFile:@"fe.png"];
.
faceeater = [CCSprite spriteWithFile:@"fe.png"];
.
看起来你有两个不同的 ccsprite * enteater,一个in init, 并且在您的实施中定义了一个。 在这里:
@implementation HelloWorldLayer
{
CCSprite* faceeater ;
}
. 其他提示
是运行的nextframe方法?检查断点。
ccctouched运行?检查断点。
是否有任何其他代码设置精灵的位置?您是否可能每帧运行CCMOVEXX方法?
注意:如果在NextFrame中手动更新位置并同时运行CCMoveto操作,则结果可能是不可预测的。
不隶属于 StackOverflow