2013-02-21 135 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; 
相關問題