我对数组的概念很新...所以我需要帮助理解并执行它....
所以我有5个按钮......
星期一 - 星期五现在当有人按下其中一个按钮时,我希望程序跟踪仅按下的那一天..有人可以一步一步地解释我该怎么做以及为什么?
我正在使用flash actionscript 3 .. 这几乎是我唯一知道该做的事情......我尝试了一些onlint tuts,但它们不是很清楚
var days: Array ["mon", "tues", "wed", "thurs", "fri"];
答案 0 :(得分:1)
您的数据在帖子中创建错误。试试这个:
var days:Array = ["mon", "tues", "wed", "thurs", "fri"];
或者您也可以使用Array类的push()方法创建它:
var days:Array = new Array();
days.push( 'mon');
days.push( 'tues');
days.push( 'weds');
days.push( 'thurs');
days.push( 'fri');
跟踪帖子中数组的值:
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"
数组的内容存储在数组的“索引”中。数组的索引始终以0开头。
数组的长度。空数组的长度为0.如果数组中至少有一个项目,则长度为1,并在添加更多项目时增加。要遍历数组以获取值,请执行以下操作:
for(var i:int = 0; i < days.length; i++)
{
trace( days[i] );
}
阵列是任何编程语言中强大而重要的部分。您可以从数组中删除项目,添加项目,删除特定索引处的特定项目,按名称删除项目,组合数组等等。您将从查看此link并研究Array类的属性和方法中受益。一旦你掌握了如何操纵数组,你就永远不会有没有它们的存在!
将按钮与特定数组索引相关联的方法有很多种。由 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...
}
如果您熟悉对象,则可以为每个按钮创建一个对象,该对象同时包含按钮和按钮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 ] );
}
您可以通过完全跳过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 );
}
现在变得像 Dr.Denis McCracleJizz的回答一样复杂。他绝对值得一看。
答案 1 :(得分:1)
您的数组是项目列表,因此您的数组由一周中的几天组成。 您可以使用索引来访问它们。
trace(days[0])
//Outputs: mon
trace(days[3])
//Outputs: thurs
现在你的按钮有两种方式,一种是更复杂的清洁,另一种是同样有效的,更简单。
在按钮上添加事件侦听器,以便在单击第一个时可以 使用days [0]
跟踪days数组的第一个元素这样做的真正方法就是这样。
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]);
}
请注意,此代码是从内存中写入的,不要复制粘贴。这是做你想做的事情的更“复杂”的方法。希望这会有所帮助:)