PDA

View Full Version : Messaging in an AS3 Game



InLiquidWonder
April 10th, 2009, 07:10 PM
Hey folks,

I keep getting weird errors for this series of functions. Essentially, I have a tutorial section of a game where I want to display movieclips in succession (instruction screens), so I have one movieclip being instantiated, then triggering the next and removing itself when it gets clicked on. Currently, the code launches all three instruction windows at once, and then throws an error and creates more screens when you click on it.

This is part of the main game controlling class, called JenGrierThesisHunter, hence the occasional self-referencing I did in the code to get it to break less. I don't know if this is correct, though, but it's the closest I've gotten to working it out.



public function onIntro(event:MouseEvent)
{
introButton.removeEventListener(MouseEvent.CLICK, onIntro);
var intro1:IntroMessage1 = new IntroMessage1();
intro1.y = 25;
intro1.x = 50;
addChild(intro1);
addEventListener(MouseEvent.CLICK, message1Handler);
}

public function message1Handler(event:MouseEvent)
{
var intro2:IntroMessage2 = new IntroMessage2();
intro2.y = 25;
intro2.x = 50;
addChild(intro2);
removeEventListener(MouseEvent.CLICK, message2Handler);
removeChild(JenGrierThesisHunter.main.intro1);
}

public function message2Handler(event:MouseEvent)
{
var intro3:IntroMessage3 = new IntroMessage3();
intro3.y = 25;
intro3.x = 50;
addChild(intro3);
removeEventListener(MouseEvent.CLICK, message2Handler);
removeChild(JenGrierThesisHunter.main.intro2);
}

public function message3Handler(event:MouseEvent)
{
removeEventListener(MouseEvent.CLICK, message3Handler);
removeChild(JenGrierThesisHunter.main.intro3);
}

The error that comes up with is "ReferenceError: Error #1069: Property intro1 not found on JenGrierThesisHunter and there is no default value.
at JenGrierThesisHunter/message1Handler()"

Any suggestions/advice appreciated! Thanks!

InLiquidWonder
April 10th, 2009, 08:45 PM
OK, so instead of trying to start up multiple message movieclips, I've put them all in one clip and will control the frame that the movie is on with each click. I've tried this so far, but it's not working:



public function onIntro(event:MouseEvent)
{
playNowButton.visible = false;
introButton.visible = false;
titleText.visible = false;

introButton.removeEventListener(MouseEvent.CLICK, onIntro);

var introText:IntroMessage = new IntroMessage();
introText.y = 25;
introText.x = 50;
addChild(introText);
introText.addEventListener(MouseEvent.CLICK, introTextHandler);
}

public function introTextHandler(event:MouseEvent)
{
if (introText.currentLabel == "text 1") {
introText.gotoAndPlay("text 2");
//removeEventListener(MouseEvent.CLICK, main.intro1.message1Handler);
//intro2.addEventListener(MouseEvent.CLICK, message2Handler);
}
if (introText.currentLabel == "text 2") {
introText.gotoAndPlay("text 3");
}
if (introText.currentLabel == "text 3") {
introText.removeEventListener(MouseEvent.CLICK, introTextHandler);
}
}


If I remove introText.etc... from the beginning of each of those statements, it runs but doesn't move to the second clip. With them in place, I get this error:

1120: Access of undefined property introText.
... for every line in introTextHandler().


All suggestions appreciated! If one way is better than the other, I'd love to know. Thanks!