Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Simple dispatch event doesnt seem to work

New Here ,
Mar 28, 2011 Mar 28, 2011

I've got 2 AS3 files going here, the document file "Main" and a "Fence" file...

Fence code

package  {

import flash.display.MovieClip;
import flash.events.Event;


public class Fence extends MovieClip {
 
 
  public function Fence() {
   addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
  }
  private function onAddedToStage(event:Event) {
   dispatchEvent(new Event("fenceAdded", true));
   trace("ping");
   removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
  }
}

Then theres the Main code

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

public class Main extends MovieClip {
 
  public function Main () {  
   addEventListener(Event.ENTER_FRAME, onEnterFrame);
   stage.addEventListener("fenceAdded", onFenceAdded);
  }
  private function onFenceAdded (event:Event) {
   trace("pong");
  }

}
}

For some reason the ping comes up, but the pong doesn't, it's starting to bug me now that the "onFenceAdded" wont even work, what have I missed? Is there some way to check if Flash is compiling correctly? I often find these weird problems where I'm sure I've done it all correct, but when I try to compile and run a swf it wont work!

TOPICS
ActionScript
1.5K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 28, 2011 Mar 28, 2011

check that your Main class is being used.  ie, use a trace() that does execute.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 28, 2011 Mar 28, 2011

