在多个地方使用变量 - AS3

时间:2012-12-14 23:34:45

标签: actionscript-3

如何添加变量“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();
        }
    }
}
}

3 个答案:

答案 0 :(得分: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
});

答案 1 :(得分: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);
}

答案 2 :(得分: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;
        }
    }

}

}