Hello,
I’m having a really weird problem I can’t figure out, the Keyboard Input won’t work until Mouse Button is pressed. Here’s the code to the class I’m having trouble with. If anyone can see what’s causing the trouble that would be great.
package
{
// Import necessary classes from the flash libraries
import flash.display.Sprite;
import flash.events.*;
import flash.events.MouseEvent;
import flash.events.KeyboardEvent;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.geom.Point;
import flash.ui.Mouse;
import flash.ui.Keyboard;
import com.efg.framework.Game;
import com.efg.framework.CustomEventLevelScreenUpdate;
import com.efg.framework.CustomEventScoreBoardUpdate;
import com.efg.framework.CustomEventSound;
public class Lunar_Mayhem extends Game
{
//constructor
private var gameWidth:int;
private var gameHeight:int;
private const SPEED:Number = .4;
//New Game
private var score:int;
private var level:int;
private var ships:int;
private var shots:int;
private var isGameOver:Boolean = false;
//New Level
private var explodeArray:Array;
private var shotArray:Array;
private var shipArray:Array;
private var crosshairs:Bitmap;
public var _vx:Number;
public var _vy:Number;
private var tempShot:Shot;
private var _player:Ship;
private var _bg1:Bitmap;
private var _bg2:Bitmap;
private var _bg3:Bitmap;
//private var tempShot:Shot;
private var tempShip:Ship;
private var tempExplode:Explosion;
public function Lunar_Mayhem(gW:int,gH:int) {
gameWidth=gW;
gameHeight=gH;
placeShip(_vx,_vy);
_vx = 0;
_vy = 0;
}
override public function newGame():void {
level = 0 ;
score = 0;
ships = 3;
shots = 0;
isGameOver = false;
//BACKGROUND IMAGE
dispatchEvent(new CustomEventScoreBoardUpdate(CustomEventScoreBoardUpdate.UPDATE_TEXT,Main.SCORE_BOARD_SCORE,"0"));
dispatchEvent(new CustomEventScoreBoardUpdate(CustomEventScoreBoardUpdate.UPDATE_TEXT,Main.SCORE_BOARD_SHOTS,String(shots)));
if (crosshairs == null) {
Mouse.hide();
//***** Flex *****
//crosshairs = new CrosshairsGif(); //chnaged
//***** Flash *****
crosshairs = new Bitmap(new CrosshairsGif(0,0));
crosshairs.scaleX = .5;
crosshairs.scaleY = .5;
crosshairs.x=gameWidth/2;
crosshairs.y=gameHeight/2;
addChild(crosshairs);
stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveListener, false, 0, true);
stage.addEventListener(MouseEvent.MOUSE_DOWN, shootListener, false, 0, true);
stage.addEventListener(KeyboardEvent.KEY_DOWN,onKeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
}
}
public function onKeyDown(e:KeyboardEvent):void
{
switch (e.keyCode)
{
case Keyboard.LEFT:
_vx -= SPEED *.5;
break;
case Keyboard.RIGHT:
_vx += SPEED * .5;
break;
case Keyboard.UP:
_vy -= SPEED;
break;
case Keyboard.DOWN:
break;
}
}
public function onKeyUp(e:KeyboardEvent):void
{
if (e.keyCode == Keyboard.LEFT
|| e.keyCode == Keyboard.RIGHT)
{
//_vx = 0;
}
if (e.keyCode == Keyboard.UP
|| e.keyCode == Keyboard.DOWN)
{
//_vy = 0;
}
}
override public function newLevel():void {
explodeArray = [];
shotArray = [];
shipArray = [];
level++;
dispatchEvent(new CustomEventLevelScreenUpdate(CustomEventLevelScreenUpdate.UPDATE_TEXT, String(level)));
dispatchEvent(new CustomEventScoreBoardUpdate(CustomEventScoreBoardUpdate.UPDATE_TEXT,Main.SCORE_BOARD_SHOTS,String(shots)));
dispatchEvent(new CustomEventScoreBoardUpdate(CustomEventScoreBoardUpdate.UPDATE_TEXT,Main.SCORE_BOARD_SHIPS,String(ships)));
}
private function placeShip(_vx:Number,_vy:Number):void {
_player = new Ship(_vx,_vy);
_player.x = 20;
_player.y = 20;
this.addChild(_player);
}
override public function runGame():void {
update(_vx,_vy);
render();
checkCollisions();
checkforEndLevel();
checkforEndGame();
trace(_vy);
trace(_vx);
}
private function update(_vx:Number,_vy:Number):void {
_player.update(_vx,_vy);
//_player.gitThis(_vx,_vy);
for each (tempShot in shotArray){
if ((x > 550) || (x < 0))
{
removeItemFromArray(tempShot,shotArray);
}
if ((y > 400) || (y < 0))
{
removeItemFromArray(tempShot,shotArray);
}
if (tempShot.finished) {
//Add Flak Explosion
//tempFlak = new Flak();
//tempFlak.x = tempShot.x-(tempShot.width/2);
//tempFlak.y = tempShot.y-(tempShot.height/2);
//this.addChild(tempFlak);
//flakArray.push(tempFlak);
dispatchEvent( new CustomEventSound(CustomEventSound.PLAY_SOUND,Main.FIRE,false,1,0,.5));
removeItemFromArray(tempShot,shotArray);
} else {
tempShot.update();
}
}
/*for each (_player in shipArray){
if (_player.finished) {
removeItemFromArray(_player,shipArray);
} else {*/
/*}
}*/
/*for each (tempEnemy in enemyArray){
if (tempEnemy.finished) {
removeItemFromArray(tempEnemy,enemyArray);
} else {
tempEnemy.update();
}
}*/
for each (tempExplode in explodeArray){
if (tempExplode.finished) {
removeItemFromArray(tempExplode,explodeArray);
} else {
tempExplode.update();
}
}
/*for each (tempBonusPlane in bonusPlaneArray){
if (tempBonusPlane.finished) {
removeItemFromArray(tempBonusPlane,bonusPlaneArray);
} else {
tempBonusPlane.update();
}
}*/
}
private function checkCollisions():void {
}
private function removeItemFromArrayByIndex(index:int, group:Array):void{ //changed
group[index].dispose();
removeChild(group[index]);
group.splice(index, 1);
}
private function cleanUpLevel():void {
var ctr:int = 0;
for (ctr = shotArray.length-1;ctr >=0;ctr--) {
//removeItemFromArray(shotArray[ctr],shotArray);
removeItemFromArrayByIndex(ctr,shotArray);
}
for (ctr = explodeArray.length-1;ctr >=0;ctr--) {
//removeItemFromArray(explodeArray[ctr],explodeArray);
removeItemFromArrayByIndex(ctr,explodeArray);
}
/*for (ctr = shipArray.length-1;ctr >=0;ctr--) {
//removeItemFromArrayBy(shipArray[ctr],shipArray);
removeItemFromArrayByIndex(ctr,explodeArray);
}*/
stage.removeEventListener(MouseEvent.MOUSE_DOWN, shootListener);
}
public function shootListener(e:MouseEvent):void {
tempShot = new Shot((_player.x +_player.width/2-5),(_player.y + _player.height/2),mouseX-(crosshairs.width/2),mouseY-(crosshairs.height/2));
this.addChild(tempShot);
shotArray.push(tempShot);
shots--;
dispatchEvent(new CustomEventScoreBoardUpdate(CustomEventScoreBoardUpdate.UPDATE_TEXT,Main.SCORE_BOARD_SHOTS,String(shots)));
dispatchEvent( new CustomEventSound(CustomEventSound.PLAY_SOUND,Main.FIRE,false,1,0,.5));
/*} else {
dispatchEvent( new CustomEventSound(CustomEventSound.PLAY_SOUND,Main.SOUND_NOSHOTS,false,1,0,.75));*/
}
public function mouseMoveListener(e:MouseEvent):void {
crosshairs.x = e.stageX-(crosshairs.width/2);
crosshairs.y = e.stageY-(crosshairs.height/2);
}
private function render():void {
for each (tempShot in shotArray){
tempShot.render();
}
/*for each (_player in shipArray){*/
_player.render();
//}
//for each (tempEnemy in enemyArray){
//tempEnemy.render();
//}
for each (tempExplode in explodeArray){
tempExplode.render();
}
//for each (tempBonusPlane in bonusPlaneArray){
//tempBonusPlane.render();
//}
}
private function checkforEndGame():void {
if (ships <=0) {
dispatchEvent(new Event(Game.GAME_OVER));
removeChild(crosshairs);
Mouse.show();
stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveListener);
cleanUpLevel();
crosshairs = null;
}
}
private function removeItemFromArray(item:Object, group:Array):void{ //changed
var index:int = group.indexOf(item);
group[index].dispose();
removeChild(group[index]);
group.splice(index, 1);
}
private function checkforEndLevel():void {
/*if (enemyArray.length <= 0 && incomingCount >= numEnemies && explodeArray.length <=0 && flakArray.length <=0 && shotArray.length <=0 && bonusPlaneArray.length <= 0) {
addToScore(10*shots);
cleanUpLevel();
dispatchEvent(new Event(Game.NEW_LEVEL));
}*/
}
}
}