首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >单击FocusManager上的按钮

单击FocusManager上的按钮
EN

Stack Overflow用户
提问于 2022-04-17 08:33:49
回答 1查看 83关注 0票数 0

我正在为Arduino.There编写一个应用程序,这个应用程序中有很多MovieClip中的按钮。我可以点击任何一个使用键盘或鼠标,一切正常工作。但是应用程序将使用来自数组的控件。例如,如果是Data5=100,那么应该按TAB键盘按钮。如果是Data5=600,则应按Enter。目前,按下TAB打开FocusManager。下一次单击时,FocusManager将移动到next按钮。但是如果Data5=600,当我单击MovieClip中的按钮时,我会得到一个错误。3个按钮(Btn1、Btn2、Btn3)处于主舞台。这段代码运行良好。

代码语言:javascript
复制
this[BtnCurentName].dispatchEvent(new MouseEvent(MouseEvent.CLICK, true, false));

3个按钮(Btn4,Btn5,Btn6)在MovieClip中,"Prop".I只能按这样的按钮。

代码语言:javascript
复制
this[BtnCurentName].dispatchEvent(new MouseEvent(MouseEvent.CLICK, true, false));

这是我的测试代码。

代码语言:javascript
复制
package
{
    import flash.events.*;
    import fl.managers.*;
    import flash.display.*;

    dynamic public class Main extends MovieClip
    {
        public var fm:FocusManager;
        public var myBtnFocus:InteractiveObject; 
        public var myBtnName:String = "";
        public var BtnCurentName:String = "";
        public var Prop:MovieClip;

        //# /////////////////////////////////////////////////////////////
        //# Main code and supporting functions...
        //# /////////////////////////////////////////////////////////////

        public function Main()
        {
            addFrameScript(0, this.frame1);
            
            fm = new FocusManager(this);
            fm.setFocus( this ); //# manually set your button as default //# fm.setFocus( your button );
            
            this.myBtnFocus = fm.getFocus(); //# update the reference to know currently focused
            this.myBtnName = this.myBtnFocus.name; //# extract name from focused
        }

        public function Rendering() : void
        {
            this.Connect.Status.text = String( this.Data[5] ); // view arduino Data[5]
            
            if (this.Data[5] > 0) //# if the button is pressed
            {
                if( ! Boolean(this.chkBtn))//# if the button is not pressed
                {   
                    //# press mouse click (Enter)
                    if ( this.Data[5] > 950 )
                    {
                        //# it works perfectly, BUT if my buttons are inside MovieClip, when pressing Data[5]=600 I get an error 
                        //# TypeError: Error #1010: A term is undefined and has no properties.
                        trace (">> Name for Click is : " + BtnCurentName);                      
                        this[BtnCurentName].dispatchEvent(new MouseEvent(MouseEvent.CLICK, true, false));
                        this.chkBtn = true;

                            /*
                            //# results of Trace...

                            >> Got TAB ... Focus is now changed to : Btn1
                            >> Name for Click is : Btn1
                            >> Got Click ... Button name is : Btn1
                            >> Got TAB ... Focus is now changed to : Btn2
                            >> Name for Click is : Btn2
                            >> Got Click ... Button name is : Btn2
                            >> Got TAB ... Focus is now changed to : Btn3
                            >> Name for Click is : Btn3
                            >> Got Click ... Button name is : Btn3
                            >> Got TAB ... with Parent ... Focus is now changed to : Prop.Btn4
                            >> Name for Click is : Prop.Btn4
                            TypeError: Error #1010: A term is undefined and has no properties.

                            //# end of results
                            */
                    }   
                    
                    //# TAB = go to next button (component)
                    //# if ( (this.Data[5] == 100) || (this.Data[5] == "100") )
                    if ( this.Data[5] > 820 && this.Data[5] < 860 )
                    {
                        //# it works perfectly
                        this.myBtnFocus = fm.getNextFocusManagerComponent(); //# find it as next one
                        fm.setFocus( this.myBtnFocus ); //# then set as current selection (focused)
                        this.chkBtn = true;
                    }               
                }
            }
            else //# if the button is pressed and Data[5]==0, deleting variables
            {
                if( Boolean(this.chkBtn)) 
                {
                    delete this.chkBtn;
                }
            }
            return;
        }

        public function onMouseClick (event:MouseEvent = null) :void        
        {
            if( event.currentTarget.name != null)
            { this.myBtnName = event.currentTarget.name; }
            trace( ">> Got Click ... Button name is : " + this.myBtnName );
        }
        
        public function onFocusedBtn (event:FocusEvent) :void 
        {
            if(event.target.parent.name == "root2")  
            {
                BtnCurentName = String(event.target.name);
                trace (">> Got TAB ... Focus is now changed to : " + BtnCurentName)
            }
            else 
            {
                BtnCurentName = String(event.target.parent.name + '.' + event.target.name);
                trace (">> Got TAB ... Focus is now changed to : " + BtnCurentName);
            }
            return;
        }
            
        function frame1()
        {                     
            this.Btn1.addEventListener( MouseEvent.CLICK, this.onMouseClick );
            this.Btn2.addEventListener( MouseEvent.CLICK, this.onMouseClick );
            this.Btn3.addEventListener( MouseEvent.CLICK, this.onMouseClick );
            
            //# Buttons into MovieClip
            this.Prop.Btn4.addEventListener( MouseEvent.CLICK, this.onMouseClick );
            this.Prop.Btn5.addEventListener( MouseEvent.CLICK, this.onMouseClick );
            this.Prop.Btn6.addEventListener( MouseEvent.CLICK, this.onMouseClick );
            
            //# Focus listener
            this.addEventListener( FocusEvent.FOCUS_IN, onFocusedBtn ); 

            return;
        }
    } //end Class Main
    
} //end Package

