2013-02-21 87 views
0

我遇到了一些cocos2d代码的问题, 它在模拟器上运行正常(这意味着在我触摸和滚动时精灵正在移动)但它并没有对我的iPod 工作ccTouchesMoved被称为两个,但如果我做了“gradA.position = CCP(0,gradA.position.y精灵在模拟器只能移动Cocos2d iphone:改变CCSprite.position在模拟器上工作,但不在设备上

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; 
} 

BTW - d);“ 除了ccTouchesMoved之外,它可以在设备和模拟器上工作。

这可能是一个愚蠢的错误(我希望它)在我身边,因为这是我的第一个项目。

+0

你在哪里更新_prevTouch?设置一个断点并检查d的值,可能为0. – LearnCocos2D 2013-02-21 09:54:18

+0

@ LearnCocos2D _prevTouch已在ccTouchesMoved方法中更新,对不起,我切断了一些代码以保持简短。 – jrmgx 2013-02-21 12:21:45

回答

0

答案很简单,如果你的设备和仿真器之间有问题,你应该检查(以及作为文件名的情况下) NSLog在设备上非常慢,太慢了,它阻止设备呈现精灵运动。

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

避免在设备上使用NSLog。

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; 
相关问题