0
votes

I'm getting the above error on this code when I try to make a bulldozer appear on the stage. It's driving me crazy, and I don't know why it's happening. I have the bulldozer clip in my library, and it seems to be correctly defined. Any help would be greatly appreciated.

import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.ui.Mouse;

//START SCREEN
var startScreen: MovieClip;
var bulldozer: MovieClip;

startClick.addEventListener(MouseEvent.CLICK, startGame);


function startGame(event: MouseEvent): void {
    startScreen.parent.removeChild(startScreen);
    startClick.parent.removeChild(startClick);
    addChild(bulldozer);
    var enemyGenerator: Number = Math.random();
    if (enemyGenerator >= 0.8) {
        bulldozer.x = stage.x = 150;
        bulldozer.y = stage.y = 150;
    } else if (enemyGenerator >= 0.6) {
        bulldozer.x = stage.x = 250;
        bulldozer.y = stage.y = 250;
    } else if (enemyGenerator >= 0.4) {
        bulldozer.x = stage.x = 350;
        bulldozer.y = stage.y = 350;
    } else if (enemyGenerator >= 0.2) {
        bulldozer.x = stage.x = 400;
        bulldozer.y = stage.y = 400;
    } else {
        bulldozer.x = stage.x = 450;
        bulldozer.y = stage.y = 450;
    }

    startClick.removeEventListener(MouseEvent.CLICK, startGame);
}
2
Also: what is startClick? Also: please post full errors when your question involves errors.FlavorScape

2 Answers

1
votes

You have to create an instance of the bulldozer!

var bulldozer: MovieClip = new MovieClip();

The above code will create a class-level instance variable accessible from any function within this class. If you say

public var MovieClip = new MovieClip();

Then you will be able to access it from outside this class. Rinse and repeat for startScreen.

There is a difference between null and undefined. This is a basic precept of any OO language. Your object bulldozer (the child you're trying to add) is null at this point because it has not been constructed with new. If it were undefined, that would mean var startScreen: MovieClip; was missing.

Please look into constructors and instance variables.

0
votes

You have the following problems:

You need to create an instance of bulldozer as FlavorScape stated above.

This line is defining bulldozer as a variable, but that variable is empty, or "null":

var bulldozer: MovieClip;

You cannot add a null variable to the stage, so it results in an error (#2007) unless you create an instance like this:

var bulldozer: MovieClip = new MovieClip();

The second error (#2071) is a result of trying to set x and y properties of stage. The stage never has x or y properties, so lines like this:

bulldozer.x = stage.x = 150;
bulldozer.y = stage.y = 150;

should be

bulldozer.x = 150;
bulldozer.y = 150;