简体   繁体   English

AS3:访问动画片段按钮

[英]AS3: Accesing movieclip in button

I was trying to access movieclip located in a button, which i refer by this code: 我试图访问位于按钮中的movieclip,该代码由我引用:

var buttonObject = this[weaponsPurchased[i]];

Then i'm setting "mouseEnabled" false (that part works) 然后我将“ mouseEnabled”设置为false(该部分有效)

buttonObject.mouseEnabled = false;

And then i'm trying to set a movieclip inside this button unvisible (and that doesn't work) 然后我试图将这个按钮内的动画片段设置为不可见(并且不起作用)

buttonObject["square"].visible = false;

I get this error: 我收到此错误:

ReferenceError: Error #1069: Property square not found on flash.display.SimpleButton and there is no default value. ReferenceError:错误#1069:在flash.display.SimpleButton上找不到属性正方形,并且没有默认值。 at (...) 在 (...)

I didn't find any help on the internet, so please help me. 我在互联网上找不到任何帮助,所以请帮助我。 What am i doing wrong? 我究竟做错了什么?

You can not access child elements of a SimpleButton. 您不能访问SimpleButton的子元素。 SimpleButtons are not DisplayObjectContainers, yet they can have child elements when being created from the Flash IDE. SimpleButton不是DisplayObjectContainers,但是从Flash IDE创建时,它们可以具有子元素。

Source: http://xtyler.com/as3-simple-button-breaking-all-rules/ 资料来源: http : //xtyler.com/as3-simple-button-breaking-all-rules/

I would just go with a MovieClip instead with your own class to handle MouseOver and MouseOut events. 我将只使用MovieClip而不是使用您自己的类来处理MouseOver和MouseOut事件。 A good tutorial outlines the creation of a reusable button class that extends MovieClip. 一个好的教程概述了扩展MovieClip的可重用按钮类的创建。

http://www.ironcoding.com/2011/02/flash-as3-movieclip-button-tutorial/ http://www.ironcoding.com/2011/02/flash-as3-movieclip-button-tutorial/

I had this problem a while ago. 我前一段时间有这个问题。 What I did was to search for the DisplayObject inside of each state of the button. 我要做的是在按钮的每个状态内搜索DisplayObject。 I'll drop some code here that might help you. 我将在此处放置一些可能对您有帮助的代码。 You must realize that if your button have objects in all four frames, you will find your object in all the states as well. 您必须意识到,如果按钮在所有四个框架中都有对象,那么您还将在所有状态下都找到对象。

    private var Obj1:DisplayObject, Obj2:DisplayObject, 
    Obj3:DisplayObject, Obj2:DisplayObject;

    private function searchInChildren(spr:DisplayObject, name:String):void
    {
        for (var i:int = 0; i < spr.numChildren; i++) 
        {
            if(spr.getChildAt(i).name == name)
            {
                return spr.getChildAt(i);
            }           
        }
        return null;
    }

    public function searchControllers(_ref:SimpleButton, name:String):void
    {   
        try{
            Obj1 = searchInChildren(_ref.upState, name);
            Obj2 = searchInChildren(_ref.overState, name);
            Obj3 = searchInChildren(_ref.downState, name);
            Obj4 = searchInChildren(_ref.hitTestState, name);
        } catch (e:Error) {
            trace("error: "+e+", when trying to search for controllers");
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM