-
a way to restart game
Hey, I'm a little new to advanced as3, im trying to figure out a way to end the update() function for my main character in my game (I actually used a tutorial to make it). I managed to add a pause screen, I just need a way to return to the main menu without it giving me a "Cannot access a property or method of a null object reference. at GameController/update()" error.
The menu screen is on a different frame, I basically want the game just to restart everything when going back to the main menu. Any ideas? Thanks.
Code:
/*
-------------------------------------------------------------------------------------------------
All contents (including text, graphics, actionscript code, fla source files and all other original
works), on MakeFlashGames.com website is licensed under a Creative Commons License.
Copyright: Joseph Tan
Location: Singapore
Website: http://www.makeflashgames.com
Email: joseph@makeflashgames.com
Licensing: http://creativecommons.org/licenses/by-nc/2.0/
None of the tutorials/codes/graphics here should be distributed or used for any commercial purposes
without first seeking prior permission from the author.
Any use of the materials here must carry an acknowledgment of the original author as the sole
owner to the rights of this document.
Kindly contact joseph@makeflashgames.com if you would like to use these materials for commercial
or educational purposes.
-------------------------------------------------------------------------------------------------
*/
package
{
import flash.display.*;
import flash.events.*;
import flash.geom.*;
import flash.text.*;
import flash.utils.*;
import flash.ui.*;
import flash.sensors.Accelerometer;
import flash.events.AccelerometerEvent;
import Game.*;
public class GameController extends MovieClip
{
private var playerScore:Number;
private var bricks:Array;
private var scrollSoFar:Number;
private var player:Player;
private var moveX:Number;
private var jump:Boolean;
var accel:Accelerometer;
public function GameController()
{
}
public function startGame()
{
playerScore = C.PLAYER_START_SCORE;
bricks = new Array();
scrollSoFar = 0;
moveX = 0;
jump = false;
//Add first 3 bricks for player to step on
//Add player on to stage
player = new Player(C.PLAYER_START_X, C.PLAYER_START_Y);
mcGameStage.addChild(player);
mcGameStage.addEventListener(Event.ENTER_FRAME,update);
//Handle event when this game is being preloaded
addEventListener(Event.ADDED_TO_STAGE, gameAddedToStage );
//Handle situations when this game is being run directly
//*****************accelerometer**************************
function accelMove(event:AccelerometerEvent):void
{
player.x -= event.accelerationX * 20;
if (player.x < 0)
{
player.x = 0;
}
else if (player.x > (stage.stageWidth) )
{
player.x = stage.stageWidth - player.width;
}
}
accel = new Accelerometer();
accel.setRequestedUpdateInterval(.1);
if (Accelerometer.isSupported)
{
accel.addEventListener(AccelerometerEvent.UPDATE, accelMove);
}
else
{
//If there is no accelerometer support...
}
if (stage != null)
{
stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_UP,keyUpHandler);
}
}
private function gameAddedToStage(evt: Event):void
{
stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_UP,keyUpHandler);
}
private function keyDownHandler(evt:KeyboardEvent):void
{
if (evt.keyCode == 37) //LEFT Key
{
moveX = -1;
}
else if (evt.keyCode == 39) //RIGHT Key
{
moveX = 1;
}
if (evt.keyCode == 32) //Spacebar
{
jump = true;
}
}
private function keyUpHandler(evt:KeyboardEvent):void
{
if ((evt.keyCode == 37) || (evt.keyCode == 39))
{
moveX = 0;
}
if (evt.keyCode == 32) //Spacebar
{
jump = false;
}
}
public function update(evt:Event)
{
//******************
//Handle User Input
//******************
if (moveX > 0)
player.moveRight();
else if (moveX < 0)
player.moveLeft();
else if (moveX == 0)
player.stopMoving();
//******************
//Handle Game Logic
//******************
//Check to spawn bricks
scrollSoFar += C.BRICK_SPEED;
if (scrollSoFar % C.SPAWN_BRICK_FACTOR == 0)
{
var randomXPos = Math.floor(Math.random() * C.GAME_WIDTH/1.6) +
C.GAME_WIDTH/480;
var newBrick = new Brick(randomXPos, C.BRICK_SPAWN_Y);
bricks.push(newBrick);
mcGameStage.addChildAt(newBrick,0);
}
//Update Bricks
for (var i=bricks.length - 1; i >= 0; i--)
{
bricks[i].update();
if (bricks[i].notInScreen())
{
mcGameStage.removeChild(bricks[i]);
bricks.splice(i,1);
//Add score
playerScore += C.SCORE_PER_BRICK;
}
}
//Update Player
player.update();
//******************This is what controls if the player hits the bottom of the screen******************
if (player.hitTestObject(bottom))
{
player.hitFloorb(bottom);
}
if (player.hitTestObject(top))
{
removeChild(benter);
gotoAndPlay(1);
}
//Check for collisions
if (player.isInAir() && player.isFalling())
{
for (var i=bricks.length - 1; i >= 0; i--)
{
if (player.hitAreaFloor.hitTestObject(bricks[i]))
{
//Player landed on the brick
player.hitFloor(bricks[i]);
}
}
}
if (player.notInScreen())
gameOver();
//******************
//Handle Display
//******************
//Display new Score
txtScorePlayer.text = String(playerScore);
}
private function gameOver()
{
player = null;
bricks = null;
mcGameStage.removeEventListener(Event.ENTER_FRAME,update);
stage.removeEventListener(KeyboardEvent.KEY_DOWN,keyDownHandler);
stage.removeEventListener(KeyboardEvent.KEY_UP,keyUpHandler);
}
/**************REMOVE GAME**************/
public function removeGame():void
{
trace("Remove Game");
}
//public functions for children to invoke
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|