Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 06-16-2011, 06:59 AM   #1
kingchuck189
Registered User
 
Join Date: Jun 2011
Posts: 3
Exclamation [AS3] Game Help

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
Code:
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???
kingchuck189 is offline   Reply With Quote
Old 06-16-2011, 10:14 AM   #2
SephirothOWA
Senior Member
 
Join Date: Jun 2010
Location: Ipswich, England
Posts: 261
Default

try movie the above if statetement to the bottom of the function:
ActionScript Code:
if(mcBall.y >= stage.stageHeight-mcBall.height){     ...
down to:
ActionScript Code:
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
ActionScript Code:
return;
after the gotoAndStop would sklip the code below if the frame changes

Last edited by SephirothOWA; 06-16-2011 at 10:18 AM.
SephirothOWA is offline   Reply With Quote
Old 06-16-2011, 08:40 PM   #3
kingchuck189
Registered User
 
Join Date: Jun 2011
Posts: 3
Default Any other ideas?

Thank you for the quick response but it still didn't fix the problem.
It keeps saying that there is something wrong with
Code:
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
kingchuck189 is offline   Reply With Quote
Old 06-16-2011, 10:21 PM   #4
SephirothOWA
Senior Member
 
Join Date: Jun 2010
Location: Ipswich, England
Posts: 261
Default

as a temporary fix you could change the line to:
ActionScript Code:
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
SephirothOWA is offline   Reply With Quote
Old 06-17-2011, 05:48 AM   #5
kingchuck189
Registered User
 
Join Date: Jun 2011
Posts: 3
Default Fixed!

Thanks man your the best. the fix was quick and easy. Thanks again!!!
kingchuck189 is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 08:33 PM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.