【问题标题】:Cocos2d iphone: changing CCSprite.position works on simulator but not on deviceCocos2d iphone:更改 CCSprite.position 在模拟器上有效,但在设备上无效
【发布时间】:2013-02-21 08:23:52
【问题描述】:

我遇到了一些 cocos2d 代码的问题, 它在模拟器上运行良好(意味着在这种情况下,当我触摸和滚动时精灵正在移动)但它在我的 ipod 上不起作用 两者都调用了 ccTouchesMoved,但精灵只在模拟器中移动

CCSprite *gradA = [CCSprite spriteWithFile:@"grad.png"];
gradA.anchorPoint = ccp(0, 0);
gradA.position = ccp(0, 0);

[...]

-(void) ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{   
    //NSLog(@"ccTouchesMoved called");

    UITouch *touch   = [touches anyObject];
    CGPoint location = [touch locationInView: [touch view]];
    location = [[CCDirector sharedDirector] convertToGL:location];

    int d = (_prevTouch.y - location.y);
    // This code should make the sprite moving
    // And it does on the simulator but not on my ipod
    gradA.position = ccp(PARALAX_X, gradA.position.y - d);

    _prevTouch = location;
}

-(void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch   = [touches anyObject];
    CGPoint location = [touch locationInView: [touch view]];
    location = [[CCDirector sharedDirector] convertToGL:location];

    _firstTouch = location;
}

-(void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{   
    UITouch *touch   = [touches anyObject];
    CGPoint location = [touch locationInView: [touch view]];
    location = [[CCDirector sharedDirector] convertToGL:location];

    _lastTouch = location;
}

顺便说一句,如果我执行“gradA.position = ccp(0, gradA.position.y - d);” 在 ccTouchesMoved 以外的任何其他方法中,它都可以在设备和模拟器上运行。

这对我来说可能是一个愚蠢的错误(我希望如此),因为这是我的第一个项目。

【问题讨论】:

  • 您在哪里更新_prevTouch?设置断点,查看d的值,应该是0。
  • @LearnCocos2D _prevTouch 已在 ccTouchesMoved 方法中更新,很抱歉我剪掉了一些代码以保持简短。

标签: cocos2d-iphone ios-simulator ccsprite


【解决方案1】:

这是我目前移动精灵的方式(在 ccTouchesMoved 中):

    UITouch *touch = [touches anyObject];

    CGPoint touchLocation = [self convertTouchToNodeSpace:touch];

    CGPoint oldTouchLocation = [touch previousLocationInView:touch.view];
    oldTouchLocation = [[CCDirector sharedDirector] convertToGL:oldTouchLocation];
    oldTouchLocation = [self convertToNodeSpace:oldTouchLocation];

    CGPoint translation = ccpSub(touchLocation, oldTouchLocation);

    CGPoint newPos = ccpAdd(currentFragment.position, translation);
    currentFragment.position = newPos;

【讨论】:

    【解决方案2】:

    答案很简单,如果您在设备和模拟器之间遇到问题,您应该检查一下(以及文件名大小写) NSLog 在设备上非常慢,太慢了以至于它阻止了设备渲染精灵的运动。

    我在 ccTouchesMoved 方法的代码中有两个 NSLog, 当我删除它们时,它就像一个魅力。

    避免在设备上使用 NSLog。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-12-21
      • 2017-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-25
      相关资源
      最近更新 更多