Movement Keys not Working - actionscript-3

Nothing is happening when I press the arrow keys, but neither are there any errors: what's wrong with this? If I remove the key press testing it accelerates accordingly...
At this stage I am just trying to move a block around a screen in an inertial manner using the arrow keys. However, this is my first foray into AS3 so I may be going about it in completely the wrong manner.
Any help would be greatly appreciated.
Unit.AS:
package {
import flash.display.MovieClip;
import flash.events.*
import flash.ui.Keyboard
public class Unit extends MovieClip {
var velocityX:Number = 1;
var velocityY:Number = 1;
var accellerationX:Number = 1;
var accellerationY:Number = 1;
public function Unit(){
addEventListener("enterFrame", move);
}
private function move(e:Event){
accellerate()
this.x += velocityX;
this.y += velocityY;
}
private function accellerate(){
if (Key.isDown(Keyboard.UP)){
velocityY += accellerationY;
trace("Accellerating");
}
if (Key.isDown(Keyboard.DOWN)){
velocityY -= accellerationY;
trace("Accellerating");
}
if (Key.isDown(Keyboard.RIGHT)){
velocityX += accellerationX;
trace("Accellerating");
}
if (Key.isDown(Keyboard.LEFT)){
velocityX -= accellerationX;
trace("Accellerating");
}
}
}
}
Key.AS:
package
{
import flash.display.Stage;
import flash.events.Event;
import flash.events.KeyboardEvent;
public class Key {
private static var initialized:Boolean = false;
private static var keysDown:Object = new Object();
public static function initialize(stage:Stage) {
if (!initialized) {
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);
stage.addEventListener(Event.DEACTIVATE, clearKeys);
initialized = true;
}
}
public static function isDown(keyCode:uint):Boolean
{
return Boolean(keyCode in keysDown);
}
private static function keyPressed(event:KeyboardEvent):void {
keysDown[event.keyCode] = true;
}
private static function keyReleased(event:KeyboardEvent):void {
if (event.keyCode in keysDown) {
delete keysDown[event.keyCode];
}
}
private static function clearKeys(event:Event):void {
keysDown = new Object();
}
}
}

On your unit constructor function call the initialize(stage) static function.
Key.initialize(stage);

Related

Movement not working AS3

I'm currently working on a game, and am fairly new to AS3.
I'm stuck on the character movement: I was following a guide and ended up with the following as my code. When I test the game it just plays the character animation and I can't control it.
package {
import flash.display.Stage;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.utils.Proxy;
import flash.utils.flash_proxy;
dynamic public class KeyObject extends Proxy {
private static var stage:Stage;
private static var keysDown:Object;
public function KeyObject(stage:Stage) {
construct(stage);
}
public function construct(stage:Stage):void {
KeyObject.stage = stage;
keysDown = new Object();
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);
}
flash_proxy override function getProperty(name:*):* {
return (name in Keyboard) ? Keyboard[name] : -1;
}
public function isDown(keyCode:uint):Boolean {
return Boolean(keyCode in keysDown);
}
public function deconstruct():void {
stage.removeEventListener(KeyboardEvent.KEY_DOWN,keyPressed);
stage.removeEventListener(KeyboardEvent.KEY_UP, keyReleased);
keysDown = new Object();
KeyObject.stage = null;
}
private function keyPressed(evt:KeyboardEvent):void {
keysDown[evt.keyCode] = true;
}
private function keyReleased(evt:KeyboardEvent):void {
delete keysDown[evt.keyCode];
}
}
}
package {
import flash.display.Sprite
import flash.events.Event;
import KeyObject;
public class Main extends Sprite{
private var key:KeyObject;
public function Main() {
addEventListener(Event.ADDED_TO_STAGE,setupKeyObject);
}
function setupKeyObject(e:Event){
key = new KeyObject(stage);
stage.addEventListener(Event.ENTER_FRAME,movePlayer);
}
function movePlayer(e:Event){
if(key.isDown(key.LEFT)){
roy.x -= 5;
}
if(key.isDown(key.RIGHT)){
roy.x +=5;
}
if(roy.x<0){
roy.x = 0;
}
if(roy.x > (stage.stageWidth - player.width)){
roy.x = stage.stageWidth - player.width;
}
}
}
}

