Reputation: 2285
I'm creating game in which user is given a map and five flags. User must drag flags, one by one to the countries, so after creating MovieClips that represent flags, I started creating code for them. That worked for France, but after I copied it to Germany and changed everything using CTRL+F ('france' to 'germany' and 'France' to 'Germany') it didn't. Well, actually it did, but there is some error. That's funny, coz German flag does absolutely everything it should, and still there is an error in the output. Here, that's the code:
package {
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class flagGermany extends MovieClip {
public var Name:String="Germany";
public var atX:uint;
public var atY:uint;
public var wasGuessed:Boolean=false;
public var isPressed:Boolean=false;
public function flagGermany() {
trace(this);
this.addEventListener(MouseEvent.MOUSE_DOWN, dragEnable);
this.addEventListener(MouseEvent.MOUSE_UP, dragDisable);
stage.addEventListener(MouseEvent.MOUSE_UP, dragDisable);
}
function dragEnable(e:MouseEvent) {
isPressed=true;
trace(isPressed);
atX=stage.mouseX-this.x;
atY=stage.mouseY-this.y;
this.alpha=0.3;
this.mouseEnabled=false;
stage.addEventListener(MouseEvent.MOUSE_MOVE, moveFlag);
}
function dragDisable(e:MouseEvent) {
if(isPressed==true) {
if(wasGuessed==false) {
this.alpha=1;
this.mouseEnabled=true;
}
trace("invoked by "+e.target);
if(MovieClip(this.root).germany.currentFrame==2) {
MovieClip(this.root).germany.gotoAndStop(3);
MovieClip(this.root).germany.removeEventListener(MouseEvent.ROLL_OVER, MovieClip(this.root).hightlight);
MovieClip(this.root).germany.removeEventListener(MouseEvent.ROLL_OUT, MovieClip(this.root).unhightlight);
correct();
}
stage.removeEventListener(MouseEvent.MOUSE_MOVE, moveFlag);
isPressed=false;
trace(isPressed);
}
}
function moveFlag(e:MouseEvent) {
this.x=stage.mouseX-atX;
this.y=stage.mouseY-atY;
}
function correct() {
this.removeEventListener(MouseEvent.MOUSE_DOWN, dragEnable);
this.removeEventListener(MouseEvent.MOUSE_UP, dragDisable);
this.alpha=0;
this.mouseEnabled=false;
this.wasGuessed=true;
}
}
}
And here's an error:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at flagGermany()
at flash.display::Sprite/constructChildren()
at flash.display::Sprite()
at flash.display::MovieClip()
at EM()
EM
is main file's Document Class. flagGermany() a constructor for German flag. Both flags are created on second frame of the movie. What's the matter with my code?!
Upvotes: 0
Views: 202
Reputation: 90776
Make sure stage
is available before adding event listeners to it:
public function flagGermany() {
/* .. */
addEventListener("addedToStage", onAddedToStage);
}
private function onAddedToStage(event:*):void {
stage.addEventListener(MouseEvent.MOUSE_UP, dragDisable);
}
Upvotes: 1
Reputation: 1165
I believe for stage to non-null, the MovieClip has to be addChild'ed first. I would check out why is France getting stage and Germany not.
Upvotes: 2