I added trace("pang"); to public function Main and it worked. Still no pong tho =(

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 28, 2011 Mar 28, 2011

where's your new Fence() constructor?  comment out your onEnterFrame to make sure that's not causing a problem.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 28, 2011 Mar 28, 2011

It's object orientated, all files are in the same folder.

Test.fla

(with Main as document class and has instants of a library item linked with Fence)

Main.as

Fence.as (putting a trace works in the class)

Iv just tried removing other elements, even though it seems I've simplified it down to call a dispatch event in the most basic way, it still doesn't seem to want to work,,,

Can it do any good to try reinstalling Flash at this rate? Because I'm sure I've had other problems lately where I've checked over and over again, and the code should be perfectly right yet it just refuses to work.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 28, 2011 Mar 28, 2011

no, don't reinstall flash.  you've almost certainly done something wrong especially in view of your last answer.

where is the constructor for your Fence instance.  ie, somewhere you have something like:

var f:Fence=new Fence();

something.addChild(f);

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 28, 2011 Mar 28, 2011

In my limited experience with dispatching events (et al), I thought the event listener would be assigned to the instance that will dispatch the event. Meaning...

....addEventListener("fenceAdded", onFenceAdded);

would be added to the Fence instance (once it exists) rather than the stage

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 28, 2011 Mar 28, 2011

" I thought the event listener would be assigned to the instance that will dispatch the event."

Events dispatched by display objects propagate through entire display list. If event's bubbling phase is set to true - event can be heard anywhere on display list including top level (stage or root) no matter how deep in the display list hierarchy it was dispatched given listeners are added. The code in original post is:

dispatchEvent(new Event("fenceAdded", true));

true sets event to bubble.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 28, 2011 Mar 28, 2011

There are instances of the fence object on the stage already, so I figure I shouldnt need to do addchilds if their already on the stage.

Edit: Oh wow... for some reason it worked when I put an eventlistener for the dispatched event in another class,,, is the document class not allowed to listen for dispatched events or something?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 28, 2011 Mar 28, 2011

that event will bubble up to the stage in any class that can reference the stage.  in particular, there should be no problem having the stage listen for the event in your document class.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 28, 2011 Mar 28, 2011

"There are instances of the fence object on the stage already, so I figure I shouldn't need to do addchilds if their already on the stage."

Timeline is handled differently. Document class wraps what was added on timeline (code and objects) at authoring before constructor is called. Thus, because event listener is added AFTER fence was added to display list - it, naturally, is not dispatched/heard, again, because addition to stage has occurred earlier.

To check it out, given that

1. instance of Fence is called fence

2. Fence writes trace("ping"); in added to stage event listener

The code:

public function Main () { 
     trace("Main constructor's first line");
     trace("fence =", fence);
     trace("Number of children =", this.numChildren);
}

traces:

ping
Main constructor's first line
fence = [object Fence]
Number of children = 1

Traces demonstrate the sequence of events in the context of document class vs timeline, most notably that Fence instantiation and addition to display list occurred before Main constructor was called - hence it is available as both instance and child.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 28, 2011 Mar 28, 2011
LATEST

Just forgot to mention. There is undocumented MainTimeline class. If you remove document class and place the following code:

import flash.utils.describeType;
var xml:XML = describeType(this);
trace(xml);

You will see result similar to the xml below - it says that type is MainTimeline.

If you assign Main to it - it will state that type is MovieClip.

In both cases you will see a line:

<variable name="fence" type="timelineCode::Fence"/>

Here is an output without document class:

<type name="stageEvent_fla::MainTimeline" base="flash.display::MovieClip" isDynamic="true" isFinal="false" isStatic="false">
  <extendsClass type="flash.display::MovieClip"/>
  <extendsClass type="flash.display::Sprite"/>
  <extendsClass type="flash.display::DisplayObjectContainer"/>
  <extendsClass type="flash.display::InteractiveObject"/>
  <extendsClass type="flash.display::DisplayObject"/>
  <extendsClass type="flash.events::EventDispatcher"/>
  <extendsClass type="Object"/>
  <implementsInterface type="flash.events::IEventDispatcher"/>
  <implementsInterface type="flash.display::IBitmapDrawable"/>
  <variable name="xml" type="XML"/>
  <variable name="fence" type="timelineCode::Fence"/>
  <method name="addFrameScript" declaredBy="flash.display::MovieClip" returnType="void">
    <metadata name="Inspectable">
      <arg key="environment" value="none"/>
    </metadata>
  </method>
  <method name="nextFrame" declaredBy="flash.display::MovieClip" returnType="void"/>
  <method name="play" declaredBy="flash.display::MovieClip" returnType="void"/>
  <accessor name="trackAsMenu" access="readwrite" type="Boolean" declaredBy="flash.display::MovieClip"/>
  <method name="gotoAndPlay" declaredBy="flash.display::MovieClip" returnType="void">
    <parameter index="1" type="Object" optional="false"/>
    <parameter index="2" type="String" optional="true"/>
  </method>
  <method name="gotoAndStop" declaredBy="flash.display::MovieClip" returnType="void">
    <parameter index="1" type="Object" optional="false"/>
    <parameter index="2" type="String" optional="true"/>
  </method>
  <accessor name="currentLabels" access="readonly" type="Array" declaredBy="flash.display::MovieClip"/>
  <method name="prevFrame" declaredBy="flash.display::MovieClip" returnType="void"/>
  <accessor name="currentFrame" access="readonly" type="int" declaredBy="flash.display::MovieClip"/>
  <accessor name="currentLabel" access="readonly" type="String" declaredBy="flash.display::MovieClip"/>
  <accessor name="enabled" access="readwrite" type="Boolean" declaredBy="flash.display::MovieClip"/>
  <accessor name="totalFrames" access="readonly" type="int" declaredBy="flash.display::MovieClip"/>
  <accessor name="scenes" access="readonly" type="Array" declaredBy="flash.display::MovieClip"/>
  <method name="stop" declaredBy="flash.display::MovieClip" returnType="void"/>
  <method name="nextScene" declaredBy="flash.display::MovieClip" returnType="void"/>
  <accessor name="framesLoaded" access="readonly" type="int" declaredBy="flash.display::MovieClip"/>
  <accessor name="currentScene" access="readonly" type="flash.display::Scene" declaredBy="flash.display::MovieClip"/>
  <method name="prevScene" declaredBy="flash.display::MovieClip" returnType="void"/>
  <accessor name="graphics" access="readonly" type="flash.display::Graphics" declaredBy="flash.display::Sprite"/>
  <accessor name="dropTarget" access="readonly" type="flash.display::DisplayObject" declaredBy="flash.display::Sprite"/>
  <accessor name="hitArea" access="readwrite" type="flash.display::Sprite" declaredBy="flash.display::Sprite"/>
  <accessor name="useHandCursor" access="readwrite" type="Boolean" declaredBy="flash.display::Sprite"/>
  <accessor name="soundTransform" access="readwrite" type="flash.media::SoundTransform" declaredBy="flash.display::Sprite"/>
  <method name="startDrag" declaredBy="flash.display::Sprite" returnType="void">
    <parameter index="1" type="Boolean" optional="true"/>
    <parameter index="2" type="flash.geom::Rectangle" optional="true"/>
  </method>
  <accessor name="buttonMode" access="readwrite" type="Boolean" declaredBy="flash.display::Sprite"/>
  <method name="stopDrag" declaredBy="flash.display::Sprite" returnType="void"/>
  <method name="swapChildrenAt" declaredBy="flash.display::DisplayObjectContainer" returnType="void">
    <parameter index="1" type="int" optional="false"/>
    <parameter index="2" type="int" optional="false"/>
  </method>
  <accessor name="numChildren" access="readonly" type="int" declaredBy="flash.display::DisplayObjectContainer"/>
  <method name="getChildByName" declaredBy="flash.display::DisplayObjectContainer" returnType="flash.display::DisplayObject">
    <parameter index="1" type="String" optional="false"/>
  </method>
  <accessor name="textSnapshot" access="readonly" type="flash.text::TextSnapshot" declaredBy="flash.display::DisplayObjectContainer"/>
  <accessor name="tabChildren" access="readwrite" type="Boolean" declaredBy="flash.display::DisplayObjectContainer"/>
  <method name="getChildIndex" declaredBy="flash.display::DisplayObjectContainer" returnType="int">
    <parameter index="1" type="flash.display::DisplayObject" optional="false"/>
  </method>
  <method name="getChildAt" declaredBy="flash.display::DisplayObjectContainer" returnType="flash.display::DisplayObject">
    <parameter index="1" type="int" optional="false"/>
  </method>
  <accessor name="mouseChildren" access="readwrite" type="Boolean" declaredBy="flash.display::DisplayObjectContainer"/>
  <method name="contains" declaredBy="flash.display::DisplayObjectContainer" returnType="Boolean">
    <parameter index="1" type="flash.display::DisplayObject" optional="false"/>
  </method>
  <method name="setChildIndex" declaredBy="flash.display::DisplayObjectContainer" returnType="void">
    <parameter index="1" type="flash.display::DisplayObject" optional="false"/>
    <parameter index="2" type="int" optional="false"/>
  </method>
  <method name="removeChildAt" declaredBy="flash.display::DisplayObjectContainer" returnType="flash.display::DisplayObject">
    <parameter index="1" type="int" optional="false"/>
  </method>
  <method name="getObjectsUnderPoint" declaredBy="flash.display::DisplayObjectContainer" returnType="Array">
    <parameter index="1" type="flash.geom::Point" optional="false"/>
  </method>
  <method name="addChildAt" declaredBy="flash.display::DisplayObjectContainer" returnType="flash.display::DisplayObject">
    <parameter index="1" type="flash.display::DisplayObject" optional="false"/>
    <parameter index="2" type="int" optional="false"/>
  </method>
  <method name="swapChildren" declaredBy="flash.display::DisplayObjectContainer" returnType="void">
    <parameter index="1" type="flash.display::DisplayObject" optional="false"/>
    <parameter index="2" type="flash.display::DisplayObject" optional="false"/>
  </method>
  <method name="addChild" declaredBy="flash.display::DisplayObjectContainer" returnType="flash.display::DisplayObject">
    <parameter index="1" type="flash.display::DisplayObject" optional="false"/>
  </method>
  <method name="removeChild" declaredBy="flash.display::DisplayObjectContainer" returnType="flash.display::DisplayObject">
    <parameter index="1" type="flash.display::DisplayObject" optional="false"/>
  </method>
  <method name="areInaccessibleObjectsUnderPoint" declaredBy="flash.display::DisplayObjectContainer" returnType="Boolean">
    <parameter index="1" type="flash.geom::Point" optional="false"/>
  </method>
  <accessor name="doubleClickEnabled" access="readwrite" type="Boolean" declaredBy="flash.display::InteractiveObject"/>
  <accessor name="contextMenu" access="readwrite" type="flash.ui::ContextMenu" declaredBy="flash.display::InteractiveObject"/>
  <accessor name="accessibilityImplementation" access="readwrite" type="flash.accessibility::AccessibilityImplementation" declaredBy="flash.display::InteractiveObject">
    <metadata name="Inspectable">
      <arg key="environment" value="none"/>
    </metadata>
  </accessor>
  <accessor name="mouseEnabled" access="readwrite" type="Boolean" declaredBy="flash.display::InteractiveObject"/>
  <accessor name="focusRect" access="readwrite" type="Object" declaredBy="flash.display::InteractiveObject"/>
  <accessor name="tabIndex" access="readwrite" type="int" declaredBy="flash.display::InteractiveObject"/>
  <accessor name="tabEnabled" access="readwrite" type="Boolean" declaredBy="flash.display::InteractiveObject"/>
  <accessor name="mask" access="readwrite" type="flash.display::DisplayObject" declaredBy="flash.display::DisplayObject"/>
  <accessor name="filters" access="readwrite" type="Array" declaredBy="flash.display::DisplayObject"/>
  <accessor name="stage" access="readonly" type="flash.display::Stage" declaredBy="flash.display::DisplayObject"/>
  <method name="localToGlobal" declaredBy="flash.display::DisplayObject" returnType="flash.geom::Point">
    <parameter index="1" type="flash.geom::Point" optional="false"/>
  </method>
  <accessor name="rotation" access="readwrite" type="Number" declaredBy="flash.display::DisplayObject"/>
  <accessor name="parent" access="readonly" type="flash.display::DisplayObjectContainer" declaredBy="flash.display::DisplayObject"/>
  <accessor name="blendMode" access="readwrite" type="String" declaredBy="flash.display::DisplayObject"/>
  <accessor name="scaleX" access="readwrite" type="Number" declaredBy="flash.display::DisplayObject"/>
  <accessor name="scaleY" access="readwrite" type="Number" declaredBy="flash.display::DisplayObject"/>
  <accessor name="alpha" access="readwrite" type="Number" declaredBy="flash.display::DisplayObject"/>
  <method name="getRect" declaredBy="flash.display::DisplayObject" returnType="flash.geom::Rectangle">
    <parameter index="1" type="flash.display::DisplayObject" optional="false"/>
  </method>
  <accessor name="scale9Grid" access="readwrite" type="flash.geom::Rectangle" declaredBy="flash.display::DisplayObject"/>
  <accessor name="accessibilityProperties" access="readwrite" type="flash.accessibility::AccessibilityProperties" declaredBy="flash.display::DisplayObject"/>
  <accessor name="mouseY" access="readonly" type="Number" declaredBy="flash.display::DisplayObject"/>
  <accessor name="y" access="readwrite" type="Number" declaredBy="flash.display::DisplayObject"/>
  <accessor name="root" access="readonly" type="flash.display::DisplayObject" declaredBy="flash.display::DisplayObject"/>
  <accessor name="cacheAsBitmap" access="readwrite" type="Boolean" declaredBy="flash.display::DisplayObject"/>
  <accessor name="scrollRect" access="readwrite" type="flash.geom::Rectangle" declaredBy="flash.display::DisplayObject"/>
  <accessor name="width" access="readwrite" type="Number" declaredBy="flash.display::DisplayObject"/>
  <accessor name="x" access="readwrite" type="Number" declaredBy="flash.display::DisplayObject"/>
  <accessor name="height" access="readwrite" type="Number" declaredBy="flash.display::DisplayObject"/>
  <method name="globalToLocal" declaredBy="flash.display::DisplayObject" returnType="flash.geom::Point">
    <parameter index="1" type="flash.geom::Point" optional="false"/>
  </method>
  <accessor name="loaderInfo" access="readonly" type="flash.display::LoaderInfo" declaredBy="flash.display::DisplayObject"/>
  <method name="getBounds" declaredBy="flash.display::DisplayObject" returnType="flash.geom::Rectangle">
    <parameter index="1" type="flash.display::DisplayObject" optional="false"/>
  </method>
  <accessor name="name" access="readwrite" type="String" declaredBy="flash.display::DisplayObject"/>
  <method name="hitTestPoint" declaredBy="flash.display::DisplayObject" returnType="Boolean">
    <parameter index="1" type="Number" optional="false"/>
    <parameter index="2" type="Number" optional="false"/>
    <parameter index="3" type="Boolean" optional="true"/>
  </method>
  <accessor name="visible" access="readwrite" type="Boolean" declaredBy="flash.display::DisplayObject"/>
  <accessor name="opaqueBackground" access="readwrite" type="Object" declaredBy="flash.display::DisplayObject"/>
  <accessor name="mouseX" access="readonly" type="Number" declaredBy="flash.display::DisplayObject"/>
  <accessor name="transform" access="readwrite" type="flash.geom::Transform" declaredBy="flash.display::DisplayObject"/>
  <method name="hitTestObject" declaredBy="flash.display::DisplayObject" returnType="Boolean">
    <parameter index="1" type="flash.display::DisplayObject" optional="false"/>
  </method>
  <method name="hasEventListener" declaredBy="flash.events::EventDispatcher" returnType="Boolean">
    <parameter index="1" type="String" optional="false"/>
  </method>
  <method name="dispatchEvent" declaredBy="flash.events::EventDispatcher" returnType="Boolean">
    <parameter index="1" type="flash.events::Event" optional="false"/>
  </method>
  <method name="removeEventListener" declaredBy="flash.events::EventDispatcher" returnType="void">
    <parameter index="1" type="String" optional="false"/>
    <parameter index="2" type="Function" optional="false"/>
    <parameter index="3" type="Boolean" optional="true"/>
  </method>
  <method name="willTrigger" declaredBy="flash.events::EventDispatcher" returnType="Boolean">
    <parameter index="1" type="String" optional="false"/>
  </method>
  <method name="addEventListener" declaredBy="flash.events::EventDispatcher" returnType="void">
    <parameter index="1" type="String" optional="false"/>
    <parameter index="2" type="Function" optional="false"/>
    <parameter index="3" type="Boolean" optional="true"/>
    <parameter index="4" type="int" optional="true"/>
    <parameter index="5" type="Boolean" optional="true"/>
  </method>
  <method name="toString" declaredBy="flash.events::EventDispatcher" returnType="String"/>
</type>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines