在actionscript 3中需要一些数组帮助

时间:2021-05-17 02:07:48

i am very new to concept of array...so i need a bit help understanding and executing it....

我对数组的概念很新...所以我需要一些帮助理解和执行它....

so I have 5 buttons...

所以我有5个按钮......

monday - friday

星期一 - 星期五

now when somebody presses one of the buttons I want the program to trace the day that was pressed only.. can somebody please explain me step by step on what to do and why?

现在,当有人按下其中一个按钮时,我希望程序跟踪仅按下的那一天..有人可以一步一步地解释我该怎么做以及为什么?

I am using flash actionscript 3.. this is pretty much the only thing I know how to do...I've tried some onlint tuts but they weren't very clear

我正在使用flash actionscript 3 ..这几乎是我唯一知道怎么做的...我尝试了一些onlint tuts但是他们不是很清楚

var days: Array ["mon", "tues", "wed", "thurs", "fri"];

2 个解决方案

#1


1  

Your Array was created wrong in your post. Try this:

您的数组在帖子中创建错误。试试这个:

var days:Array = ["mon", "tues", "wed", "thurs", "fri"];

or you could also create it like this, using the push() method of the Array class:

或者您也可以使用Array类的push()方法创建它:

var days:Array = new Array();
days.push( 'mon');
days.push( 'tues');
days.push( 'weds');
days.push( 'thurs');
days.push( 'fri');

to trace the values of the array in your post:

跟踪帖子中数组的值:

trace( days[0] ); // returns "mon"
trace( days[1] ); // returns "tues"
trace( days[2] ); // returns "wed"
trace( days[3] ); // returns "thurs"
trace( days[4] ); // returns "fri"

The array's contents are stored in the array's "indexes". An array's index always starts with 0.

数组的内容存储在数组的“索引”中。数组的索引始终以0开头。

Array's have a length. An empty array's length is 0. If the array has at least one item in it, the length is 1 and increases as you add more items. To loop through your array to get the values do this:

数组的长度。空数组的长度为0.如果数组中至少有一个项目,则长度为1,并在添加更多项目时增加。要遍历数组以获取值,请执行以下操作:

for(var i:int = 0; i < days.length; i++)
{
    trace( days[i] );
}

Arrays are a powerful and essential part of any programming language. You can remove items from the array, add items, remove a specific item at a specific index, remove an item by name, combine arrays, and lots more. You'd benefit from looking at this link and studying the properties and methods of the Array class. Once you get the hang of how to manipulate Array's you'll never now how you existed without them!

数组是任何编程语言的强大而重要的部分。您可以从数组中删除项目,添加项目,删除特定索引处的特定项目,按名称删除项目,组合数组等等。您将从查看此链接并研究Array类的属性和方法中受益。一旦你掌握了如何操纵数组的话,你现在就永远不会有没有它们的存在!

There are many ways to associate a button with a particular array index. The answer provided by Dr.Denis McCracleJizz is one way, although if you are as new to AS3 as you say, it might seem a little overwhelming as it uses several concepts you may not yet be familiar with. Let me see if I can simplify it a bit, although this will make the code a bit more lengthy:

将按钮与特定数组索引相关联的方法有很多种。 Dr.Denis McCracleJizz提供的答案是单向的,尽管如果你和AS3一样陌生,它可能看起来有点压倒性,因为它使用了一些你可能还不熟悉的概念。让我看看我是否可以简化它,虽然这会使代码更长一些:

mondayButton.addEventListener( MouseEvent.CLICK, onClickDayButton );
tuesdayButton.addEventListener( MouseEvent.CLICK, onClickDayButton );

function onClickDayButton( e:MouseEvent ):void
{
    if( e.target.name == 'mondayButton')
    {
        trace( days[0] );
    }
    else if( e.target.name == 'tuesdayButton')
    {
        trace( days [1] );
    }

    // and so on...
}

If you're familiar with objects you could create an object for each button that hold both the button and the button id and store those in an array:

如果您熟悉对象,则可以为每个按钮创建一个对象,该对象同时包含按钮和按钮ID,并将它们存储在数组中:

var days:Array = ["mon", "tues", "wed", "thurs", "fri"];

var dayButtonArray:Array = new Array();

var mondayButtonObject:Object = new Object();
mondayButtonObject.button = mondayButton;
mondayButtonObject.id = 0;
dayButtonArray.push( mondayButtonObject );


var tuesdayButtonObject:Object = new Object();
tuesdayButtonObject.button = tuesdayButton;
tuesdayButtonObject.id = 1;
dayButtonArray.push( tuesdayButtonObject );

// and like above, the rest of the days here

// then loop through them and set the mouseEvent

for(var i:int = 0; i < dayButtonArray.length; i++)
{
   dayButtonArray[i].button.addEventListener( MoouseEvent.CLICK, onClickDayButton );
}

// and the function each button calls to

function onClickDayButton( e:MouseEvent ):void
{
    trace( days[ evt.target.id ] );
}

You could further simplify the object method above by skipping the days array altogether and adding the day associated with the button right into the dayButtonArray instead of an id:

您可以通过完全跳过days数组并将与按钮关联的日期添加到dayButtonArray而不是id中来进一步简化上述对象方法:

var dayButtonArray:Array = new Array();

var mondayButtonObject:Object = new Object();
mondayButtonObject.button = mondayButton;
mondayButtonObject.day = "monday";
dayButtonArray.push( mondayButtonObject );


var tuesdayButtonObject:Object = new Object();
tuesdayButtonObject.button = tuesdayButton;
tuesdayButtonObject.day = "tuesday";
dayButtonArray.push( tuesdayButtonObject );

// and like above, the rest of the days here

// then loop through them and set the mouseEvent

for(var i:int = 0; i < dayButtonArray.length; i++)
{
   dayButtonArray[i].button.addEventListener( MoouseEvent.CLICK, onClickDayButton );
}

// and the function each button calls to

function onClickDayButton( e:MouseEvent ):void
{
    trace( evt.target.day );
}

Now were getting as complicated as Dr.Denis McCracleJizz's answer. His is definitely worth looking at as well.

现在变得像Dr.Denis McCracleJizz的回答一样复杂。他绝对值得一看。

#2


1  

Your array is a list of items, so your array is composed of the days of the week. You can access them by using an index like so.

您的数组是项目列表,因此您的数组由一周中的几天组成。您可以使用类似的索引来访问它们。

trace(days[0])
//Outputs: mon


trace(days[3])
//Outputs: thurs

Now for your buttons there is two way of doing it, the clean one that's more complicated and the other one that works just as well and that is more simple.

现在你的按钮有两种方式,一种是更复杂的清洁方式,另一种方式也是如此,而且更简单。

Add event listeners on your buttons so that when you click the first one you can trace the first element of your days array using days[0]

在按钮上添加事件侦听器,以便在单击第一个时可以使用days [0]跟踪days数组的第一个元素

the real way to do it is this.

真正做到这一点的方法就是这样。

var days: Array ["mon", "tues", "wed", "thurs", "fri"];
var buttons: Array = [ stage.getChildByName("mondayButton"), stage.getChildByName("tuesdayButton") ];
//you need buttons name 'mondayButton' in your stage or this will crash.

//then somewhere in your program you add event listener to all the buttons in your array like so
for(var i:int =0; i < buttons.Length; i++){
     var myButton:Button = buttons[i];
     myButton.AddEventListener(onButtonClicked,Event.Mouse_Click);
}

public onButtonClicked(MouseEvent e):void{
    var myClickedButton:Button = (e.target as Button);
    //e.target is who sends the event, in this case its your buttons
    //next you look what position this button takes in your button array and get get
    //the day in the array from its index
    var index:int = buttons.indexOf(myClickedButton);
    trace(days[index]);
}

Take note that this code is written from memory, dont copy paste. This is the more "complicated" method of doing what you want to do. Hope this helps :)

请注意,此代码是从内存中编写的,不要复制粘贴。这是做你想做的事情的更“复杂”的方法。希望这可以帮助 :)

#1


1  

Your Array was created wrong in your post. Try this:

您的数组在帖子中创建错误。试试这个:

var days:Array = ["mon", "tues", "wed", "thurs", "fri"];

or you could also create it like this, using the push() method of the Array class:

或者您也可以使用Array类的push()方法创建它:

var days:Array = new Array();
days.push( 'mon');
days.push( 'tues');
days.push( 'weds');
days.push( 'thurs');
days.push( 'fri');

to trace the values of the array in your post:

跟踪帖子中数组的值:

trace( days[0] ); // returns "mon"
trace( days[1] ); // returns "tues"
trace( days[2] ); // returns "wed"
trace( days[3] ); // returns "thurs"
trace( days[4] ); // returns "fri"

The array's contents are stored in the array's "indexes". An array's index always starts with 0.

数组的内容存储在数组的“索引”中。数组的索引始终以0开头。

Array's have a length. An empty array's length is 0. If the array has at least one item in it, the length is 1 and increases as you add more items. To loop through your array to get the values do this:

数组的长度。空数组的长度为0.如果数组中至少有一个项目,则长度为1,并在添加更多项目时增加。要遍历数组以获取值,请执行以下操作:

for(var i:int = 0; i < days.length; i++)
{
    trace( days[i] );
}

Arrays are a powerful and essential part of any programming language. You can remove items from the array, add items, remove a specific item at a specific index, remove an item by name, combine arrays, and lots more. You'd benefit from looking at this link and studying the properties and methods of the Array class. Once you get the hang of how to manipulate Array's you'll never now how you existed without them!

数组是任何编程语言的强大而重要的部分。您可以从数组中删除项目,添加项目,删除特定索引处的特定项目,按名称删除项目,组合数组等等。您将从查看此链接并研究Array类的属性和方法中受益。一旦你掌握了如何操纵数组的话,你现在就永远不会有没有它们的存在!

There are many ways to associate a button with a particular array index. The answer provided by Dr.Denis McCracleJizz is one way, although if you are as new to AS3 as you say, it might seem a little overwhelming as it uses several concepts you may not yet be familiar with. Let me see if I can simplify it a bit, although this will make the code a bit more lengthy:

