2013-12-12 5 views
3

게임에서 내 캐릭터의 최대 속도를 제어하려고합니다.스프라이트 키트의 최대 속도 제어

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 


UITouch *touch = [touches anyObject]; 

CGPoint positionInScene = [touch locationInNode:self]; 
SKSpriteNode *touchedNode = (SKSpriteNode *)[self nodeAtPoint:positionInScene]; 
CGPoint posicionHero = [self childNodeWithName:@"hero"].position; 
SKSpriteNode *touchHero = (SKSpriteNode *)[self nodeAtPoint:posicionHero]; 
if((touchedNode !=touchHero) //jump forward 
    && (positionInScene.x > [self childNodeWithName:@"Hero"].position.x) 
    && (positionInScene.y > [self childNodeWithName:@"Hero"].position.y) 
    ) 
{ 
    [[self childNodeWithName:@"Hero"].physicsBody applyImpulse:CGVectorMake(5,0)]; 
    [[self childNodeWithName:@"Hero"].physicsBody applyImpulse:CGVectorMake(0, 10)]; 
    NSLog(@"jump forward done"); 
} 

그러나 문제는 속도가 제한되지 않고, 그리고 내가이 두 개 또는 세 번 할 때 문자는 매우 빠르게 진행 : 내가 그를 이동하면 나는이 사용합니다. 나는 많은 특성 (속도, 각속도 등)을 가지고 시험해 보았고 만족스런 것을 찾지 못했습니다. 캐릭터의 최대 속도를 제어하기 위해 속도 제한 또는 "트릭"을 설정하는 방법을 아는 사람이 있습니까?

답변

2

이것은 정상적으로 작동하고 있습니다. 이 벡터 속도가 대각선에없는 빠른 확인합니다 때문에 @ LearnCocos2D & @Andy

3

당신은 노드의 물리 본체의 속도를 확인하고 당신이 생각하고있는 최대 속도로 설정하는 SKScene의

- (void)didSimulatePhysics 

위임 방법을 사용할 수 있습니다.

- (void)didSimulatePhysics { 
    if (node.physicsBody.velocity.x < MAX_SPEED_X) { 
    node.physicsBody.velocity = CGVectorMake(MAX_SPEED_X, MAX_SPEED_Y); 
} 
} 

속도의 Y 방향에 대한 다른 검사를 추가 할 수 있습니다.

+10

팁 : 업데이트 또는 didEvaluateActions에서 더 잘 수행하십시오. ** 물리 신체 위치가 업데이트되기 전에 ** 속도가 제한됩니다 ** – LearnCocos2D

+2

velocity.x> MAX_SPEED_X인지 확인하는 것이 좋습니다. 그렇지 않으면 항상 얻을 수 있습니다. 최대 속도 – Andy

4

나를 위해 가장 좋은 방법으로 앤드류 & 코멘트로 대답

- (void)didEvaluateActions 
{ 
    CGFloat maxSpeed = 600.0f; 

    if (self.heroShip.physicsBody.velocity.dx > maxSpeed) { 
     self.heroShip.physicsBody.velocity = CGVectorMake(maxSpeed, self.heroShip.physicsBody.velocity.dy); 
    } else if (self.heroShip.physicsBody.velocity.dx < -maxSpeed) { 
     self.heroShip.physicsBody.velocity = CGVectorMake(-maxSpeed, self.heroShip.physicsBody.velocity.dy); 
    } 

    if (self.heroShip.physicsBody.velocity.dy > maxSpeed) { 
     self.heroShip.physicsBody.velocity = CGVectorMake(self.heroShip.physicsBody.velocity.dx, maxSpeed); 
    } else if (self.heroShip.physicsBody.velocity.dy < -maxSpeed) { 
     self.heroShip.physicsBody.velocity = CGVectorMake(self.heroShip.physicsBody.velocity.dx, -maxSpeed); 
    } 
} 

감사는 다음과 같습니다

/* Clamp Velocity */ 

    // Set the initial parameters 
    let maxVelocity = CGFloat(100) 
    let deltaX = (self.physicsBody?.velocity.dx)! 
    let deltaY = (self.physicsBody?.velocity.dy)! 


    // Get the actual length of the vector with Pythagorean Theorem 
    let deltaZ = sqrt(pow(deltaX, 2) + pow(deltaY, 2)) 


    // If the vector length is higher then the max velocity 
    if deltaZ > maxVelocity { 

     // Get the proportions for X and Y axis compared to the Z of the Pythagorean Theorem 
     let xProportion = deltaX/deltaZ 
     let yProportion = deltaY/deltaZ 

     // Get a new X and Y length in proportion to the max velocity 
     let correctedDeltaX = xProportion * maxVelocity 
     let correctedDeltaY = yProportion * maxVelocity 

     // Assign the new velocity to the Node 
     self.physicsBody?.velocity = CGVector(dx: correctedDeltaX, dy: correctedDeltaY) 
    } 
+0

성능을 위해'sqrt'에 대한 호출을 건너 뛰고'(maxVelocity * maxVelocity)'와 비교하십시오. – bcattle

관련 문제