控制精灵套件中的最大速度

时间:2013-12-12 16:47:51

标签: ios objective-c sprite-kit

我正在尝试控制游戏中角色的最高速度。 当我移动他时,我使用它:

-(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");
}

但问题是速度不受限制,当我这样做两到三次时,角色变得非常快。我尝试了很多属性(速度,角速度等),我没有找到任何令人满意的东西。 有人知道如何设置速度限制或任何“技巧”来控制角色的最大速度吗?

3 个答案:

答案 0 :(得分:5)

对我来说最好的方法是以下因为它可以确保对角线中的矢量速度不快:

    /* 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)
    }

答案 1 :(得分:3)

您可以使用

- (void)didSimulatePhysics

委托SKScene的方法检查节点物理体的速度,并将其设置为您想到的最大速度。

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

您可能想要为速度的Y方向添加另一个检查。

答案 2 :(得分:2)

这很有效。

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

感谢@Andrew&amp;的回答。 @ LearnCocos2D&amp;的评论@Andy