2014-07-22 1 views
1

다음 코드로 플레이어를 움직이게됩니다. 화면의 왼쪽과 오른쪽. 화면의 중앙에는 플레이어가 터치 할 때 플레이어가 점프하게 만드는 "점핑 유물"이 있습니다.내 노드 점프하기

내 플레이어가 실행 중이고 점프가 시작되면 문제가 발생합니다. 플레이어는 자신의 (수평) x.velocity을 잃습니다. 수직선을 따라 점프 만하고, 내가 필요한 것은 그의 x.velocity을 보존하는 것입니다.

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch *touch = [touches anyObject]; 
    CGPoint touchLocation = [touch locationInNode:self.scene]; 
    if(touchLocation.x >self.size.width/2.0){ 

     player.physicsBody.velocity=CGVectorMake(100, player.physicsBody.velocity.dy); 

    }else{ 

     player.physicsBody.velocity=CGVectorMake(-100, player.physicsBody.velocity.dy); 

    } 
} 


- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch *touch = [touches anyObject]; 
    CGPoint touchLocation = [touch locationInNode:self.scene]; 
    if(touchLocation.x >self.size.width/2.0){ 

     player.physicsBody.velocity=CGVectorMake(5, player.physicsBody.velocity.dy); 

    }else{ 

     player.physicsBody.velocity=CGVectorMake(-5, player.physicsBody.velocity.dy); 
    } 
} 

- (void)didBeginContact:(SKPhysicsContact *)contact 
{ 
    SKPhysicsBody *firstBody, *secondBody; 
    if (contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask) 
    { 
     firstBody = contact.bodyA; 
     secondBody = contact.bodyB; 
    } 
    else 
    { 
     firstBody = contact.bodyB; 
     secondBody = contact.bodyA; 
    } 

    if((firstBody.categoryBitMask == playerCategory && secondBody.categoryBitMask == jumpingCategory) || 
     (firstBody.categoryBitMask == jumpingCategory && secondBody.categoryBitMask == playerCategory)) 
    { 
     NSLog(@"player hits jumping"); 

     player.physicsBody.velocity = CGVectorMake(player.physicsBody.velocity.dx, 390.0f); 

    } 
} 

답변

0

속도를 설정하지 마십시오. 대신에 임펄스를 적용하십시오.

[player.physicsBody applyImpulse:CGVectorMake(0, 1)]; 

이렇게하면 현재 가로 속도 값이 유지됩니다.

+0

그것은 작동합니다, Reecon. 내 잘못. 감사! – sone3d

+0

반가워요. 내 대답을 받아 들일 수 있니? :) – RaffAl

+0

"투표하기"를 원하십니까? "투표까지 15 평판이 필요합니다." 나는 그것을 시도했다. 이 평판 포인트를 얻었을 때 upvote로 돌아갈거야;) – sone3d

관련 문제