0

我遇到了一些 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 以外的任何其他方法中,它都可以在设备和模拟器上运行。

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

4

2 回答 2

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;
于 2013-02-21T12:25:37.657 回答
0

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

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

避免在设备上使用 NSLog。

于 2013-02-21T12:36:19.640 回答