【问题标题】:Cocos2d sprite wont move?Cocos2d精灵不会移动?
【发布时间】:2012-09-29 18:28:23
【问题描述】:

我一直试图让我的精神动起来,但它不会让步!我使用 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]];
 }

我可以看到精灵,但无论我做什么它都不会移动。谢谢。

【问题讨论】:

    标签: cocos2d-iphone ios6


    【解决方案1】:

    看来你需要改变

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

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

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

    @implementation HelloWorldLayer
    {
    CCSprite* faceeater ;
    }
    

    【讨论】:

    • 我爱你...不敢相信我没有注意到!
    【解决方案2】:

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

    ccTouchEnded 是否运行?检查断点。

    还有其他代码设置精灵的位置吗?您是否可能每帧都运行一个 CCMoveXX 方法?

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

    【讨论】:

    • 试过了,touches 结束了,touches 开始了,从来没有被调用过,我该怎么办?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-18
    • 1970-01-01
    相关资源
    最近更新 更多