0

如何添加变量“userSave”使代码看起来像第二个片段?

var userSave:Number = 1;

user_presets["u"+userSave].addEventListener(MouseEvent.MOUSE_DOWN, ["u"+userSave]);
function ["u"+userSave](event:MouseEvent):void
{

}


// It needs to look like this:
user_presets.u1.addEventListener(MouseEvent.MOUSE_DOWN, u1);
function u1(event:MouseEvent):void
{

}

编辑:好的,让我试着解释一下我想要实现的目标。我为如此含糊而道歉。我花了一些时间来弄清楚我要做什么。

我有 36 个按钮,我想做下面的代码。下面的代码是第一个按钮。下一个按钮需要是 u2、u3、u4 等。因此,以下代码中 u1 的所有实例都需要针对每个按钮进行相应更改。如何在不重复此代码 36 次的情况下执行此操作?代码在时间线上。

f_presets.user_presets.u1.addEventListener(MouseEvent.MOUSE_DOWN, u1);

function u1(event:MouseEvent):void
{

if (saveFlag == "play")
{

userSave = "u1";
myuserSave();


}
else
{
    userSave = "u1";
myuserPlay();

}

}

最终编辑 感谢所有帮助。这就是最终为我工作的原因。

for (var i:Number=1; i<37; i++)
{
var userSaver = "u" + i;
f_presets.user_presets[userSaver].addEventListener(MouseEvent.MOUSE_DOWN, recall);
function recall(event:MouseEvent):void
{
    if (saveFlag == "play")
    {
        trace(userSave = event.currentTarget.name);
        myuserSave();
    }
    else
    {
        trace(userSave = event.currentTarget.name);
        if (mySO.data["user_saved" + userSave] == "yes")
        {
            myuserPlay();
        }
    }
}
}
4

3 回答 3

1

不确定我是否理解您要实现的目标,但假设 userSave 将采用多个值...

//save userSave for later
user_presets["u"+userSave].id = String(userSave);
//add listener
user_presets["u"+userSave].addEventListener(MouseEvent.MOUSE_DOWN, eventHandler);

function eventHandler(event:MouseEvent):void
{
    //retrieve userSave from target
    var userSaveValue:Number = Number(event.target.id);
    //do something based on value of userSave...?    
}

或者也许使用内联函数(绝对不是最佳实践,但有时必须):

user_presets["u"+userSave].addEventListener(MouseEvent.MOUSE_DOWN, function(event:MouseEvent){
    //code here
});
于 2012-12-15T01:39:04.840 回答
1

您需要调整您的方法以使其简单化。

首先我假设 save_presets 的孩子都是你的按钮。如果没有,您应该考虑为这些按钮创建一个容器,以便您可以简单地执行此操作:

for(var index:int = 0;index < save_presets.numChildren;index++)
{
    var button:MovieClip = save_presets.getChildAt(index) as MovieClip;
    button.id = index;
    button.addEventListener(MouseEvent.MOUSE_DOWN, eventHandler);
}

要删除处理程序,您可以这样做:

for(var index:int = 0;index < save_presets.numChildren;index++)
{
    var button:MovieClip = save_presets.getChildAt(index) as MovieClip;
    button.removeEventListener(MouseEvent.MOUSE_DOWN, eventHandler);
}
于 2012-12-15T02:09:36.320 回答
0

编辑:

好吧,我很确定我知道你想做什么,这是一个示例类。此类创建 36 个方形按钮,每个按钮之间有 1 个像素间距。每个按钮都有一个事件侦听器,当鼠标悬停在按钮上时,每个按钮都会触发一个事件。我希望这会有所帮助,如果不让我知道

package {

import flash.display.MovieClip;
import flash.events.*;
import flash.geom.*; // Used to create rectangle for buttons

/**
 * ...
 * @author [Your Name Here]
 */
public class Main extends MovieClip
{
    public var user_presets:Array; // Array to store buttons

    public const BUTTON_COUNT:int = 36; // How many buttons do you want?

    public function Main():void 
    {   
        user_presets = new Array(); // Initialize button array

        this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage); // Listener for ADDED_TO_STAGE event
    }

    private function onAddedToStage(e:Event = null):void 
    {
        var tmpX:int = 0; // Button's x value
        var tmpY:int = 0; // Button's y value

        for (var i:int = 0; i < BUTTON_COUNT; i++) { // For loop to create 36 buttons

            var tmpButton:MovieClip = new MovieClip(); // Create a temporary button

            tmpButton.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown); // Add MOUSE_DOWN event listener to the temporary button

            tmpButton.graphics.beginFill(0x000000, 1); // Fill the rectangle we create with 0x000000 (Black)
            tmpButton.graphics.drawRect(0, 0, 10, 10); // Draw a 10 x 10 rectangle that will be one button
            tmpButton.graphics.endFill(); // Stop filling rectangle

            tmpButton.name = "u" + (i + 1); // This is your u1, u2 ... u(n) where n is how many buttons you want
            tmpButton.x = tmpX; // Temporary buttons x value
            tmpButton.y = tmpY; // Temporary buttons y value

            tmpX += 11; // Add 11 to tmpButtons x value, basically adding a 1 pixel space between each button

            user_presets.push(tmpButton); // Add the temporary button to your user_presets array

            stage.addChild(tmpButton); // Add the temporary button to the stage

            } // Repeat above steps 35 times

        this.removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage); // Remove the ADDED_TO_STAGE listener
    }

    public function onMouseDown(event:Event):void
    {
        trace(event.currentTarget.name); // Output which button was clicked

        // I used a switch statement but you can use if else statements if you'd like,
        // I think switch statements are easier to code for alot of conditionals.
        switch(event.currentTarget.name) {
            case "u1": // If the mouse is down over the first button, output "u1 was clicked!"
                trace("u1 was clicked!");
                break;

            case "u2": // If the mouse is down over the second button, output "u2 was clicked!"
                trace("u2 was clicked!");
                break;
        }
    }

}

}

于 2012-12-15T23:17:52.073 回答