views:

458

answers:

2

Hello, I'm having problem with listening to an event dispatched from a child class, and I don't know why?

I have to classes: class 1, witch draws a rectangle and dispatches a custom Event

package { import flash.display.; import flash.events.; import flash.text.*;

public class clase1 extends Sprite
{
 private var labelField:TextField;
 public function clase1(label:String = "buttono") {
  // draw the background for the button.
  graphics.beginFill(0x3366CC);
  graphics.drawRect(0, 0, 100, 30);
  // store the label as the button’s name.
  name=label;
  // create a TextField to display the button label.
  labelField = new TextField();
  // ensure clicks are sent from labelField rather than the button.
  labelField.mouseEnabled=false;
  labelField.selectable=false;
  labelField.text=label;
  labelField.x=10;
  labelField.y=10;
  labelField.width=80;
  labelField.height=20;
  addChild(labelField);
  dispatchEvent(new Event("Hello",true));
 }


}

}

class 2, witch draws another rectangle and listens to the event

package { import flash.display.; import flash.events.; import flash.text.*;

public class clase2 extends Sprite {
 private var labelField:TextField;
 public function clase2(label:String = "buttono") {
  // draw the background for the button.
  graphics.beginFill(0xFFFCCC);
  graphics.drawRect(200, 0, 100, 30);
  // store the label as the button’s name.
  name=label;
  // create a TextField to display the button label.
  labelField = new TextField();
  // ensure clicks are sent from labelField rather than the button.
  labelField.mouseEnabled=false;
  labelField.selectable=false;
  labelField.text=label;
  labelField.x=210;
  labelField.y=10;
  labelField.width=80;
  labelField.height=20;
  addChild(labelField);
  addEventListener( "Hello",eventHandler,true);

 }
 function eventHandler( event: Event )
 {
  trace( "event received ");
 }
}

}

and on the fla I have

import clase1;

var c1:clase1 = new clase1();

import clase2;

var c2:clase2 = new clase2();

addChild(c2);

c2.addChild(c1);

, making c2 parent of c1, but no message appears, why??

thankyou

A: 

Just a quick glance, it looks like c1 is going to fire the event from it's constructor before c2 is ever created and adds the event listener. Try firing the event from a different method in c1, and you can call that method after you have called c2.addChild(c1).

If I modify your code just a bit, I am able to see the event be fired

   public function clase1(label:String = "buttono") {
        name=label;
        labelField = new TextField();
        labelField.text=label;
    }

   public function drawRect():void {
      // draw the background for the button.
      graphics.beginFill(0x3366CC);
      graphics.drawRect(0, 0, 100, 30);
      labelField.mouseEnabled=false;
      labelField.selectable=false;
      labelField.x=10;
      labelField.y=10;
      labelField.width=80;
      labelField.height=20;
      addChild(labelField);
  dispatchEvent(new Event("Hello",true));
  }

And then in the main application

public function test():void {
   var c1:clase1 = new clase1();
   var c2:clase2 = new clase2();

   c2.addChild(c1);
   c1.drawRect();
   }
ryanday
Thank you!!!!!! really, I have been struggling with this thing all day
A: 

I am having a different problem but i think its along the same lines as this thread. For what ever reason my event listener is not firing.I know for a fact that my object is being created because I can see "listener added" in the output. Here is my code.

package com.wsh.savage.world {

import com.wsh.savage.world.WorldManager;

import flash.events.Event;
import flash.events.EventDispatcher;

public class WorldState extends EventDispatcher{

    //constants defining worlds state
    private const STATE_STARTING:uint = 0;
    private const STATE_RUNNING:uint = 1;
    private const STATE_PAUSED:uint = 2;
    private const STATE_DESTROYING:uint = 3;

    //an instance of the manager of all world states
    private var m_worldManager:WorldManager;    

    //keeps track of what state the WorldState is in
    private var m_stateStatus:uint;


    /**
     * Constructor starts the state up
     */
    public function WorldState(manager:WorldManager) { 
        //set flag that we are creating a world state
        m_stateStatus = STATE_STARTING;

        m_worldManager = manager;
        this.addEventListener(Event.ENTER_FRAME,enterFrameListener,false,1,true);
        trace('listener added');

        //set the flag that the state is now running
        m_stateStatus = STATE_RUNNING;
    }

    protected function enterFrameListener(event:Event){
        trace('event fired');
        if(m_stateStatus == STATE_RUNNING){
            trace('running');
            m_worldManager.getKeyboardManager().getKeyStatus(40);
        }
    }
}

Thanks for the help!

cr1ms0n3cho