PDA

View Full Version : [AS3] Arrow key frustration


Keenan
04-23-2009, 10:59 AM
I'm moving a game camera around via the arrow keys. Everything works fine, expect for the fact that if I strike the UP and LEFT keys simultaneously, the camera gets locked up and keeps scrolling diagonally north-west. I can stop it from scrolling that way by pressing up or left arrow keys. The camera never does this with any other combination of key presses.

What is going on here?

// __________________________________________________ ___________________________ keyPress
private function keyPress(event:TimerEvent):void {

_elapsed = getTimer() - _time;
_time = getTimer();
var delta:Number = _elapsed / 1000;

// KEY DOWN
if(_key.isDown(Keyboard.UP)) {
_upPress = true;
this.move();
}

if(_key.isDown(Keyboard.DOWN)) {
_dnPress = true;
this.move();
}

if(_key.isDown(Keyboard.RIGHT)) {
_rtPress = true;
this.move();
}

if(_key.isDown(Keyboard.LEFT)) {
_ltPress = true;
this.move();
}

// KEY UP
if(_key.isUp(Keyboard.UP)) {
_upPress = false;
}

if(_key.isUp(Keyboard.DOWN)) {
_dnPress = false;
}

if(_key.isUp(Keyboard.RIGHT)) {
_rtPress = false;
}

if(_key.isUp(Keyboard.LEFT)) {
_ltPress = false;
}

this.update(delta);
}

// __________________________________________________ ___________________________ move
public function move():void {

_vel += _accel;
if(_vel > _maxVel) { _vel = _maxVel; }

_isMovUp = false;
_isMovDn = false;
_isMovRt = false;
_isMovLt = false;

if(_upPress) {
_isMovUp = true;
}
if(_dnPress) {
_isMovDn = true;
}
if(_rtPress) {
_isMovRt = true;
}
if(_ltPress) {
_isMovLt = true;
}
}

// __________________________________________________ ___________________________ update
public function update(delta:Number):void {

if(!_upPress && !_dnPress && !_rtPress && !_ltPress) {

_vel *= _fric;
}

var movAmt:Number = _vel * delta;
if(movAmt < .1) { movAmt = 0; }

if(movAmt <= 0) {
_isMovUp = false;
_isMovDn = false;
_isMovRt = false;
_isMovLt = false;
}

if(_isMovUp) {
this.motionUp(movAmt);
}

if(_isMovDn) {
this.motionDn(movAmt);
}

if(_isMovRt) {
this.motionRt(movAmt);
}

if(_isMovLt) {
this.motionLt(movAmt);
}
}