开发者

Custom Actionscript 3 EVENT "Super" Function Not Working

开发者 https://www.devze.com 2022-12-20 10:52 出处:网络
This is a super simple event. Why it is not working is making me go crazy. This is in my AsciiArt class:

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 A开发者_如何学运维rtEvent()
        {
            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 :(


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


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()); 
      }


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);
    }

}

}

`

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号