【问题标题】:Strings and variables字符串和变量
【发布时间】:2013-12-26 19:48:45
【问题描述】:

我的代码:

var nameOfMc:String = "";

function moverMc(nameOfMc:String):void {
    nameOfMc.play();
}

function handleCollision( e:Event ):void{
    var _point:Point = localToGlobal(new Point(mouseX,mouseY));

    if(bigCircle_mc.hitTestPoint(_point.x,_point.y,true)){
           output_txt.text = "BIGCIRCLE"
           nameOfMc = "bigCircle_mc";
           moverMc(nameOfMc);
    }else{
        if(circle_mc.hitTestPoint(_point.x,_point.y,true)){
               output_txt.text = "CIRCLE"
               nameOfMc = "circle_mc";
               moverMc(nameOfMc);
        }else{
            if(rect_mc.hitTestPoint(_point.x,_point.y,true)){   
                output_txt.text = "RECT"
                nameOfMc = "rect_mc"
                moverMc(nameOfMc);
            }else{
                output_txt.text = ""
                stopAll();
                nameOfMc = ""
            }
        }
    }

我的问题:

在函数 moverMc 中,我希望 Flash 执行与“bigCircle_mc.play();”相同的操作例如,如果鼠标在大圆圈上,“rect_mc.play();”如果鼠标悬停在矩形上...

感谢帮助

【问题讨论】:

    标签: actionscript-3 function variables


    【解决方案1】:

    您的moverMc 函数错误:nameOfMc 是字符串,而不是显示对象,因此您无法播放它:String 类型没有play() 方法。

    您需要做的是找到名称包含在nameOfMc 变量中的DisplayObject 实例。 (影片剪辑是显示对象。)

    要按名称查找DisplayObject,您可以在包含您要查找的影片剪辑的容器上使用getChildByName() 函数。 (例如,Stage 可以是此容器,但它可以是显示链中的任何其他 DisplayObjectContainer - 如果您的剪辑嵌套在其他几个剪辑中,则显示链中将有多个 DisplayObjectContainer 实例。 )

    所以你的函数应该是这样的:

    function moverMc(nameOfMc:String):void
    {
        var oClip:DisplayObject = theStage.getChildByName(nameOfMc);
    
        if((oClip != null) && (oClip is MovieClip))
        {
            // You need to cast oClip to MovieClip because getChildByName()
            // returns DisplayObject (the base type of MovieClip) but
            // not all DisplayObjects have a timeline.
            (oClip as MovieClip).play();
        }
    }
    

    theStage 变量是一个全局变量(Stage 类型),您必须在 Flash 电影开始时将其设置为该阶段。请参阅此SO question,了解有关如何操作的更多详细信息。

    编辑: 在 OP 的评论之后,我添加了一个不需要舞台的版本;这只是使用当前的 DisplayObjectContainer 来搜索孩子。 (Stage 只是顶层显示对象容器,但所有可以嵌套显示对象的显示对象都是显示对象容器。)

    function moverMc(parentOfMc:DisplayObject, nameOfMc:String):void
    {
        var oClip:DisplayObject = parentOfMc.getChildByName(nameOfMc);
    
        if((oClip != null) && (oClip is MovieClip))
        {
            // You need to cast oClip to MovieClip because getChildByName()
            // returns DisplayObject (the base type of MovieClip) but
            // not all DisplayObjects have a timeline.
            (oClip as MovieClip).play();
        }
    }
    

    你可以这样称呼这个版本:

    nameOfMc = "circle_mc";
    moverMc(this, nameOfMc);
    

    this 在当前上下文中指的是当前显示对象容器 - 这可能是舞台,也可能是舞台上的显示对象。您已经在代码中使用了this(可能没有意识到):localToGlobal 调用实际上是this.localToGlobal

    【讨论】:

    • 你能解释一下这个theStage变量吗?我创建了一个新类(根据此页面:github.com/inruntime/AS3-Global-Object),但我真的不知道它们将如何结合在一起。
    • @as3learner 我用不依赖舞台的函数版本更新了我的答案。
    猜你喜欢
    • 2021-04-12
    • 1970-01-01
    • 1970-01-01
    • 2020-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多