Question

I recently discovered the custom classes in actionscript 3. I started using them in my latest project but I find it hard to bend my brain around how it all works.
I created two different classes to test.
One is an extended movieclip called "Persoon" and the other is an extended simplebutton called "SpeakerBtn".

package  {
import flash.display.Sprite;

public class Persoon extends Sprite {

    public function Persoon(xPos:Number, yPos:Number, naam:String) {
        var persoon:Sprite = new Sprite;
        persoon.graphics.beginFill(0x000000,1);
        persoon.graphics.drawCircle(xPos, yPos, 2);
        persoon.graphics.endFill();
        this.addChild(persoon);
        trace ("hij heet " + naam);
    }

}

}

package  {

import flash.display.SimpleButton;
import flash.events.MouseEvent;
import flash.media.Sound;
import flash.media.SoundChannel;


public class SpeakerBtn extends SimpleButton {
    public var snd:Sound;
    public var cnl:SoundChannel = new SoundChannel();

    public function SpeakerBtn(xp:Number,yp:Number,naam:String) {
        var speaker:SimpleButton = new SimpleButton();
        speaker.name = naam;
        speaker.x = xp;
        speaker.y = yp;
        speaker.addEventListener(MouseEvent.CLICK, playSnd);

        //this.addChild(speaker);
    }

    public function playSnd (event:MouseEvent) : void {
        trace ("ping");
    }
}

}


Then I have my main:

package  {

import flash.display.MovieClip;
import SpeakerBtn;
import flash.display.SimpleButton;
import Persoon;

public class Main extends MovieClip {
    var sp:SpeakerBtn;
    var ps:Persoon;

    public function Main() {
        sp = new SpeakerBtn(50,50,"donna");         
        addChild(sp);

        ps = new Persoon(300,300,"wilf");
        addChild(ps);
    }
}

}

Persoon wilf works like I expected, displays fine and traces correctly.

SpeakerBtn donna does not display and does not trace correctly. I commented out the addChild in the SpeakerBtn package because if I turn it on, I get the error 1061: Call to a possibly undefined method addChild through a reference with static type SpeakerBtn

I noticed that when I define the x and the y and addChild in Main for the speakerBtn it does work. But I don't want to have to define all that in Main, I want my SpeakerBtn to do all that.

I checked this question but it does not provide me with an answer. Can someone explain to me what is happening, or alternatively link me to a comprehensible tutorial (one not too heavy on techspeak, more like an explain-it-to-me-like-I'm-5-years-old)? Thanks!

Update
I forgot to add a button with the class SpeakerBtn to my library, so there was nothing to display. Fixed that now, and with this code the button does appear on the stage, only the x and y values are not registered and it appears on 0,0. Also, the event playSnd does not trigger the trace and I assume is not working.

Solution With help of Cherniv's information I came to the following solution for my SpeakerBtn.

Main does this:

package  {

    import flash.display.MovieClip;
    import SpeakerBtn;
    import flash.display.SimpleButton;

    public class Main extends MovieClip {
        var sp:SpeakerBtn;

        public function Main() {
            sp = new SpeakerBtn("donna", 300, 50);
            addChild(sp);
        }
    }   
}

And SpeakerBtn does this:

package  {

    import flash.display.SimpleButton;
    import flash.events.MouseEvent;
    import flash.media.Sound;
    import flash.media.SoundChannel;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.net.URLRequest;


    public class SpeakerBtn extends SimpleButton {
        private var snd:Sound;
        private var cnl:SoundChannel = new SoundChannel();
        private var _naam:String;
        private var _x:Number;
        private var _y:Number;

        public function SpeakerBtn(naam:String, xp:Number, yp:Number) {
            _naam = naam;
            _x = xp;
            _y = yp;
            addEventListener(Event.ADDED_TO_STAGE, addBtn);
        }

        private function addBtn (event:Event) : void {
            this.x = _x;
            this.y = _y;
            this.name = _naam;
            snd = new Sound(new URLRequest("mp3/" + _naam + ".mp3"));
            addEventListener(MouseEvent.CLICK, playSnd);
        }
        private function playSnd (event:MouseEvent) : void {
            cnl = snd.play();
        }
    }   
}

So what I did was add an EventListener for when the button was added to the stage and then set all the variables like x-position, y-position and name.

Was it helpful?

Solution

That's because of inheritance. Your SpeakerBtn doesn't inherits the addChild method from his ancestors , because as we can see in SimpleButton's documentation it is inheritor of DisplayObject and not of DisplayObjectContainer , which do have a addChild method and passes it to all his inheritors including MovieClip and Persoon.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top