将按钮与特定数组索引相关联的方法有很多种。 Dr.Denis McCracleJizz提供的答案是单向的,尽管如果你和AS3一样陌生,它可能看起来有点压倒性,因为它使用了一些你可能还不熟悉的概念。让我看看我是否可以简化它,虽然这会使代码更长一些:

mondayButton.addEventListener( MouseEvent.CLICK, onClickDayButton );
tuesdayButton.addEventListener( MouseEvent.CLICK, onClickDayButton );

function onClickDayButton( e:MouseEvent ):void
{
    if( e.target.name == 'mondayButton')
    {
        trace( days[0] );
    }
    else if( e.target.name == 'tuesdayButton')
    {
        trace( days [1] );
    }

    // and so on...
}

If you're familiar with objects you could create an object for each button that hold both the button and the button id and store those in an array:

如果您熟悉对象,则可以为每个按钮创建一个对象,该对象同时包含按钮和按钮ID,并将它们存储在数组中:

var days:Array = ["mon", "tues", "wed", "thurs", "fri"];

var dayButtonArray:Array = new Array();

var mondayButtonObject:Object = new Object();
mondayButtonObject.button = mondayButton;
mondayButtonObject.id = 0;
dayButtonArray.push( mondayButtonObject );


var tuesdayButtonObject:Object = new Object();
tuesdayButtonObject.button = tuesdayButton;
tuesdayButtonObject.id = 1;
dayButtonArray.push( tuesdayButtonObject );

// and like above, the rest of the days here

// then loop through them and set the mouseEvent

for(var i:int = 0; i < dayButtonArray.length; i++)
{
   dayButtonArray[i].button.addEventListener( MoouseEvent.CLICK, onClickDayButton );
}

// and the function each button calls to

function onClickDayButton( e:MouseEvent ):void
{
    trace( days[ evt.target.id ] );
}

You could further simplify the object method above by skipping the days array altogether and adding the day associated with the button right into the dayButtonArray instead of an id:

您可以通过完全跳过days数组并将与按钮关联的日期添加到dayButtonArray而不是id中来进一步简化上述对象方法:

var dayButtonArray:Array = new Array();

var mondayButtonObject:Object = new Object();
mondayButtonObject.button = mondayButton;
mondayButtonObject.day = "monday";
dayButtonArray.push( mondayButtonObject );


var tuesdayButtonObject:Object = new Object();
tuesdayButtonObject.button = tuesdayButton;
tuesdayButtonObject.day = "tuesday";
dayButtonArray.push( tuesdayButtonObject );

// and like above, the rest of the days here

// then loop through them and set the mouseEvent

for(var i:int = 0; i < dayButtonArray.length; i++)
{
   dayButtonArray[i].button.addEventListener( MoouseEvent.CLICK, onClickDayButton );
}

// and the function each button calls to

function onClickDayButton( e:MouseEvent ):void
{
    trace( evt.target.day );
}

Now were getting as complicated as Dr.Denis McCracleJizz's answer. His is definitely worth looking at as well.

现在变得像Dr.Denis McCracleJizz的回答一样复杂。他绝对值得一看。

#2


1  

Your array is a list of items, so your array is composed of the days of the week. You can access them by using an index like so.

您的数组是项目列表,因此您的数组由一周中的几天组成。您可以使用类似的索引来访问它们。

trace(days[0])
//Outputs: mon


trace(days[3])
//Outputs: thurs

Now for your buttons there is two way of doing it, the clean one that's more complicated and the other one that works just as well and that is more simple.

现在你的按钮有两种方式,一种是更复杂的清洁方式,另一种方式也是如此,而且更简单。

Add event listeners on your buttons so that when you click the first one you can trace the first element of your days array using days[0]

在按钮上添加事件侦听器,以便在单击第一个时可以使用days [0]跟踪days数组的第一个元素

the real way to do it is this.

真正做到这一点的方法就是这样。

var days: Array ["mon", "tues", "wed", "thurs", "fri"];
var buttons: Array = [ stage.getChildByName("mondayButton"), stage.getChildByName("tuesdayButton") ];
//you need buttons name 'mondayButton' in your stage or this will crash.

//then somewhere in your program you add event listener to all the buttons in your array like so
for(var i:int =0; i < buttons.Length; i++){
     var myButton:Button = buttons[i];
     myButton.AddEventListener(onButtonClicked,Event.Mouse_Click);
}

public onButtonClicked(MouseEvent e):void{
    var myClickedButton:Button = (e.target as Button);
    //e.target is who sends the event, in this case its your buttons
    //next you look what position this button takes in your button array and get get
    //the day in the array from its index
    var index:int = buttons.indexOf(myClickedButton);
    trace(days[index]);
}

Take note that this code is written from memory, dont copy paste. This is the more "complicated" method of doing what you want to do. Hope this helps :)

请注意,此代码是从内存中编写的,不要复制粘贴。这是做你想做的事情的更“复杂”的方法。希望这可以帮助 :)