tailedmouse
tailedmouse

Reputation: 365

need some array help in actionscript 3

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

so I have 5 buttons...

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

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

Upvotes: 0

Views: 142

Answers (2)

Ribs
Ribs

Reputation: 1505

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:

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.

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:

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!

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:

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:

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:

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.

Upvotes: 1

Dr.Denis McCracleJizz
Dr.Denis McCracleJizz

Reputation: 870

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]

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 :)

Upvotes: 1

Related Questions