actionscript-3flashoopflash-builderflash-cs5

Why is my button instance forgetting its textfield text and event listener?


I'm working on an assignment due at midnight tomorrow and I'm about to rip my hair out. I am fairly new to ActionScript and Flash Builder so this might be an easy problem to solve. I think I know what it is but I do not know for sure...

I'm developing a weather application. I designed the GUI in Flash CS5. The interface has 2 frames. The first frame is the menu which has a zipcode input and a button instance called "submit". On the second frame, it has another button instance called "change" which takes you back to the first frame, menu.

In Flash Builder 4, I wrote a class to extend that GUI called Application. When Main.as instantiates it, the constructor function runs. However, this was my first problem.

public class Application extends InputBase {
    public function Application() {
        super();
        
        // Y U NO WORK???
        this.gotoAndStop(1);
        this.change.tfLabel.text = "Change";
    }
}

When I ran debug it tossed a #1009 error saying it could not access the property or method of undefined object. It is defined on frame 2 in Flash CS5. I think this is the problem... Isn't ActionScript a frame based programming language? Like, you cannot access code from frame 2 on frame 1? But, I'm confused by this because I'm not coding on the timeline?

Anyway, I thought of a solution. It kinda works but its ugly.

public class Application extends InputBase {
    public function Application() {
        super();
        
        // Y U NO WORK???
        this.gotoAndStop(1); // when app is first ran, it will stop on the first frame which is the menu frame
        setButton(this.submit, "Submit", 1);
        setInput(this.tfZipCode);
    }
    
    private function submitZip(e:MouseEvent):void {
        this.nextFrame();
        setButton(this.change, "Change", 2);
    }
    
    private function menu(e:MouseEvent):void {
        this.prevFrame();
        setButton(this.submit, "Submit", 1); // if I comment this out, the submit button will return to the default text label and it forgets it event.
    }
    
    private function setButton(button:ButtonBase, string:String="", action:uint=0):void {
        button.buttonMode = true;
        button.mouseChildren = false;
        button.tfLabel.selectable = false;
        button.tfLabel.text = string;
        
        switch (action) {
            case 1:
                button.addEventListener(MouseEvent.CLICK, submitZip);
                break;
            case 2:
                button.addEventListener(MouseEvent.CLICK, menu);
                break;
            default:
                trace("Action code was invalid or not specified.");
        }
    }
}

Its not my cup of tea to run the set button function every time one of the button instances are clicked. Is this caused by frames or something else I maybe looking over?


Solution

  • I believe you have two keyframes & both have an instance of the button component called button.

    If you are working on the timeline try putting the button in a separate layer, with only one key frame.

    Something like the following:

    enter image description here

    Ensures that you are referencing the same button every time & not spawning new ones on each frame.