Actionscript how to get name of instance using getChildByName

谁说胖子不能爱 提交于 2019-12-11 20:05:38

问题


Okay so I have a MovieClip called sC and need to write a code where, if you click the button (sC) then sC will dissapear. The function needs to work for multiple buttons. What I tried was

sC.addEventListener(MouseEvent.CLICK, clickHandler);
function clickHandler(event:MouseEvent):void {
    var self;
    self = MovieClip(getChildByName(event.target.name));
    self.visible=false;

Now when I try this code, it gives me an error when I click sC. It says "cannot access a property or method of a null object reference.". when I try to trace(self) it outputs "null". Is there a way where I can get the name of the instance of the object which is using the clicKHandler function and then make it's visibilty equal to false (visible=false)?

Note that when I trace(event.target.name) it says "instance127".


回答1:


In your code, the variable self resolves to your movieClip's name, but not the complete path to where it exists. Try setting it up like below, where target is the button that was clicked:

sC.addEventListener(MouseEvent.CLICK, clickHandler);

function clickHandler(event:MouseEvent):void
{
    event.target.visible = false;
}


来源:https://stackoverflow.com/questions/19010473/actionscript-how-to-get-name-of-instance-using-getchildbyname

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!