How do I get my movieclip character to move?

I've been trying for a few hours now and I cant get my little character to move with the keyboard.
I have ran a trace to make see if anything was happening and the position value does change but my character doesn't react to that position change.
I receive no errors. Both my Character and BrickBlock are movieclips and they have been imported for ActionScript.
If any other information is needed please let me know. Thank you! :)
My following code:
package {
import flash.events.Event
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
public class CharMove extends MovieClip {
var char1 :Character;
var block :BrickBlock;
public function CharMove()
{
char1 = new Character();
block = new BrickBlock();
//this.addEventListener(Event.ENTER_FRAME, collide)
stage.addEventListener(KeyboardEvent.KEY_DOWN, kDown);
}
/*function collide(e:Event):void
{
if(char.hitTestObject(block))
{
char.visible = !char.visible;
}
}*/
function kDown(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case Keyboard.LEFT:
char1.x -= 5;
trace(char1.x);
break;
case Keyboard.RIGHT:
char1.x +=5;
trace(char1.x);
break;
}
}
}
}
You might want to consider writing a static Input class.
package input {
import flash.display.Stage;
import flash.events.KeyboardEvent;
public class Input {
private static var keys:Array = new Array(255);
public static function setup(stage:Stage):void {
stage.addEventListener(KeyboardEvent.KEY_DOWN, KeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, KeyUp);
}
private static function keyDown(e:KeyboardEvent):void {
keys[e.keyCode] = true;
}
private static function keyUp(e:KeyboardEvent):void {
keys[e.keyCode] = false;
}
public static function isKeyDown(k:int):Boolean {
return keys[k];
}
public static const A:uint = 65;
public static const B:uint = 66;
public static const C:uint = 67;
// The rest of the keys...
}
}
To use it first call setup() which adds the listeners for KEY_DOWN and KEY_UP events.
They you can easily query keys and do relevant actions accordingly.
Input.setup(stage);
/...
if(Input.isKeyDown(Input.A)) {
char1.x -= 5;
}

Inherit MovieClip From Another Class