如何确定按钮位于哪个MovieClip中才能按下它?

EN

回答 1

Stack Overflow用户

发布于 2022-04-20 11:14:02

尝试:

代码语言:javascript
复制
public function onFocusedBtn (event:FocusEvent) :void 
{
    if(event.target.parent.name == "root2") 
    {
        this.BtnParent = event.target.parent.name;
        this.BtnName = event.target.name;
        trace (">> Got TAB ... Focus is now changed to : " + this.BtnName)
    }
    else 
    {
        this.BtnParent = event.target.parent.name;
        this.BtnName = event.target.name;
        trace (">> Got TAB ... with Parent ... Focus is now changed to : " + this.BtnParent + "." + this.BtnName);
    }
    return;
}

然后,在您的function Rendering中,您可以检查如下:

代码语言:javascript
复制
//# press mouse click (Enter)
if ( this.Data[5] > 950 )
{
    if(this.BtnParent == "root2")
    {
        trace (">> Name for Click is : " + this.BtnName);
        this[this.BtnName].dispatchEvent(new MouseEvent(MouseEvent.CLICK, true, false));
    }
    else 
    {
        trace (">> Name for Click is : " + this.BtnParent + "." +this.BtnName);
        this[this.BtnParent][this.BtnName].dispatchEvent(new MouseEvent(MouseEvent.CLICK, true, false));
    }
    this.chkBtn = true;

} 

解决了。我得到了这么多线索

代码语言:javascript
复制
>> Got TAB ... Focus is now changed to : Btn1
>> Name for Click is : Btn1
>> Got Click ... Button name is : Btn1
>> Got TAB ... Focus is now changed to : Btn2
>> Name for Click is : Btn2
>> Got Click ... Button name is : Btn2
>> Got TAB ... Focus is now changed to : Btn3
>> Name for Click is : Btn3
>> Got Click ... Button name is : Btn3
>> Got TAB ... with Parent ... Focus is now changed to : Prop.Btn4
>> Name for Click is : Prop.Btn4
>> Got Click ... Button name is : Btn4
>> Got TAB ... with Parent ... Focus is now changed to : Prop.Btn5
>> Name for Click is : Prop.Btn5
>> Got Click ... Button name is : Btn5
>> Got TAB ... with Parent ... Focus is now changed to : Prop.Btn6
>> Name for Click is : Prop.Btn6
>> Got Click ... Button name is : Btn6
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71900439

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档