0
votes

Note this has to be done in ActionScript 2 due to my audience player restricted to flash player 8.

I'm trying to build a simple class that extends the base Movieclip class. I'm then applying this class to a movieclip in the library using the properties panel and giving it the class name in there. That much is working fine, and I'm able to use my class and methods etc. on that object after I've attached the movieclip tot he stage using attachMovie. What I can't seem to get to work is the button that I have within the movieclip. Originally I had a button set up within the library movieclip, however I can't seem to target that within my class. What I would like to do is simply set up a simple getURL type of thing within the button, but it doesn't seem to work.

I tried just assigning the onMouseDown handler within the class rather than a button, and that did work, but happened wherever I clicked on the stage rather than just over the graphic area. This had the effect of triggering all the other handlers on any other instances of the class that I had on the stage, so they all fire off at once. However, using onRollOver and onRollOut did work as I expected.

I've also tried setting up another button in the library and using this.attachMovie with my class to attach it, but I still can't seem to get a handle for it.

I've created a very simple test script to try and illustrate what I'm trying to do:-

class Test extends MovieClip {

    public function Test() {
        // constructor code
        this.attachMovie("theButton" , "myButton" , _root.getNextHighestDepth() )
        this.myButton.onMouseDown = clickMe;
        this._x = Math.random() * 800;
        this._y = Math.random() * 400;
    }


    public function clickMe() {
        trace("I have been clicked"+this._name);
    }

}

So, the question is, how do I attach (or have present in the movieclip) a button, that I can provide an onMouseDown or onMouseUp type handler for that button that will only work if I click on the actual button? What am I doing wrong here ... I've tried loads of different ideas, but nothing seems to be working. Remember it's AS2 not AS3. Thanks.

1
it's been a while for as2, but did you try this.myButton.onRelease = clickMe;? and you don't have any click events on instances of Test do you? (side note: use this.getNextHighestDepth(), not _root, get the next depth of what you're attaching to)zamnuts
the line this.myButton.onMouseDown gives me an error saying there is no property with the name 'myButton'. I do understand why it's saying that, but I can't get to grips with how attachMovie works within a class and how I get a property from it that I can target and use.Darren Crabb
Thanks Zamnuts ... it looks as if just using this.onRelease makes a difference if I'm using a button that exists within my movieclip (with the class on) in the library and I just use the onRelease at the class level rather than trying to attach it to the button. I find that a bit odd but at least it seems to be working.Darren Crabb

1 Answers

1
votes

Three points:

  1. Remember, attachMovie returns a reference to the newly attached movieclip; use this to your advantage.

  2. Also, onRelease is the preferred way, it is most associated with a click, see this SO post "What is the difference between the onMouseUp/Down and onPress/Release events in Flash?".

  3. Lastly, when getting the next highest depth (with getNextHighestDepth), remember each MovieClip has its own depth system (even though, internally there is a single global one), so always find the depth relative to the MC you're attaching too. Since attachMovie is being called on this, then getNextHighestDepth should be called on this.

Updated Code:

class Test extends MovieClip {
    public function Test() {
        var btn = this.attachMovie("theButton" , "myButton" , this.getNextHighestDepth() );
        // "bnt" is now the button instance, while "this" is still the Test mc
        btn.onRelease = btn.onReleaseOutside = this.clickMe; // i roped in onReleaseOutside, but this might not be necessary
        this._x = Math.random() * 800;
        this._y = Math.random() * 400;
    }
    public function clickMe() {
        trace("I have been clicked"+this._name);
    }
}