Skip to main content
Participant
August 21, 2015
Question

Accessing Arrays w/ EventListener??

  • August 21, 2015
  • 1 reply
  • 378 views

- Listening for a certain array value in order to change frames -

hello, it's me again

I am trying to implement this typewriter effect for my dialogues, but I don't know how to access the array in order to know when a conversation is over. Ideally I'd like to create a conversation/scene class, but for now I'm just trying to be able to get out of a conversation.

my main class is:

package {

  import flash.display.MovieClip;

  public class Main extends MovieClip {

  // TEXT BOX CONSTRUCTOR

  public function Main() {

  var textBlocks:Array=new Array(["pic1","what's up?"],["pic3","oh!"],["pic3","nothing."],["pic4","I'm here too"],["pic2","me too"],["pic4","what should we do"],["pic3","nothing..."],["pic1","ok"]);

  textBox.textBlocks=textBlocks;

  textBox.startText();

  }

  }

}

//////////////////////////

my text box script is:

package {

  import flash.events.Event;

  import flash.events.MouseEvent;

  import flash.display.MovieClip;

  import flash.media.Sound;

  public class TextBox extends MovieClip {

  private const CHARA:int=0;

  private const TEXT:int=1;

  private var _currentTextBlockIndex:int=0;

  private var _currentTextBlock:String;

  private var _textBlocks:Array;

  // CONSTRUCTOR

  public function TextBox() {

  }

  public function set textBlocks(txt:Array):void {

  _textBlocks=txt;

  }

  public function startText():void {

  _currentTextBlock=_textBlocks[_currentTextBlockIndex][TEXT];

  characterIcon.gotoAndStop(_textBlocks[_currentTextBlockIndex][CHARA]);

  addEventListener(Event.ENTER_FRAME, updateText);

  addEventListener(MouseEvent.CLICK, fillText);

  }

  private function updateText(e:Event):void {

  if (txt.text.length<_currentTextBlock.length) {

  txt.text=_currentTextBlock.substr(0,txt.text.length+1);

  } else {

  removeEventListener(Event.ENTER_FRAME, updateText);

  fillText();

  }

  }

  private function fillText(e:MouseEvent = null):void {

  txt.text=_currentTextBlock;

  if (_currentTextBlockIndex<_textBlocks.length-1) {

  addEventListener(MouseEvent.CLICK, nextTextBlock);

  }

  }

  private function nextTextBlock(e:MouseEvent):void {

  removeEventListener(MouseEvent.CLICK, nextTextBlock);

  txt.text="";// clear the text

  _currentTextBlockIndex++;

  _currentTextBlock=_textBlocks[_currentTextBlockIndex][TEXT];// set the text

  characterIcon.gotoAndStop(_textBlocks[_currentTextBlockIndex][CHARA]);// set the character icon

  addEventListener(Event.ENTER_FRAME, updateText);// start updating the text

  addEventListener(MouseEvent.CLICK, fillText);

  }

  }

}

//////////////////

From here, I thought I could do something like

private function endConvo(e:Event):void {

  if (textBlocks[8]) {

  removeEventListener(MouseEvent.CLICK, nextTextBlock);

  addEventListener(MouseEvent.CLICK, nextConvo);

  }

  }

public function nextConvo(e:MouseEvent = null):void {

  gotoAndStop("end");

  }

so that at the end of the 8th array, which is the last array in the conversation, that i would remove the nextTextBlock event listener and put in one for going to the next frame, but it doesn't work. I'm still trying to get a hang on working outside of the timeline, and furthermore working with as3 instead of as2.

needless to say, the conversation upon ending will not go to the next frame.

thank you so much!!!!

This topic has been closed for replies.

1 reply

Ned Murphy
Legend
August 22, 2015

It is a bit hard for me to decipher, but it looks like you might be adding the same listeners multiple times, and never removing them when you add the next.  You should see about limiting adding them only once or remove them when another should be working.

As far as ending and getting to the next convo, I think it would require something along the lines of changing your fillText function

  private function fillText(e:MouseEvent = null):void {

       txt.text=_currentTextBlock;

       if (_currentTextBlockIndex<_textBlocks.length-1) {

           addEventListener(MouseEvent.CLICK, nextTextBlock);

       } else {

           endConvo();   // remove the evt:Event argument from that function

       }

  }

Orion-11Author
Participant
August 22, 2015

Thank you! Though now it says

else {

endConvo();

}

Gives me the error "1136 incorrect number of arguments"

??