ForumsProgramming ForumKeyboard

5 5958
alex57834
offline
alex57834
88 posts
Nomad

I'm not quite sure whats wrong. I have received no errors just right and left keys not moving the basket.

package {

import flash.display.MovieClip;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.events.Event;

public class MainGame extends MovieClip {

public var army:Array;
public var gb:GoldBar;
public var basket:Basket;
public var gameTimer:Timer;
public var leftKeyPress:Boolean;
public var rightKeyPress:Boolean;

public function MainGame()
{
leftKeyPress = false;
rightKeyPress = false;

addEventListener(Event.ADDED_TO_STAGE, onAddToStage);

army = new Array();

basket = new Basket();
basket.x = 225;
basket.y = 100;
addChild(basket);

gameTimer = new Timer(25);
gameTimer.addEventListener(TimerEvent.TIMER, onTick);
gameTimer.start();

}

public function onTick(e:TimerEvent):void
{
if( Math.random () < 0.08 )
{

var randomX:Number = Math.random() * 500;
var gb = new GoldBar(randomX , -20);
army.push(gb);
addChild (gb);

for each (var gb in Army)
{
gb.moveDown();
}

}

if( rightKeyPress == true)
{
basket.Move(1);
}

if (leftKeyPress == true)
{
basket.Move(-1);
}
}

function onAddToStage(e:Event):void
{
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPress);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyRelease);
}

public function onKeyPress(e:KeyboardEvent):void
{
if(keyboardEvent.keyCode == Keyboard.RIGHT)
{
rightKeyPress = true;
}

if(keyboardEvent.keyCode == Keyboard.LEFT)
{
leftKeyPress = true;
}
}

public function onKeyRelease(e:KeyboardEvent):void
{
if(keyboardEvent.keyCode == Keyboard.RIGHT)
{
rightKeyPress = false;
}

if(keyboardEvent.keyCode == Keyboard.LEFT)
{
leftKeyPress = false;
}
}

}

}




package {

import flash.display.MovieClip;

public class Basket extends MovieClip {

public function Basket()
{

}

public function Move(xMove:Number):void
{
var baseSpeed:Number = 3;
x += (xMove * baseSpeed);
}
}

}

  • 5 Replies
strouthas
offline
strouthas
24 posts
Nomad

On keyboard listeners replace keyboardEvent.keyCode with e.keyCode and you are done.

alex57834
offline
alex57834
88 posts
Nomad

doesn't work all the other functions work except the keyboard stuff. I put traces in all the functions but none showed so I can't tell where it has gone wrong.

package {

import flash.display.MovieClip;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.events.Event;

public class MainGame extends MovieClip {

public var army:Array;
public var gb:GoldBar;
public var basket:Basket;
public var gameTimer:Timer;
public var leftKeyPress:Boolean;
public var rightKeyPress:Boolean;

public function MainGame()
{
trace("game has started&quot;
leftKeyPress = false;
rightKeyPress = false;

army = new Array();

basket = new Basket();
basket.x = 225;
basket.y = 100;
addChild(basket);

gameTimer = new Timer(25);
gameTimer.addEventListener(TimerEvent.TIMER, onTick);
gameTimer.start();

addEventListener(Event.ADDED_TO_STAGE, onAddToStage);
}

public function onTick(e:TimerEvent):void
{
if( Math.random () < 0.08 )
{
var randomX:Number = Math.random() * 500;
var gb = new GoldBar(randomX , -20);
army.push(gb);
addChild (gb);

for each (var gb in Army)
{
gb.moveDown();
}

}

if( rightKeyPress);
{
Basket.moveAway(1);
}

if (leftKeyPress);
{
Basket.moveAway(-1);
}
}

function onAddToStage(e:Event):void
{

stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPress);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyRelease);
}

public function onKeyPress(e:KeyboardEvent):void
{
if(e.keyCode == Keyboard.RIGHT);
{
rightKeyPress = true;
}

if(e.keyCode == Keyboard.LEFT);
{
leftKeyPress = true;
}
}
public function onKeyRelease(e:KeyboardEvent):void
{
if(e.keyCode == Keyboard.RIGHT);
{
rightKeyPress = false;
}

if(e.keyCode == Keyboard.LEFT);
{
leftKeyPress = false;
}
}

}

}


package {

import flash.display.MovieClip;

public class Basket extends MovieClip {

var baseSpeed:Number;

public function Basket()
{
baseSpeed = 3;
}

public function moveAway(xMove:Number):void
{
x += (xMove * baseSpeed);
}
}

}

strouthas
offline
strouthas
24 posts
Nomad

Well, I tried it my self and works fine, I had to remove the GoldBar Class though as it wasn't included in your code togehter with the portion in the timertick function to make it work. Btw there is a typo in there as well:

army.push(gb);
for each (var gb in Army)
{
gb.moveDown();
}

beware the capitalization.

The interseting point is you said that there were no warnings or errors, but in my CS5.5 F;ash Pro doesn't compile with that.

If you still have problems, let me know and I will upload for you the working .fla and .as I tested already.

weirdlike
offline
weirdlike
1,299 posts
Prince

your key input need to be switch and you need an event listener for when the boolean is true here I give you my controls.as file all you need is to use

public static var up:Boolean = false;
public static var down:Boolean = false;
public static var left:Boolean = false;
public static var right:Boolean = false;

controls.addWASD(stage);

or

controls.addArrow(stage);

addEventListener(Event.ENTER_FRAME, keyInput);
function keyInput(event:Event):void
{
if (left)
{
trace("LEFT&quot;
}
if (right)
{
trace("RIGHT&quot;
}
if (up)
{
trace("UP&quot;
}
if (down)
{
trace("DOWN&quot;
}
}

LINK
to download you might need to right click > save as

weirdlike
offline
weirdlike
1,299 posts
Prince

if you use my controls.as file you will need to change the

public class controls extends gamePlay

to

public class controls extends MainGame

it needs to extend from the main game constructor

Showing 1-5 of 5