I can not locate where the problem is coming from, I am trying to make an online game and it keeps telling me there’s an error with my public function:‘stage.addEventListener…onKeyPressed)…’ It may be something simple with the arrangements of my code but I cant work it out and the deadline is on Thursday. If somebody could help me on this matter it would be much, much appreciated!
package {
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.events.Event;
import flash.display.MovieClip;
import flash.events.*;
public class Main extends MovieClip{
var instructions:InstructionsPage;
var gameScreen:GameScreen;
var endGameScreen:EndGameScreen;
public var sc:Scuba;
public var vx:int;
public var vy:int;
public var leftPressed:Boolean;
public var rightPressed:Boolean;
public var upPressed:Boolean;
public var downPressed:Boolean;
public var sharkArray:Array;
public var numberOfSharks:int;
public function Main(){
instructions = new InstructionsPage();
gameScreen = new GameScreen();
endGameScreen = new EndGameScreen();
instructions.playBtn.addEventListener(MouseEvent.CLICK,gotoGame);
gameScreen.endgameBtn.addEventListener(MouseEvent.CLICK,endGame);
endGameScreen.playagainBtn.addEventListener(MouseEvent.CLICK,playAgain);
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyReleased);
addEventListener(Event.ENTER_FRAME,frameLoop);
addChild(instructions);
}
private function gotoGame(event:MouseEvent):void {
stage.focus=null;
removeChild(instructions);
addChild(gameScreen);
sc = new Scuba();
sc.x = 320;
sc.y = 300;
addChild(sc);
/*gameScreen.upBtn.addEventListener(MouseEvent.CLICK,moveUp)
gameScreen.downBtn.addEventListener(MouseEvent.CLICK,moveDown);
gameScreen.leftBtn.addEventListener(MouseEvent.CLICK,moveLeft);
gameScreen.rightBtn.addEventListener(MouseEvent.CLICK,moveRight);
gameScreen.rotateBtn.addEventListener(MouseEvent.CLICK,rotate);
gameScreen.biggerBtn.addEventListener(MouseEvent.CLICK,makeBigger);
gameScreen.smallerBtn.addEventListener(MouseEvent.CLICK,makeSmaller);
gameScreen.visibleBtn.addEventListener(MouseEvent.CLICK,makeInvisible);*/
init();
}
private function endGame(event:MouseEvent):void {
removeChild(gameScreen);
addChild(endGameScreen);
}
private function playAgain(event:MouseEvent):void {
removeChild(endGameScreen);
addChild(instructions);
}
/*private function moveUp(event:MouseEvent):void{
sc.y -= 10;
}
private function moveDown(event:MouseEvent):void{
sc.y += 10;
}
private function moveLeft(event:MouseEvent):void{
sc.x -= 10;
}
private function moveRight(event:MouseEvent):void{
sc.x += 10;
}
private function rotate(event:MouseEvent):void{
sc.rotation += 10;
}
private function makeBigger(event:MouseEvent):void{
sc.scaleX = sc.scaleY += 0.1;
}
private function makeSmaller(event:MouseEvent):void{
sc.scaleX = sc.scaleY -= 0.1;
}
private function makeInvisible(event:MouseEvent):void{
sc.visible=false;
}*/
private function init():void{
vx=0;
vy=0;
sc.x = stage.stageWidth/2;
sc.y = stage.stageHeight/2;
numberOfSharks = 7;
for(var j:int=0; j<numberOfSharks; j++){
// create a random speed and a random direction
var sharkSpeed:int = Math.ceil(Math.random() * 10);
var directionValues:Array = [-1,1]
// the Math.round(Math.random()) bit creates a value of either 0 or 1
// based on this I can pick a direction value of -1 or 1
var randomDirection:int = directionValues[Math.round(Math.random())];
// create a new worm and pass it some start values
var newShark:Shark = new Shark(sharkSpeed,randomDirection);
sharkArray.push(newShark);
addChild(newShark);
private function onKeyPressed(event:KeyboardEvent):void{
if (event.keyCode == Keyboard.LEFT){
leftPressed = true;
} else if (event.keyCode == Keyboard.RIGHT){
rightPressed = true;
} else if (event.keyCode == Keyboard.DOWN){
downPressed = true;
} else if (event.keyCode == Keyboard.UP){
upPressed = true;
}
}
private function onKeyReleased(event:KeyboardEvent):void{
if (event.keyCode == Keyboard.LEFT){
leftPressed = false;
} else if (event.keyCode == Keyboard.RIGHT){
rightPressed = false;
} else if (event.keyCode == Keyboard.DOWN){
downPressed = false;
} else if (event.keyCode == Keyboard.UP){
upPressed = false;
}
}
private function frameLoop(evt:Event):void{
vx = 0;
vy = 0;
if(leftPressed){
vx -= 10;
}
if(rightPressed){
vx +=10;
}
if(downPressed){
vy += 10;
}
if(upPressed){
vy -=10;
}
sc.x += vx;
sc.y += vy;
var scHalfHeight:Number = sc.height/2;
var scHalfWidth:Number = sc.width/2;
if (sc.x + scHalfWidth > stage.stageWidth){
sc.x = stage.stageWidth - scHalfWidth;
} else if (sc.x - scHalfWidth < 0){
sc.x = 0 + scHalfWidth;
}
if (sc.y - scHalfHeight < 0){
sc.y = 0 + scHalfHeight;
} else if (sc.y + scHalfHeight > stage.stageHeight){
sc.y = stage.stageHeight - scHalfHeight;
}
}
}
}