hey guys!
I got a wierd problem about ApplyForce, i create a ball(represent player) in the b2world. and use keyboard to control its movements through ENTER_FRAME event listener which is the update(e:Event) function to handle the event. when player presses Arrow Key Left, the ball calls ApplyForce. The problem is when i stopped pressed Arrow Key Left, the ball stops calling ApplyForce, but the ball is still accelerating! I don't get it. I traced the ball's LinearVelocity().x. It s something like this. Where does it go wrong?
TRACE INFO:
0
0
0
Apply force to right.
0.0019894367886486917
Apply force to right.
0.005968310365946075
0.009947183943243457
0.013926057520540842
0.017904931097838226
0.02188380467513561
0.025862678252432995
0.02984155182973038
0.03382042540702776
0.037799298984325144
0.04177817256162253
0.04575704613891991
0.0497359197162173
0.05371479329351468
And here is some of my code:
I got a wierd problem about ApplyForce, i create a ball(represent player) in the b2world. and use keyboard to control its movements through ENTER_FRAME event listener which is the update(e:Event) function to handle the event. when player presses Arrow Key Left, the ball calls ApplyForce. The problem is when i stopped pressed Arrow Key Left, the ball stops calling ApplyForce, but the ball is still accelerating! I don't get it. I traced the ball's LinearVelocity().x. It s something like this. Where does it go wrong?
TRACE INFO:
0
0
0
Apply force to right.
0.0019894367886486917
Apply force to right.
0.005968310365946075
0.009947183943243457
0.013926057520540842
0.017904931097838226
0.02188380467513561
0.025862678252432995
0.02984155182973038
0.03382042540702776
0.037799298984325144
0.04177817256162253
0.04575704613891991
0.0497359197162173
0.05371479329351468
And here is some of my code:
Code: Select all
private function update(e:Event):void {
PhysicsValues.world.Step(1 / PhysicsValues.FRAME_RATE, 10, 10);
for each (var actor:Actor in _allActors) {
actor.updateNow();
}
trace(_player._body.GetLinearVelocity().x);
if (_action.leftPressed && _player._body.GetLinearVelocity().x > -0.1) {
trace("Apply force to left.");
_player._body.ApplyForce(new b2Vec2(-0.05, 0), _player._body.GetWorldCenter());
}
if (_action.rightPressed && _player._body.GetLinearVelocity().x < 0.1) {
trace("Apply force to right.");
_player._body.ApplyForce(new b2Vec2(0.05, 0), _player._body.GetWorldCenter());
}
CONFIG::DEBUG_DRAW {
PhysicsValues.world.DrawDebugData();
}
}
Code: Select all
package {
import flash.display.Stage;
import flash.events.KeyboardEvent;
public class Action {
public var spacePressed:Boolean = false;
public var leftPressed:Boolean = false;
public var rightPressed:Boolean = false;
public var upPressed:Boolean = false;
public var downPressed:Boolean = false;
public var num0Pressed:Boolean = false;
public var num1Pressed:Boolean = false;
public var num2Pressed:Boolean = false;
public var num3Pressed:Boolean = false;
public var num4Pressed:Boolean = false;
public var num5Pressed:Boolean = false;
public var num6Pressed:Boolean = false;
public var num7Pressed:Boolean = false;
public var num8Pressed:Boolean = false;
public var num9Pressed:Boolean = false;
public function Action() {
var stage:Stage = PlayerDemo.getStage();
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
}
private function onKeyUp(e:KeyboardEvent):void {
switch(e.keyCode) {
case 32: spacePressed = false; break;
case 37: leftPressed = false; break;
case 38: upPressed = false; break;
case 39: rightPressed = false; break;
case 40: downPressed = false; break;
case 48: num0Pressed = false; break;
case 49: num1Pressed = false; break;
case 50: num2Pressed = false; break;
case 51: num3Pressed = false; break;
case 52: num4Pressed = false; break;
case 53: num5Pressed = false; break;
case 54: num6Pressed = false; break;
case 55: num7Pressed = false; break;
case 56: num8Pressed = false; break;
case 57: num9Pressed = false; break;
}
}
private function onKeyDown(e:KeyboardEvent):void {
switch(e.keyCode) {
case 32: spacePressed = true; break;
case 37: leftPressed = true; break;
case 38: upPressed = true; break;
case 39: rightPressed = true; break;
case 40: downPressed = true; break;
case 48: num0Pressed = true; break;
case 49: num1Pressed = true; break;
case 50: num2Pressed = true; break;
case 51: num3Pressed = true; break;
case 52: num4Pressed = true; break;
case 53: num5Pressed = true; break;
case 54: num6Pressed = true; break;
case 55: num7Pressed = true; break;
case 56: num8Pressed = true; break;
case 57: num9Pressed = true; break;
}
}
}
}