PDA

View Full Version : [AS3] Game Help


kingchuck189
06-16-2011, 06:59 AM
Can anybody help me with this error?
Error #1009: Cannot access a property or method of a null object reference.
at BB_fla::MainTimeline/moveBall()[BB_fla.MainTimeline::frame2:82]

I am making a brick breaker game for android using air for android.

On frame 2

stop();
//VARIABLES
//These variables are needed for moving the ball
var ballXSpeed:Number = 12; //X Speed of the Ball
var ballYSpeed:Number = 12; //Y Speed of the Ball
//How many bricks are left on the stage
var brickAmt:int = 0;
//how many lives you got
var lives:int = 3;
//if the game is over
var gameOver:Boolean = false;
//The score of the game
var score:int = 0;
//First I defined a function where all of
//the code needed to start the game is placed
//This includes listeners, variable definitions, and other stuff
function beginCode(event:MouseEvent):void{
//removes the listener for a click
stage.removeEventListener(MouseEvent.CLICK, beginCode);
//Adds a listener to the paddle which
//runs a function every time a frame passes
mcPaddle.addEventListener(Event.ENTER_FRAME, movePaddle);
//Adds a listener to the ball which
//runs a function every time a frame passes
mcBall.addEventListener(Event.ENTER_FRAME, moveBall);
//adding a listener to check if the level is done
addEventListener(Event.ENTER_FRAME, checkLevel);
//removing the "Tap to Start" Text
txtStart.text = '';
}

function movePaddle(event:Event):void{
//The paddle follows the mouse
mcPaddle.x = mouseX - mcPaddle.width / 2;
//Keeping the paddle in the stage

//If the mouse goes off too far to the left
if(mouseX < mcPaddle.width / 2){
//Keep the paddle on stage
mcPaddle.x = 0;
}
//If the mouse goes off too far to the right
if(mouseX > stage.stageWidth - mcPaddle.width / 2){
//Keep the paddle on stage
mcPaddle.x = stage.stageWidth - mcPaddle.width;
}
}

function moveBall(event:Event):void{
//Code for moving ball goes here
mcBall.x += ballXSpeed; //Move the ball horizontally
mcBall.y += ballYSpeed; //Move the ball vertically
//Bouncing the ball off of the walls
if(mcBall.x >= stage.stageWidth-mcBall.width){
//if the ball hits the right side
//of the screen, then bounce off
ballXSpeed *= -1;
}
if(mcBall.x <= 0){
//if the ball hits the left side
//of the screen, then bounce off
ballXSpeed *= -1;
}
if(mcBall.y >= stage.stageHeight-mcBall.height){
//if the ball hits the bottom
//then bounce up and lose a life
ballYSpeed *= -1;
lives --;
//if there aren't any lives left
if(lives <= 0){
//the game is over now
gameOver = true;
//remove those silly listeners
mcPaddle.removeEventListener(Event.ENTER_FRAME, movePaddle);
mcBall.removeEventListener(Event.ENTER_FRAME, moveBall);
removeEventListener(Event.ENTER_FRAME, checkLevel);
removeEventListener(Event.ENTER_FRAME, updateTextFields);
//go to a lose frame
gotoAndStop('lose');
}
}
if(mcBall.y <= 0){ <-- LINE 82. PROBLEM LIES HERE
//if the ball hits the top
//then bounce down
ballYSpeed *= -1;
}
//Hitting the paddle
if(mcBall.hitTestObject(mcPaddle)){
calcBallAngle();
}
}

function calcBallAngle():void{
//ballPosition is the position of the ball is on the paddle
var ballPosition:Number = mcBall.x - mcPaddle.x;
//hitPercent converts ballPosition into a percent
//All the way to the left is -.5
//All the way to the right is .5
//The center is 0
var hitPercent:Number = (ballPosition / (mcPaddle.width - mcBall.width)) - .5;
//Gets the hitPercent and makes it a larger number so the
//ball actually bounces
ballXSpeed = hitPercent * 10;
//Making the ball bounce back up
ballYSpeed *= -1;
}

