更改数据时AS3事件侦听器?

时间:2011-01-21 06:45:12

标签: actionscript-3 actionscript

如果这是一个简单/愚蠢的问题,希望你能原谅我。我大约6天前开始学习动作脚本,并且已经开始了一个小项目:D

无论如何,有一个属性偶尔会改变以反映游戏中关卡的名称(object._I._M.text)。改变可能需要一分钟,或者最多两分钟,这取决于所有玩家能够以多快的速度完成关卡。

我希望能够侦听此属性中的更改以触发其他功能。我在网上找到的答案很少,我发现的例子非常不完整,写得很差。有谁知道我怎么能做到这一点?

我试过......

theobject._I._M.addEventListener(Event.CHANGE,myfunction);

没有成功。感谢您提供任何帮助或建议,我将在等待回复时回过头来学习:D

2 个答案:

答案 0 :(得分:3)

我可能会使用getter / setter,或者只是声明一个方法来更改该文本字段的文本,以便每次都可以调度一个事件。

function changeLevel(text:String):void {
   levelTf.text=text;
   dispatchEvent(new Event("levelChange"));
}

答案 1 :(得分:1)

我同意Adobe文档有点“沉重”。 kirupa论坛是一个很好的资源。

对于TextField更改事件侦听器,您的原始代码非常接近。 Here是如何添加事件侦听器的一个很好的示例。基础是:

public class Main extends Sprite {
  // Store a reference to the text field
  // (you're already doing this somewhere, so adapt as you see fit)
  private var inputfield:TextField = new TextField();

  public function Main() {
    // Make sure the field is added to an on-screen Sprite
    addChild(inputfield);

    // Add the event listener.
    // I recommend adding the 'false, 0, true' params. There are lengthy
    // discussions around about this.
    inputfield.addEventListener(Event.CHANGE, changeListener, false, 0, true);
  }

  // This function gets called every time the event is fired.
  private function changeListener (e:Event):void {
    trace("event");
  }
}

希望这能让你开始朝着正确的方向前进。

相关问题