我在电影剪辑里有很多按钮。与其为每个监听器创建侦听器,我只想根据它们在mc中单击的目标执行一个操作。我想要根据他们点击的按钮来更改alpha。这是如何做到的呢?
下面我尝试了4种选择,但这些都没有效果。
manyButtons.addEventListener(MouseEvent.MOUSE_UP, mUp);
function mDown(e:Event)
{
trace(e.target.name); // Works! Outputs name of button I click
this[e.target.name].alpha = .5; // Does not work
e.target.name.alpha = .5; // Error: can not create property alpha on sting
e.target.alpha = .5; // changes ALL children buttons and parent mc.
}
发布于 2014-02-03 00:12:10
e.target.name返回按钮的名称,正如您已经观察到的那样。但你的按钮不在“这个”里。它在this.manyButtons里。你必须提供完整的路径。因此,解决办法是:
this.manyButtons[e.target.name].alpha = .5;
...assuming表示按钮是manyButtons显示对象的子元素。
顺便说一句,一个更好的方法就是:
MovieClip(e.target).alpha = .5
编辑:如果您来自AS2 ->,请记住侦听器中的作用域不再更改。如果你以前写过这样的东西,忘了它吧:
this.manyButtons.onRelease = function() {
trace(this); //this changed the scope to the manyButtons object!
}
this
在as3中不再改变。它总是指在其中声明的对象!
https://stackoverflow.com/questions/21521932
复制