2013-02-21 1 views
0

일부 cocos2d 코드에 문제가 발생했습니다. 시뮬레이터에서 잘 실행됩니다 (이 경우 터치 및 스크롤 할 때 스프라이트가 움직이는 경우를 의미 함) 하지만 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가 아닌 다른 방법으로 장치 및 시뮬레이터에서 작동합니다.

내 첫 번째 프로젝트이기 때문에 내 측면에서 바보 같은 실수가 될 수 있습니다. 나는 (ccTouchesMoved에서) 지금 내 스프라이트를 이동하고 방법은 다음과

+0

어디에서 _prevTouch를 업데이트합니까? 중단 점을 설정하고 d의 값을 확인하십시오. 아마도 0입니다. – LearnCocos2D

+0

@ LearnCocos2D _prevTouch가 ccTouchesMoved 메소드에서 업데이트되었습니다. 코드를 짧게 유지하는 것이 유감입니다. – jrmgx

답변

0

대답은 간단하고, 당신이 장치와 시뮬레이터 사이에 문제가있는 경우, 당신은뿐만 아니라 (즉를 확인해야합니다 파일 이름의 경우) NSLog는 장치에서 매우 느리므로 느리게 장치가 스프라이트 이동을 렌더링하지 못하게합니다.

나는 코드를 제거했을 때 의 ccTouchesMoved 메소드에 내 코드에 두 개의 NSLog가 있습니다.

기기의 NSLog 사용을 피하십시오.

1

은 다음과 같습니다

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;