我在收听从儿童班调度的事件时遇到问题,我不知道为什么?
我必须上课: class 1,witch绘制一个矩形并调度一个自定义事件
包 { 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));
}
}
}
第2课,女巫绘制另一个矩形并听取事件
包{ 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 ");
}
}
}
在fla上我有
import clase1;
var c1:clase1 = new clase1();
import clase2;
var c2:clase2 = new clase2();
的addChild(C2);
c2.addChild(C1);
,使c1成为c1的父级,但没有出现消息,为什么??
三江源
答案 0 :(得分:0)
只需快速浏览一下,看起来c1将在创建c2之前从它的构造函数触发事件并添加事件侦听器。尝试从c1中的其他方法触发事件,然后在调用c2.addChild(c1)后调用该方法。
如果我稍微修改你的代码,我可以看到该事件被解雇
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));
}
然后在主应用程序中
public function test():void {
var c1:clase1 = new clase1();
var c2:clase2 = new clase2();
c2.addChild(c1);
c1.drawRect();
}
答案 1 :(得分:0)
我有一个不同的问题,但我认为它与这个线程一样。 因为我的事件监听器没有触发的原因。我知道我的对象正在被创建,因为我可以在输出中看到“listener added”。这是我的代码。
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);
}
}
}
感谢您的帮助!