The code below generates an error any idea?
thanks
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at pang/checkForheroHits()
PHP Code:
public function checkForheroHits(event:Event) {
for (var ballNum:int=balls.length-1;ballNum>=0;ballNum--) {
if (myhero.hitTestObject(balls[ballNum])) {
//myhero.heroHit();
//protectedhero();
//balls[ballNum].width=50;
//removeball(balls[ballNum]);
//ball = new Ball()
var hisnum: int=ballNum+4;
Is the checkForheroHits method sitting in an ActionScript class?
You need to provide it with a reference to the Stage, either by having this class be the main Document Class, or by passing a reference to the Stage in the constructor.
If you don't have a "base" class, I would just cheat a little and send a reference to the stage.
From the main timeline:
Code:
var myClass = new MyClass(this.stage);
this.addChild(myClass);
And then from your custom class:
Code:
import flash.display.Stage;
var stageReference:Stage;
public function MyClass(stageReference:Stage) {
this.stageReference = stageReference;
}
...
public function checkForheroHits(event:Event) {
...
this.stageReference.addChild(ball[hisnum]);
...
}
public class mygame extends MovieClip {
private var balls:Array;
private var ball1:Ball;
private var ball2:Ball;
private var ball3:Ball;
private var ball4:Ball;
private var ball5:Ball;
private var ball6:Ball;
private var ball:Ball;
ReferenceError: Error #1069: Property 4 not found on Ball and there is no default value.
at pang/checkForheroHits()
PHP Code:
public function checkForheroHits(event:Event) {
for (ballNum=balls.length-1;ballNum>=0;ballNum--) {
if (myhero.hitTestObject(balls[ballNum])) {
//myhero.heroHit();
protectedhero();
//balls[ballNum].width=50;
//removeball(balls[ballNum]);
ball = new Ball()
var hisnum: int=ballNum+4;
Don't quit just yet. You're learning really quick and your game is coming along well!
The issue is that you're sometimes adding balls that don't exist to the Stage in the checkForharpoonhits function:
Code:
public function checkForharpoonHits(event:Event) {
if (weapon != null) {
if (weapon.hitTestObject(ball1)) {
trace("was hit");
addChild(ball5);
...
In your startPang function, you never initialize the the ball5 object, which is why you get an error when you try to add it.
Instead of manually instantiating multiple Ball objects, why not run a loop to declare several at once?
Code:
var numberOfBalls = 5;
// create balls and add them to the stage
balls = new Array();
for (var i=0; i<numberOfBalls; i++) {
ball = new Ball();
ball.name = "ball" + (i + 1);
ball.x = (i + 1) * 50 + Math.random() * 100;
ball.y = (i + 1) * 25 + Math.random() * 100;
if (Math.random() * 100 > 50)
ball.gotoAndStop(2);
balls.push(ball);
this.addChild(ball);
}
This code replaces the chunk of code in startPang that manually instantiates those balls. You can change how many Ball objects are instantiated with the numberOfBalls variable, which is a temporary way to do things. Later on, you should make the Ball objects spawn more dynamically, which I'm sure you were planning.
You'll see that I randomly shift the ball's position around using the current index of the loop, an arbitrary number (50 and 25 for x and y, respectively), and some random math. You can change how this works and tweak the values to your liking.
I also run a check so that 50% of the time, the ball becomes a shield when instantiated.
All of these Ball objects sit in the balls array for later use.
This is the new checkForharpoonHits:
Code:
// check for collisions
public function checkForharpoonHits(event:Event) {
if (weapon != null) {
for (var i=0; i<balls.length; i++) {
if (weapon.hitTestObject(balls[i])) {
if (balls[i] != null)
balls[i].deleteballx();
}
}
}
}
You'll notice that I loop through the balls array and test each Ball instance individually. This is not the best way to do this, because it can become quite CPU-intensive as this loop will run EVERY TIME A FRAME IS HIT. A better solution should be researched for later on, but for now, it works. And the best part is you don't have to do any of the hit tests manually. It loops through all the Ball objects and checks for you (whereas before you were only checking for ball1).
I'm getting error messages when the balls are deleted. I think it's complaining about the parent.removeChild call in the deleteballx function, but I didn't look too much into it.
This should put you on the right track, hopefully. =)
There's a list of things to try every time you get Error #1009: Cannot access a property or method of a null object reference.
I believe that the error above, is basically flash saying Ahhhh! I can't delete that! It doesn't exist!
Well, usually flash can have something, you're just not telling it to look in the right place. If you get that error, always try stage.removeChild(myMovie). Using the stage property sometimes works, for whatever reason. also, you could try whatever movieclip the balls are in, or to make things even easier, make a holder that holds all of the balls.
Hope that helps a bit,
XenElement
In the process of designing a quirky little game engine called gulp. Check out it's progress below: @ my blog.