Hey guys im making a flash game for a uni project and i had it all working great! I then proceeded to move my code and content onto the second frame to add menus and such to the game and i updated all the code so it should work on the second frame and the game still works great apart from my paddle no long moving and i cant figure out at all what ive done.
this is the code for the actual game part of it. sorry for the clutter of it but im not good with code!
could anyone tell me what i need to do to get my paddle moving again!
//code!
import flash.utils.Timer;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent
var keyArray : Array = new Array(false,false);//An array to check if keys are pressed
var ballSpeed : int = 5;//the speed of falling lemmings
var moveSpeed : int = 7;//the speed the player can move their boat at
boat.x = stage.stageWidth/2;//move the boat to the centre of the stage at the start of the game
var lives : int = 5;//the amount of lives the player will have
var score : int = 0;//the starting score a player has
var minSpawnTime : int = 1500;//the time it takes the lemmings to spawn
var maxSpawnTime : int = 3250;//maximum time before next lemming is spawned
var spawnTimer:Timer = new Timer(2000,1); //2 seconds and loops
lives_txt.text = "Lives Left: " + lives;//text box reads lives left
score_txt.text = "Score: " + score;//text box reads score
stop();
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);//event listener to tell flash to call "key pressed"
stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);//event listener to tell flash to call "key released"
stage.addEventListener(Event.ENTER_FRAME, update);//event listener to tell flash to call "update" for new frames
startGame();//tells flash to call the startGame function
function startGame():void//a new function called startGame
{
spawnTimer.addEventListener(TimerEvent.TIMER, spawnBall);//new event listener to listen for the timer to run out and spawn new lemming
spawnTimer.start();//starts the clock on the timer
}
function keyPressed(event:KeyboardEvent) : void//a new function called keyPressed for when player uses arrow keys
{
if(event.keyCode == 39)//if the keyboard event is the right key
{
trace("right");//prints right in output window
keyArray[0] = true;//right key array is true
}
if(event.keyCode == 37)//if the keyboard event is the left key
{
trace("left");//prints left in output window
keyArray[1] = true;//left key array is true
}
}
function keyReleased(event:KeyboardEvent) : void//a new function called keyReleased for when arrow keys are released
{
if(event.keyCode == 39 )//if the keyboard event is the right key
{
keyArray[0] = false;//right key array false
}
if(event.keyCode == 37)//if the keyboard event is the left key
{
keyArray[1] = false;//left key array false
}
}
function update(event:Event) : void//a new function called update for updating the x position of the boat
{
if(keyArray[0] == true)//if the keyArray for left is true
{
boat.x += moveSpeed;//move the x position of the boat right with the designated move speed
}
if(keyArray[1] == true)//if the keyArray for right is true
{
boat.x -= moveSpeed;//move the x position of the boat left with the designated move speed
}
}
function spawnBall(event:TimerEvent): void//a new function called spawnBall
{
trace("SPAWN");//prints SPAWN in the output
var ball : Ball = new Ball();//declares a new "temporary variable called "ball" that stores object of Type:Ball and populates it with a new object Ball
stage.addChild(ball);//adds this new ball onto the stage
ball.y = 0;//set the ball's vertical position to fall from
ball.x = ball.width + (Math.random() * (stage.stageWidth - (2*ball.width))); //sets the ball to have a random position on the x axsis so they seem to fall randomly
ball.addEventListener(Event.ENTER_FRAME, moveBall); //new event listener for the ball to listen for a new frame and to call the "moveBall" function
spawnTimer.reset(); //resets the spawnTimer so it can be re-started
spawnTimer.delay = minSpawnTime + (Math.random() * (maxSpawnTime - minSpawnTime) ); //set the delay for the next time to be somewhere between the minSpawnTime (1.5 seconds) and maxSpawnTime (3.25 seconds)
spawnTimer.start();//tell the spawnTimer to start again
if((maxSpawnTime - 20) >= minSpawnTime) //if the maximum time between spawn times is too close to the minimum time between spawns
{
maxSpawnTime -= 20;//drop the minimum time between spawns by 20
}
}
function moveBall(event:Event):void //new function called "moveBall" takes up 1 argument "event"
{
event.currentTarget.y += ballSpeed; //move the object this functions event listener by the speed set.
if(event.currentTarget.hitTestObject(sea)) //if the lemming falls and hits the sea object
{
if(this.currentFrame == 2)//and we are still on frame 2
{
lives--;//take 1 life away from player
if(lives > -1)//if the player still have lifes
{
//set the text box to display the amount of lives remaining
lives_txt.text = "Lives Left: " + lives;
}
}
if(lives < 0)//if the player has less than no lives game is over
{
stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyPressed);//take away event listeners for key presses and releases
stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyReleased);
stage.removeEventListener(Event.ENTER_FRAME, update);//remove the update event listeners
spawnTimer.stop();//stop the timer
gotoAndStop(3);//tell flash to go to and stop on the loser screen
}
event.currentTarget.parent.removeChild(event.currentTarget);//remove the falling objects from the game
event.currentTarget.removeEventListener(Event.ENTER_FRAME, moveBall);//remove eventlistener for when game is over updating falling objects
}
else
{
if(this.currentFrame == 2)//if the player is still in game
{
if(event.currentTarget.hitTestObject(boat))//if eventListener is attached to hits the game object called "boat"
{
score++;//add 1 to the players score
score_txt.text = "Score: " + score;//update the score text to read the players score
event.currentTarget.parent.removeChild(event.currentTarget);//remove remove the lemming after it has hit boat
event.currentTarget.removeEventListener(Event.ENTER_FRAME, moveBall);//remove the eventlistener after caught so it doesnt keep updating
}
if(score > 24)//if the player has more than 24 points
{
stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyPressed);//take away event listeners for key presses and releases
stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyReleased);
stage.removeEventListener(Event.ENTER_FRAME, update);//remove the update event listeners
spawnTimer.stop();//stop the timer
gotoAndStop(4);//tell flash to go to and stop on the 3rd frame of the time line i.e. the Game Over screen
}
}
}
}