Hey I seem to be having some problems inheriting Movie-clip from a class, I'm fairly new to as3, I've had a look around and can't tell if I'm doing something fundamentally wrong or not.
Let me show you
So I have a class I want to use to move EVERYTHING but the player sprite. So I want everything but the player to extend it. (or so I'm assuming.)
So I declare my class
package code {
import flash.display.*;
import flash.events.*;
import flash.ui.*;
import code.Main;
public class everythingContainer extends MovieClip {
function brackets and so on...
(I'm just importing everything in an attempt to avoid errors)
I then have a class I want to inherit everythingContainer and Movieclip
package code {
import flash.display.*;
import flash.events.*;
import flash.ui.*;
import code.Main;
import code.everythingContainer;
public class Tree1 extends everythingContainer {
Yet when I run this I get the error:
Line 1 5000: The class 'code.Tree1' must subclass 'flash.display.MovieClip' since it is linked to a library symbol of that type.
Why am I getting this error?
Any help would be greatly appreciated!
I haven't got the full code to run yet so there may still be other obvious bugs laying about.
everythingContainer
Full code:
package code {
import flash.display.*;
import flash.events.*;
import flash.ui.*;
import code.Main;
public class everythingContainer extends MovieClip {
var speed: Number = 4;
var wpressed: Boolean = false;
var apressed: Boolean = false;
var spressed: Boolean = false;
var dpressed: Boolean = false;
var xprev:int = 0;
var yprev:int = 0;
public function everythingContainer() {
// constructor code
trace ('Container started');
if(stage) init();
else
this.addEventListener(Event.ADDED_TO_STAGE, init);
}
function init(eventInfo:Event = null):void
{
if(eventInfo != null)
{
this.removeEventListener(Event.ADDED_TO_STAGE, init);
trace ('Container init removed');
}
// constructor code
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPress);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyRelease);
this.addEventListener( Event.ENTER_FRAME, containerEveryFrame);
}
public function containerEveryFrame (Event): void {
if (stage.contains(Main.player)) {
xprev = this.x;
yprev = this.y;
checkPlayerMovement();
}
}
// check the set keypress variables
public function checkPlayerMovement () : void {
if (wpressed) {
this.y -= this.speed;
}
if (spressed) {
this.y += this.speed;
}
if (apressed){
this.x -= this.speed;
}
if (dpressed) {
this.x += this.speed;
}
}
//assign key presses to variables
public function onKeyPress (event:KeyboardEvent):void {
//up
if (event.keyCode == 87){
wpressed = true;
}
//down
if (event.keyCode == 83) {
spressed = true;
}
//left
if (event.keyCode == 65){
apressed = true;
}
//right
if (event.keyCode == 68) {
dpressed = true;
}
}
//reset key press variables
public function onKeyRelease (event:KeyboardEvent) : void {
//up
if (event.keyCode == 87){
wpressed = false;
}
//down
if (event.keyCode == 83) {
spressed = false;
}
//left
if (event.keyCode == 65){
apressed = false;
}
//right
if (event.keyCode == 68) {
dpressed = false;
}
}
}
}
Main (there's some other stuff going on in here, but at the minute I'm just trying to get the trees working with my other class.)
package code
{
import flash.display.*;
import flash.events.*;
import flash.ui.*;
import flash.geom.Rectangle;
import flashx.textLayout.container.ContainerController;
public class Main extends MovieClip
{
//public static var main
public static var player:PC;
//public static var firstenemy: WolfEnemy;
public static var MainContainer:everythingContainer;
public function Main()
{
// constructor code
trace('main started');
if (stage)
{
init();
}
else
{
this.addEventListener(Event.ADDED_TO_STAGE, init);
}
}
function init(eventInfo:Event = null):void
{
if (eventInfo != null)
{
this.removeEventListener(Event.ADDED_TO_STAGE, init);
trace('Main init removed');
}
MainContainer = new everythingContainer ;
MainContainer.x = stage.stageWidth / 2;
MainContainer.y = stage.stageHeight / 2;
stage.addChild(MainContainer);
player = new PC();
player.x = stage.stageWidth / 2;
player.y = stage.stageHeight / 2;
stage.addChild(player);
//firstenemy = new WolfEnemy();
//firstenemy.x = 100;
//firstenemy.y = 100;
//stage.addChild(firstenemy);
stage.addEventListener( Event.ENTER_FRAME, everyFrameMain);
}
// check if an enemy hits the player.
/*public function enemycollison(): void {
if(firstenemy.hitTestObject(player)){
trace ('hit enemy');
player.health--;
firstenemy.kill();
}
}*/
// manage events that need to haapen globally for every frame
public function everyFrameMain(Event):void
{
/*if (stage.contains(firstenemy)){
enemycollison();
} */
//root.scrollRect = new Rectangle(player.x - 400, player.y - 300, 800, 600);
}
// finish and close game
public function endgame():void
{
}
}
}
and finally my tree class
package code {
import flash.display.*;
import flash.events.*;
import flash.ui.*;
import code.Main;
import code.everythingContainer;
public class Tree1 extends everythingContainer {
public function Tree1()
{
// constructor code
trace ('Tree1 started');
if(stage) init();
else
this.addEventListener(Event.ADDED_TO_STAGE, init);
}
function init(eventInfo:Event = null):void
{
if(eventInfo != null)
{
this.removeEventListener(Event.ADDED_TO_STAGE, init);
trace ('Tree1 init removed');
}
this.addEventListener( Event.ENTER_FRAME, tree1EveryFrame);
}
public function tree1EveryFrame (Event): void {
playercollision();
}
public function playercollision(): void {
if(this.hitTestObject(Main.player)){
trace ('hit wall');
Main.player.x = Main.player.xprev;
Main.player.y = Main.player.yprev;
}
}
}
}
Probably due to linkage errors inside Fla-file.
RMB on library-item: Tree1
Export for ActionScript + Export in frame 1 + "class: code.Tree1"
File/Publish Settings/Actionscript Settings (the small wrench, right of Script-dropdown)
Source Path (add the linkage to where the compiler can find your package-folder), usually for me I crate a folder next to the fla file called src or something like that so the code-file would be found at "/MyProject/src/code/Tree1.as", in that case I add "./src/" inside Source path inside Advanced ActionScript 3.0 settings
Added an example project in Flash CS6 found at url:
https://user.iter.org/filesharing/?uid=927205f7-cdfe-4915-a175-bc87f64af444
that is available for ~40 days.
Project structure in that file:
"/MyProject/DeepInheritage.fla"
"/MyProject/src/code/Foobar.as"
"/MyProject/src/code/Tree1.as"
Foobar.as which extends MovieClip
Tree1 library item which extends Foobar
That should be the exact same thing that you described in your issue, meaning that there is nothing wrong with that approach, it is just a matter of finding what is wrong. Most likely that is due to errors inside FLA-file, but might be something else.
code files:
package code {
import flash.display.MovieClip;
public class Foobar extends MovieClip {
public function Foobar() {
trace("foobar ctor()");
}
}
}
package code {
import flash.display.MovieClip;
public class Tree1 extends Foobar {
public function Tree1() {
trace("Tree1 ctor()");
}
}
}

AS3 TypeError: Error #2007: Parameter hitTestObject must be non-null

TypeError: Error #2007: Parameter hitTestObject must be non-null.
I am working on a game right now and trying to get it so the player will collide with the ground (he already collides with table)
It says the problem is happening in playerOneCollisions
table and ground are just classes with the basic class code linked to movieclips,
they are extended movieclips (if that matters), and they have imported stage and movieclip (again, if that matters)
(the parts I think are important)
Player.as
public var main:Main;
public var table:Table;
public var player:Player;
public var ground:Ground;
public function playerOneCollisions(table, ground)
{
if(this.hitTestObject(table))
{
trace("tabled");
animation = "kong";
}
if(this.hitTestObject(ground))
{
trace("grounded");
animation = "idle";
}
}
The function playerOneCollisions is called in Main.as
public function GameClock(timerEvent:TimerEvent):void
{
player.playerOnePress();
player.playerOneKeyResults();
player.playerOneCollisions(table, ground);
player.playerAnimaitons();
}
The rest of the code incase I was wrong about the important parts
Player.as
package
{
import flash.display.Stage;
import flash.display.MovieClip;
import flash.events.Event;
import KeyObject;
import Table;
public class Player extends MovieClip
{
public var stageRef:Stage;
public var key:KeyObject;
public var main:Main;
public var table:Table;
public var player:Player;
public var ground:Ground;
public var leftPressed:Boolean = false;
public var rightPressed:Boolean = false;
public var upPressed:Boolean = false;
public var downPressed:Boolean = false;
public var grounded:Boolean = false;
public var animation:String = "idle";
public var runSpeed:Number = 5;
public var animationState:String = "idle";
public function Player (stageRef:Stage, X:int, Y:int):void
{
this.stageRef = stageRef;
this.x = X;
this.y = Y;
key = new KeyObject(stageRef);
}
public function playerOnePress()
{
if(key.isDown(37) || key.isDown(65)){ // if left arrow or A is pressed
leftPressed = true;
//trace("left pressed");
} else {
leftPressed = false;
}
if(key.isDown(38) || key.isDown(87)){ // if up arrow or W is pressed
upPressed = true;
//trace("up pressed");
} else {
upPressed = false;
}
if(key.isDown(39) || key.isDown(68)){ //if right arrow or D is pressed
rightPressed = true;
//trace("right pressed");
} else {
rightPressed = false;
}
if(key.isDown(40) || key.isDown(83)){ //if down arrow or S is pressed
downPressed = true;
//trace("down pressed");
} else {
downPressed = false;
}
}
public function playerOneKeyResults()
{
if(leftPressed)
{
// trace("left");
this.x -= runSpeed;
}else if(rightPressed)
{
// trace("right");
this.x += runSpeed;
}
}
public function playerOneCollisions(table, ground)
{
if(this.hitTestObject(table))
{
trace("tabled");
animation = "kong";
}
if(this.hitTestObject(ground))
{
trace("grounded");
animation = "idle";
}
}
public function playerAnimaitons()
{
if(animation == "kong")
{
this.gotoAndStop(2);
}
if(animation == "idle")
{
this.gotoAndStop(1);
}
}
}
}
Main.as
package
{
import flash.display.Stage;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.utils.Timer;
public class Main extends MovieClip
{
public var gameTimer:Timer;
public var player:Player;
public var table:Table;
public var ground:Ground;
public function Main():void
{
gameTimer = new Timer (30);
gameTimer.addEventListener(TimerEvent.TIMER, GameClock);
gameTimer.start();
player = new Player(stage, 80, 420);
stage.addChild(player);
}
public function GameClock(timerEvent:TimerEvent):void
{
player.playerOnePress();
player.playerOneKeyResults();
player.playerOneCollisions(table, ground);
player.playerAnimaitons();
}
}
}
One thing I notice is that you have table and ground as class properties of Player, and they are also parameters for the method you are having trouble with.
The table and/or ground properties of your Player class are likely null as I don't see code that sets them.
It's not good practice to have method parameters named the same as your class properties.
Try removing the properties in your Player class. (I don't see them being set or used)
If you still get the error, trace out their values at the beginning of the playerOneCollisions method and ensure they are not null.
If they are null, the problem exists outside this class and you are passing in a null value.
I should also note that I don't see anywhere in the Main class where you set the table or ground properties, so they are null unless you have not shown us all the code. If they are null, then that will certainly cause the issue you are experiencing.

AS3 - TypeError # 1009 with Timers

I just have no idea what to do with this. I've been looking through this for an hour now and I keep getting an error reading:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Shooter_Enemy/shotHandler()
at flash.utils::Timer/_timerDispatch()
at flash.utils::Timer/tick()
When I debug it it points to the line of the code where "seekingBullet" is added to the stage. Any help resolving this would be greatly welcomed.
package
{
import flash.display.*;
import flash.events.*;
import flash.utils.Timer;
public class Shooter_Enemy extends MovieClip
{
private var yMove:int = 2;
private var shootTimer:Timer = new Timer(500);
public function Shooter_Enemy()
{
this.name = "mc_shooter_enemy";
this.addEventListener(Event.ENTER_FRAME,enemyMove);
shootTimer.start();
shootTimer.addEventListener(TimerEvent.TIMER,shotHandler);
}
public function addShooterEnemy(X:int):void
{
this.x = X;
this.y = 0;
}
public function removeEnemy()
{
shootTimer.removeEventListener(TimerEvent.TIMER,shotHandler);
shootTimer.stop();
this.removeEventListener(Event.ENTER_FRAME,enemyMove);
this.x = 0;
this.y = (stage.height + this.height);
}
private function shotHandler(te:TimerEvent):void
{
var seekingBullet:SeekingBullet = new SeekingBullet();
Main.seekingBulletArray.push(seekingBullet);
stage.addChild(seekingBullet);
seekingBullet.addSeekingBullet(this.x,this.y);
}
private function enemyMove(e:Event)
{
this.y += yMove;
}
}
}
If it's actual, a good practice for using stage is listening *Event.ADDED_TO_STAGE* and then starting your activity like:
package
{
import flash.display.*;
import flash.events.*;
import flash.utils.Timer;
public class Shooter_Enemy extends MovieClip
{
private var yMove:int = 2;
private var shootTimer:Timer = new Timer(500);
public function Shooter_Enemy()
{
this.name = "mc_shooter_enemy";
this.addEventListener(Event.ENTER_FRAME,enemyMove);
shootTimer.addEventListener(TimerEvent.TIMER, shotHandler);
addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
}
public function addShooterEnemy(X:int):void
{
this.x = X;
this.y = 0;
}
public function removeEnemy()
{
shootTimer.removeEventListener(TimerEvent.TIMER,shotHandler);
shootTimer.stop();
this.removeEventListener(Event.ENTER_FRAME,enemyMove);
this.x = 0;
this.y = (stage.height + this.height);
}
private function addedToStageHandler(e:Event)
{
shootTimer.start();
}
private function shotHandler(te:TimerEvent):void
{
var seekingBullet:SeekingBullet = new SeekingBullet();
Main.seekingBulletArray.push(seekingBullet);
stage.addChild(seekingBullet);
seekingBullet.addSeekingBullet(this.x,this.y);
}
private function enemyMove(e:Event)
{
this.y += yMove;
}
}
}