Question

Okay Im relatively new to nape and Im in the process of making a game, I've made a Body called platform of type KINEMATIC, and I simply want to move it back a forth in a certain range on the stage. Can somebody please see where im going wrong , thanks.

private function enterFrameHandler(ev:Event):void
{
if (movingPlatform.position.x <= 150 )
            {
                movingPlatform.position.x +=  10;
            }
            if (movingPlatform.position.x >= 260)
            {
                movingPlatform.velocity.x -=  10;
            }
}
Was it helpful?

Solution

First of in one of the if blocks you are incrementing position.x by 10 in the other one you are decrementing velocity.x by 10. I guess you meant position.x in both.

Secondly, imagine movingPlatform.position.x is 150 and your enterFrameHandler runs once. movingPlatform.position.x will become 160 and on the next time enterFrameHandler is called none of the if blocks will execute since 160 is neither less than or equal to 150 or greater than or equal to 260.

You can use the velocity to indicate the side its moving and invert it once you go beyond an edge, something like :

// assuming velocity is (1,0)
private function enterFrameHandler(ev:Event):void {
    if (movingPlatform.position.x <= 150 || movingPlatform.position.x >= 260) {
        movingPlatform.velocity.x = -movingPlatform.velocity.x;
    }

    movingPlatform.position.x += movingPlatform.velocity.x;
}

Obviously this might cause problems if the object is already at let's say x=100, it will just keep inverting it's velocity, so either make sure you place it between 150-260 or add additional checks to prevent it from inverting it's direction more than once.

This might be a better way of doing it :

// assuming velocity is (1,0)
private function enterFrameHandler(ev:Event):void {
    if (movingPlatform.position.x <= 150) {
        movingPlatform.velocity.x = 1;
    } else if (movingPlatform.position.x >= 260) {
        movingPlatform.velocity.x = -1;
    }

    movingPlatform.position.x += movingPlatform.velocity.x;
}

OTHER TIPS

In general:

Kinematic bodies are supposed to be moved solely with velocity, if you change their position directly then they are not really moving as much as they are 'teleporting' and as far as the physics is concerned their velocity is still exactly 0 so things like collisions and friction will not work as you might expect.

If you want to still work with positions instead of velocities, then there's the method setVelocityFromTarget on the Body class which is designed for kinematics:

body.setVelocityFromTarget(targetPosition, targetRotation, deltaTime);

where deltaTime is the time step you're about to use in the following call to space.step();

All this is really doing is setting an appropriate velocity and angularVel based on the current position/rotation, the target position/rotation and the amount of time it should take to get there.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top