Nov 08

So…  I’ve decided to take a dive in to ActionScript in an attempt to be able to create small little Flash-based utilities for inclusion in to some of our eLearning modules.  Of course, having an application seems to be the best way to really learn something like this, at least it is for me, as I then have a vision of what I expect the end-result to be.

I decide to create a simple little graphical tool which allows one to get a grasp, or understanding, on how to set a MAC address on a card via a bank of dip switches.  Seems simple enough.

As you can see, just a series of switches that can be displayed on the screen, the learner clicks on a switch, it changes state and displays itself as such graphically, and updates the MAC Address display to either add or subtract it’s Base2 value from the total displayed.

So, if switch 1 and 7 are set to ON, the value displayed should be 65.  Again…  seems simple enough.

At least until I try to implement.

I create a simpleButton on the stage and convert it to a symbol.  I then edit the symble for this button as the following images show

Button_UP depicts the switch in the OFF position and Button_DOWN depicts the switch in the ON position.

I drop seven instances of this one switch on the stage and give each of them their own name, sw1_btn, sw2_btn, sw3_btn, etc.

Now, I create an Actions layer and begin plugging in my code.

address_txt.text = "0";
sw1_btn.addEventListener(MouseEvent.CLICK, sw1);
sw2_btn.addEventListener(MouseEvent.CLICK, sw2);
sw3_btn.addEventListener(MouseEvent.CLICK, sw3);
sw4_btn.addEventListener(MouseEvent.CLICK, sw4);
sw5_btn.addEventListener(MouseEvent.CLICK, sw5);
sw6_btn.addEventListener(MouseEvent.CLICK, sw6);
sw7_btn.addEventListener(MouseEvent.CLICK, sw7);
function sw1(event:MouseEvent) {
    address_txt.text = "1";
}
function sw2(event:MouseEvent) {
    address_txt.text = "2";
}
function sw3(event:MouseEvent) {
    address_txt.text = "3";
}
function sw4(event:MouseEvent) {
    address_txt.text = "4";
}
function sw5(event:MouseEvent) {
    address_txt.text = "5";
}
function sw6(event:MouseEvent) {
    address_txt.text = "6";
}
function sw7(event:MouseEvent) {
    address_txt.text = "7";
}

That ultimately gets me this:

So I’m close(er).  But here is what’s stumping me.  I’m most certain there are several better ways of doing this but these are my questions.

1) My original thought was to have each eventListener call the same function, however, I can’t seem to figure out how to pass something to that function to let me know which switch has been pressed so as to know the value to add or subtract from the MacAddress text field.

2) How can I get simpleButton to act like a toggle?  By default, it seems to act like a momentary switch — push and release and it presses and depresses or DOWN and then UP.  I want to be able to CLICK the switch/button and it remain in that new state until I CLICK on it again.   Once that is figured out, I then will have to evaluate the state of the switch/button after the CLICK so I can add/subtract the value from the MacAddress text field.

3) From within a function, how can I determine which listener was the sending listener?  This is sort of the same question as my first question, but I’m curious of there is a way to determine from the CLICK event which listener fired it.

Anyway…  I’m getting closer, but I still have a ways to go.  I’m combing though all things Flash at Lynda.com to see if I can locate what I need, and have been combing the AS Reference Guide at Adobe.  Sooner or later I’ll “get it.”  Until then though, if anyone has any helpful tips, I’m open to them!  :-)

Comments are closed.

preload preload preload