views:

268

answers:

3

This is a super simple event. Why it is not working is making me go crazy.

This is in my AsciiArt class:

dispatchEvent(new ArtEvent());

That fires this very simple event class:

package 
{
    import flash.events.*;

    public class ArtEvent extends Event
    {
        public static const DONE_NOW = "done";

        public function ArtEvent()
        {
            super(DONE_NOW);
            trace("constructed");
        }
    }
}

I know it's firing because in my .fla where I'm instantiating an AsciiArt object it will trace "constructed" upon completion with this code:

var art:AsciiArt = new AsciiArt(bitMapData);
addChild(art)

to which I of course attach my event listener (which is what seems to not be doing anything.

art.addEventListener(ArtEvent.DONE_NOW, function():void{ trace("hi"); });

So, in summary, "constructed" will trace. But "hi" will not.

Any ideas? Thanks -J

edit - (catching the correct event type and matching num of arguments)

art.addEventListener(ArtEvent.DONE_NOW, function(event:ArtEvent) {
    trace("hi"); 
});

Also does not work :(

+2  A: 

If the class posted on your block on the code is the actual context then there are 2 problems:

  1. you never dispatch the Event, so the handler can never be called
  2. ActionScript is single-threaded. If you do a big loop, then your programm will simply freeze until the loop is done. if you want the operation to "run in the backround", you need to split it into chunks and distribute execution over several frames, and then dispatch the event after last chunk has been executed.

greetz

back2dos

back2dos
@back2dos. Thanks for the response. I don't know if you noticed (it's a small detail) but I'd written the that the dispatch event WILL exist at the end of the loop. It wasn't in my code yet, because it didn't work. I like your answer, but I'm not a big fan of using frames. So, what I did was add an ENTER_FRAME event listener to the end of the loop that would call up my dispatch event function "fireFunction()"... I'll give you the green arrow in that you are right about the multiple frames.
Jascha
A: 

As back2dos pointed out (and bhups hinted at), Flash is "single threaded" therefor there was no instantiation period for my ArtEvent. To solve this (without adding ugly frames to my script).. at the end of my loop I added:

if(yPos<imgData.height){ // if yPos is less than the height of the image, start over again
            xPos = 0;
            yPos +=detail;
            makeArt();
        }else{ 
            addEventListener(Event.ENTER_FRAME, fireEvent);
        }
     }
      public function fireEvent(e:Event):void
      {
         removeEventListener(Event.ENTER_FRAME, fireEvent);
         dispatchEvent(new ArtEvent()); 
      }
Jascha
this won't solve the problem, I'm afraid. you'll still run **the whole loop** in one frame, **then** register the handler, and you'll trigger your `ArtEvent` one frame later, when `fireEvent` is triggered ... you need to call `makeArt` each frame, and when your break condition turns true, unregister `makeArt` and dispatch your `ArtEvent` ... let me know if you have problems ...
back2dos
@back2dos I tell you what, it seems to be working just fine. you can see an example here... http://www.sugarpillfactory.com/projects/asciiartGIF.swf (the whole purpose was to create an ascii animated gif)
Jascha
A: 

Main Class ` package { import flash.display.Sprite;

public class Test extends Sprite
{
    public function Test()
    {
        var art:AsciiArt = new AsciiArt();
        art.addEventListener(ArtEvent.DONE_NOW, onArtDone, false, 0, true);
        addChild(art);
    }

    public function onArtDone(event:ArtEvent):void{

        trace(event);
    }
}

}

`

AsciiArt `package { import flash.display.MovieClip; import flash.events.Event;

public class AsciiArt extends MovieClip
{
    public function AsciiArt()
    {
        super();
        addEventListener(Event.ADDED_TO_STAGE, createEvent, false, 0, true);
    }

    private function createEvent(event:Event):void{
        dispatchEvent(new ArtEvent(ArtEvent.DONE_NOW))

    }
}

} `

Event `package { import flash.events.Event;

public class ArtEvent extends Event
{
    public static const DONE_NOW:String = "done";
    public function ArtEvent(type:String, bubbles:Boolean=false, cancelable:Boolean=false)
    {
        super(type, bubbles, cancelable);
    }

}

}

`

Alexey