Forum Topic: [AS3] error help again :(

(61 views • 3 replies)

This topic is 1 page long.

<< < > >>
Resigned

goldleader23

Reply To Post Reply & Quote

Posted at: 7/4/09 10:29 PM

goldleader23 LIGHT LEVEL 20

Sign-Up: 08/29/08

Posts: 31

I just restructured my entire game, because it wasn't working right. I cleared up about 20 compiler errors and kept playing with stuff, but now I get this. It's been bugging me all day. I'm sure I'm just overlooking something, and there still might be a lot more work to do. Also, I'm still trying to figure out the whole "Class" thing, if anything seems off.

TypeError: Error #1009: Cannot access a property or method of a null object reference.
	at vars$cinit()
	at global$init()[C:\Documents and Settings\Me\Desktop\Breakout\Flash Stuff\vars.as:11]
	at Breakout_fla::MainTimeline/frame1()[Breakout8_fla.MainTimeline::frame1:136]

A note on organization: I have code that covers the first three frames on one layer, and code that is for each of those frames individually on another layer. It looks like the error is pointing to line 136 (if I'm reading that right), but neither layer goes up that high, which led me to think it's combining the total lines of code. That also means I have no idea where to look for that part of the error.

main file, layer 1 (covers frames 1-3):

stop();

import flash.display.*;
import flash.events.*;

import blueBlock;
import lavenderBlock;
import redOrangeBlock;
import blackBlock;

import vars;

addEventListener(Event.ENTER_FRAME, movement);
addEventListener(KeyboardEvent.KEY_DOWN, checkKeys);
addEventListener(KeyboardEvent.KEY_UP, keyUps);
addEventListener(Event.ENTER_FRAME, pacBallFly);

function checkKeys(event:KeyboardEvent):void
{
	if (event.keyCode == 39)
	{
		vars.varRight = true;
	}
	if (event.keyCode == 37)
	{
		vars.varLeft = true;
	}
}

function keyUps(event:KeyboardEvent):void
{
	if (event.keyCode == 39)
	{
		event.keyCode = 0;
		vars.varRight = false;
	}
	if (event.keyCode == 37)
	{
		event.keyCode = 0;
		vars.varLeft = false;
	}
}

function movement(event:Event):void
{
	if (vars.varRight == true)
	{
		pacPaddle.x += vars.xspeed;
	}
	if (vars.varLeft == true)
	{
		pacPaddle.x -= vars.xspeed;
	}
	if (pacPaddle.hitTestObject(pacLeft))
	{
		pacPaddle.x += vars.xspeed;
	}
	if (pacPaddle.hitTestObject(pacRight))
	{
		pacPaddle.x -= vars.xspeed;
	}
}

function pacBallFly(event:Event):void
{
	pacBall.y -= vars.pacBallSpeedY;
	pacBall.x += vars.pacBallSpeedX;
	if (pacBall.hitTestObject(pacRight))
	{
		pacBall.x = 620;
		vars.pacBallSpeedX *= -1;
	}
	if (pacBall.hitTestObject(pacTop))
	{
		pacBall.y = 20;
		vars.pacBallSpeedY *= -1;
	}
	if (pacBall.hitTestObject(pacLeft))
	{
		pacBall.x = 20;
		vars.pacBallSpeedX *= -1;
	}
	if (pacBall.hitTestObject(pacPaddle))
	{
		pacBallAngle();
	}
	if (pacBall.hitTestObject(pacBottom))
	{
		vars.pacBallSpeedY *= -1;
		pacBall.x = 320;
		pacBall.y = 480;
		vars.balls --;
		if (vars.balls == 0)
		{
			removeEventListener(Event.ENTER_FRAME, pacBallFly);
			removeEventListener(Event.ENTER_FRAME, movement);
			removeEventListener(KeyboardEvent.KEY_DOWN, checkKeys);
			removeEventListener(KeyboardEvent.KEY_UP, keyUps);
			removeEventListener(Event.ENTER_FRAME, checkLvl);
			vars.gameOver = true;
			gotoAndStop('pacGameOver');
		}
	}
}

function pacBallAngle():void
{
	vars.pacBallSpeedX = vars.pacAnglePercent * 10;
	vars.pacBallSpeedY *= -1;
}

main file, layer 2 (covers only frame 1):

addEventListener(Event.ENTER_FRAME, checkLvl);

var pacLvl1:Array = new Array();

pacLvl1.push(new Array());
var pac1Row1:Array = new Array([0,0,0,0,0,1,1,1,1,0,0,0,0,0]);
var pac1Row2:Array = new Array([0,0,0,1,1,1,1,1,1,1,1,0,0,0]);
var pac1Row3:Array = new Array([0,0,1,1,1,1,1,1,1,1,1,1,0,0]);
var pac1Row4:Array = new Array([0,1,1,1,1,1,1,1,1,1,1,1,1,0]);
var pac1Row5:Array = new Array([0,1,1,1,2,2,1,1,2,2,1,1,1,0]);
var pac1Row6:Array = new Array([0,1,1,1,2,2,1,1,2,2,1,1,1,0]);
var pac1Row7:Array = new Array([1,1,1,1,1,1,1,1,1,1,1,1,1,1]);
var pac1Row8:Array = new Array([1,1,1,1,1,1,1,1,1,1,1,1,1,1]);
var pac1Row9:Array = new Array([1,1,2,2,1,1,2,2,1,1,2,2,1,1]);
var pac1Row10:Array = new Array([1,2,1,1,2,2,1,1,2,2,1,1,2,1]);
var pac1Row11:Array = new Array([1,1,1,1,1,1,1,1,1,1,1,1,1,1]);
var pac1Row12:Array = new Array([1,1,0,1,1,1,0,0,1,1,1,0,1,1]);
var pac1Row13:Array = new Array([1,0,0,0,1,1,0,0,1,1,0,0,0,1]);

pacLvl1[0].push(pac1Row1, pac1Row2, pac1Row3, pac1Row4, pac1Row5, pac1Row6, pac1Row7, pac1Row8, pac1Row9, pac1Row10, pac1Row11, pac1Row12, pac1Row13);

for (var k in pacLvl1[0])
{
	for (var i in pacLvl1[0][k][0])
	{
		if (pacLvl1[0][k][0][i] == vars.blue)
		{
			var block:blueBlock = new blueBlock();
			block.x = (i * (20)) + 190;
			block.y = (k * (20)) + 50;
			addChild(block);
		}
		else if (pacLvl1[0][k][0][i] == vars.lavender)
		{
			var block2:lavenderBlock = new lavenderBlock();
			block2.x = (i * (20)) + 190;
			block2.y = (k * (20)) + 50;
			addChild(block2);
		}
	}
}

function checkLvl(e:Event):void
{
	if(vars.blockAmt == 0)
	{
		gotoAndStop(nextFrame);
		removeEventListener(Event.ENTER_FRAME, checkLvl);
	}
}

blueBlock.as

package
{
	import flash.display.*;
	import flash.events.*;
	
	import vars;
	
	public class blueBlock extends MovieClip
	{
		public static var _root:MovieClip;
		public function blueBlock()
		{
			addEventListener(Event.ADDED, beginClass);
			addEventListener(Event.ENTER_FRAME, enterFrameEvents);
		}
		private function beginClass(event:Event):void
		{
			_root = MovieClip(root);
			vars.blockAmt ++;
		}
		private function enterFrameEvents(event:Event):void
		{
			if(vars.gameOver)
			{
				parent.removeChild(this);
				removeEventListener(Event.ENTER_FRAME, enterFrameEvents);
			}
			else if (this.hitTestObject(_root.pacBall))
			{
				vars.pacBallSpeedY *= -1;
				if ((_root.pacBall.x >= (x +10)) || (_root.pacBall.x <= (x -10)))
				{
					vars.pacBallSpeedY *= -1;
					vars.pacBallSpeedX *= -1;
				}
				parent.removeChild(this);
				vars.blockAmt --;
				removeEventListener(Event.ENTER_FRAME, enterFrameEvents);
			}
		}
	}
}

vars.as

package
{
	import flash.display.*;
	import flash.events.*;
	
	import blueBlock;
	import lavenderBlock;
	import redOrangeBlock;
	import blackBlock;
		
	public class vars
	{
		public static var varRight:Boolean = false;
		public static var varLeft:Boolean = false;
		public static var xspeed:Number = 10;
		public static var blockAmt:int = 0;
		public static var gameOver:Boolean = false;
		public static var pacBallSpeedX:Number = 5;
		public static var pacBallSpeedY:Number = 5;
		public static var balls:int = 3;
		public static var pacBallPosition:Number = blueBlock._root.pacBall.x - blueBlock._root.pacPaddle.x;
		public static var pacAnglePercent:Number = (pacBallPosition / (blueBlock._root.pacPaddle.width - blueBlock._root.pacBall.width));
		public static var blue:int = 1;
		public static var lavender:int = 2;
		public static var redOrange:int = 3;
		public static var black:int = 4;
	}
}

Sorry for the long post.

We shall not cease from exploration, and the end of all our exploring will be to arrive where we started and know the place for the first time -TS Eliot-


None

goldleader23

Reply To Post Reply & Quote

Posted at: 7/7/09 01:24 AM

goldleader23 LIGHT LEVEL 20

Sign-Up: 08/29/08

Posts: 31

Nobody, huh?

We shall not cease from exploration, and the end of all our exploring will be to arrive where we started and know the place for the first time -TS Eliot-


None

billowillo

Reply To Post Reply & Quote

Posted at: 7/7/09 01:30 AM

billowillo DARK LEVEL 15

Sign-Up: 04/21/08

Posts: 691

just make sure that you remove everything that is trying to acess another thing properly, those errors are always the hardest to trouble shoot. try commenting out some parts of the code until it works to help pin point it.

BBS Signature

None

Woadraiders

Reply To Post Reply & Quote

Posted at: 7/7/09 02:09 AM

Woadraiders DARK LEVEL 11

Sign-Up: 11/11/07

Posts: 561

The error seems pretty obvious to me.

TypeError: Error #1009: Cannot access a property or method of a null object reference.
at vars$cinit()
at global$init()[C:\Documents and Settings\Me\Desktop\Breakout\Flash Stuff\vars.as:11]
at Breakout_fla::MainTimeline/frame1()[Brea kout8_fla.MainTimeline::frame1:136]

Notice how it says "at vars$cint()

That means when the vars class is initiated. And since you don't have a constructor, and there are no instances of the var class, it must mean as soon as it's imported or maybe even when the flash is compiled, and it doesn't have to be imported at all.

So then it's not finding blueBlock._root.pacball or whatever you wrote, it's null, it hasn't been defined yet.

And I'm not sure when or where you add to blueBlock._root. All I know is it thinks that it is null, so you need to change something. I recommend having a static Init() function or just setting those two properties. You would do either of these AFTER you make sure to add blueBlock._root.pacball.

Hopefully that helps? I don't recommend setting variables with object references straight out of the class definitions ever, just do it inside of an init function so you don't have to assume.

Mibbygames.com
panterA

BBS Signature

All times are Eastern Standard Time (GMT -5) | Current Time: 06:26 AM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!