PDA

View Full Version : [AS3] Enter Frame not working


braican
04-13-2010, 10:56 PM
I'm trying to make a basic vehicle game (a zamboni game to be specific) with a button that makes the zamboni visible on the stage. The event listener for the button works fine, and makes the zamboni visible, but the stage is not picking up on the keyboard events, used to power the zamboni . the zamboni is a class that extends MovieClip, and the keyboard events are in the zamboni class, with the constructor calling the keyboard function on ENTER_FRAME.

Basically, everything works, but the user has to click on the stage somewhere to access the keyboard events in the class

I have this code for the zamboni:

package {

import flash.display.MovieClip;
import flash.display.Stage;
import flash.events.Event;
import assets.utils.KeyObject;
import flash.ui.Keyboard;

public class zamboni extends MovieClip {

private var key:KeyObject;
public var speed:Number=0.15;
private var rotateSpeed:Number=2;
private var vx:Number=0;
private var vy:Number=0;
private var friction:Number=0.85;

public function zamboni():void {
key=new KeyObject(stage);
addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
}

public function loop(e:Event):void {

if (key.isDown(Keyboard.UP)) {
vy+=Math.sin(degreesToRadians(rotation))*speed;
vx+=Math.cos(degreesToRadians(rotation))*speed;
} else {
vy*=friction;
vx*=friction;
}

if (key.isDown(Keyboard.RIGHT)) {
rotation+=rotateSpeed;
} else if (key.isDown(Keyboard.LEFT)) {
rotation-=rotateSpeed;

}
y+=vy;
x+=vx;

if (x>stage.stageWidth - 20) {
x=stage.stageWidth - 20;
} else if (x < 20) {
x=20;

}
if (y>stage.stageHeight - 20) {
y=stage.stageHeight - 20;
} else if (y < 100) {
y=100;
}
}

public function degreesToRadians(degrees:Number):Number {
return degrees * Math.PI / 180;
}
}
}

there's no additional code in the .fla, as the zamboni is an instance of the class on the timeline.

ASWC
04-13-2010, 10:58 PM
[ut]