function makeLvl():void{ //Places bricks onto Level
//checking if indeed there are any more levels left
if(currentLvl > lvlArray.length){
//the game is over now
gameOver = true;
//remove those silly listeners
mcPaddle.removeEventListener(Event.ENTER_FRAME, movePaddle);
mcBall.removeEventListener(Event.ENTER_FRAME, moveBall);
removeEventListener(Event.ENTER_FRAME, checkLevel);
removeEventListener(Event.ENTER_FRAME, updateTextFields);
//go to a lose frame
gotoAndStop("win");
}
//finding the array length of the lvl code
//The index has to be currentLvl-1 because:
//array indexes start on 0 and our lvl starts at 1
//our level will always be 1 higher than the actual index of the array
var arrayLength:int = lvlArray[currentLvl-1].length;
//the current row of bricks we are creating
var brickRow:int = 0;
//Now, creating a loop which places the bricks onto the stage
for(var i:int = 0;i<arrayLength;i++){
//checking if it should place a brick there
if(lvlArray[currentLvl-1][i] == 1){
//creating a variable which holds the brick instance
var brick:Brick = new Brick();
//setting the brick's coordinates via the i variable and brickRow
brick.x = 15+(i-brickRow*7)*75;
brick.y = 10+brickRow*20;
//checks if the current brick needs a new row
for(var c:int = 1;c<=10;c++){
if(i == c*7-1){
brickRow ++;
}
}
//finally, add the brick to stage
addChild(brick);
}
}
}

function checkLevel(event:Event):void{
//checking if the bricks are all gone
if(brickAmt == 0){
//reset the level by increasing the level
currentLvl ++;
//and re-running makeLvl
//resetting the text's word
txtStart.text = "Tap To Begin";
makeLvl();
//then resetting the ball's and paddle's position
mcBall.x = 150;
mcBall.y = 265;
mcPaddle.x = 230;
//then removing all of the listeners
mcPaddle.removeEventListener(Event.ENTER_FRAME, movePaddle);
mcBall.removeEventListener(Event.ENTER_FRAME, moveBall);
removeEventListener(Event.ENTER_FRAME, checkLevel);
//then listening for a mouse click to start the game again
stage.addEventListener(MouseEvent.CLICK, beginCode);
}
}

function updateTextFields(event:Event):void{
txtStats.text = "Level: "+currentLvl+" Lives: "+lives+" Score: "+score;
}
//if the mouse clicks, then begin the game
stage.addEventListener(MouseEvent.CLICK, beginCode);
//setting the text's word
txtStart.text = "Tap To Begin";
//creating a function to update the text fields
addEventListener(Event.ENTER_FRAME, updateTextFields);
//making the level
makeLvl();


any ideas???

SephirothOWA
06-16-2011, 10:14 AM
try movie the above if statetement to the bottom of the function:

if(mcBall.y >= stage.stageHeight-mcBall.height){
...

down to:

if(mcBall.hitTestObject(mcPaddle)){
calcBallAngle();
}
if(mcBall.y >= stage.stageHeight-mcBall.height){
...


If the frame where gotoAndStop('lose') doesn't have the instance "mcBall" on it, code after the frame jump that involves it will be trying to access something that isn't there.

Alternatively, putting return; after the gotoAndStop would sklip the code below if the frame changes :)

kingchuck189
06-16-2011, 08:40 PM
Thank you for the quick response but it still didn't fix the problem.
It keeps saying that there is something wrong with

if(mcBall.y <= 0){


even after I do what you said. Same error:
#1009: Cannot access a property or method of a null object reference.
Any suggestions?
If you need the rest of my code i can provide that.
Thanks

SephirothOWA
06-16-2011, 10:21 PM
as a temporary fix you could change the line to:

if(mcBall != null && mcBall.y <= 0) {

But if you played around wtih it and found out why it is set to null in the first place, then it'd be easier to actually solve the problem.

Note, you'd have to do the same for the hitTest below it

kingchuck189
06-17-2011, 05:48 AM
Thanks man your the best. the fix was quick and easy. Thanks again!!! :)