control max speed in sprite kit

时间秒杀一切 提交于 2020-01-04 02:46:12

问题


I'm trying to control the max speed of my character in my game. When I move him I use this:

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

But the problem is the speed is not limited, and when i do this two or three times the character goes very fast. I tried with a lot of properties (velocity, angular speed, etc) and i didn't found anything satisfactory. Does anybody know how to set a speed limit or any "trick" to control the max speed of a character?


回答1:


This is working good.

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

Thanks to the answer by @Andrew & the comments by @LearnCocos2D & @Andy




回答2:


The best approach for me is the following because it makes sure the vector speed is not faster in diagonals:

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



回答3:


You could use the

- (void)didSimulatePhysics

delegate method of SKScene to check the velocity of the physics body of the node and set it to the max velocity that you have in mind.

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

You might want to add another check for the Y direction of the velocity.



来源:https://stackoverflow.com/questions/20549271/control-max-speed-in-sprite-kit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!