【问题标题】:AS3 Getting which movieclip clicked onAS3 获取点击了哪个movieclip
【发布时间】:2012-07-26 14:48:41
【问题描述】:

所以,我创建了一个 for 循环来获取项目中的几个按钮。这是一个问题,我需要一个按钮来快速选择并导航到那里的任何问题。我可以手动完成所有这些,但我的代码不仅会冗长且令人困惑,而且还会出现问题,因为问题的数量并不总是相同。

所以现在我有:

function SetQuestionSquares():void{
    for(var i:Number = 1; i <= TestProperties.QuestionLimit;i++){
        var QuestionSquare:questionsquare = new questionsquare;
        QuestionSquare.buttonMode = true;
        QuestionSquare.mouseChildren = false;
        QuestionSquare.x = NavLeft.x + (20 * i);
        QuestionSquare.y = NavLeft.y;
        QuestionSquare.questionsquaretext.text = i.toString();
        addChild(QuestionSquare);
        QuestionSquare.addEventListener(MouseEvent.CLICK, GoToQuestionNumber);
    }
    addChild(NavLeft);
    addChild(NavRight);
}

function GoToQuestionNumber(e:MouseEvent):void{
    WhichQuestion = ???; //I don't know what goes here. 
    UpdateQuestions();
    trace("testing"); //Gets called correctly, so its working.
}

我的问题是确定点击了哪个方块。我需要有一些方法来获取“e”(点击)事件,所以我知道用户点击了哪个按钮。

【问题讨论】:

    标签: actionscript-3 flash movieclip


    【解决方案1】:

    您需要Event 对象的.target 属性:

    WhichQuestion = e.target as questionsquare;
    

    【讨论】:

    • 根据我的测试,这并没有给我一个唯一的标识符。他们都以“问题方”的形式返回。我需要像“QuestionSquare1”这样的东西。我是否需要在“addChild”循环中为他们做一些事情以获得唯一标识符?
    • 好的,想出了一个解决方案。刚刚做了一个“QuestionSquare.name = i.toString();”在循环中,然后是“WhichQuestion = e.target.name;”。不知道这是最正确的方法,但它正在工作。谢谢。
    • @MarcoFox 或者你可以使用getChildIndex -> WhichQuestion = e.target.parent.getChildIndex(e.target)+1
    • 你真的应该使用e.currentTarget 而不是e.target
    • @32bitkid 不,你错了,请参阅 OP 问题中的QuestionSquare.mouseChildren = false;
    【解决方案2】:
    function GoToQuestionNumber(e:MouseEvent):void{
        var WhichQuestion:DisplayObject = e.currentTarget as DisplayObject;
        UpdateQuestions();
        trace("testing");
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-27
      • 1970-01-01
      • 2014-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-06
      相关资源
      最近更新 更多