1

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.

Community
  • 1
  • 1
silvith
  • 290
  • 2
  • 14

1 Answers1

0

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.

Ivan Chernykh
  • 41,617
  • 13
  • 134
  • 146
  • The documentation says also ~The DisplayObject class supports basic functionality like the x and y position of an object~ The why does it appear on position 0,0 when I pass position values through the constructor function? And why does the eventListener not work? – silvith Aug 06 '13 at 07:55
  • @silvith you do not need to create a `SimpleButton` in a `SpeakerBtn` , because your `SpeakerBtn` **is** `SimpleButton` cause it extends `SimpleButton`. So you need to attach a listener in the `Main` constructor , like: `sp = new SpeakerBtn(50,50,"donna"); addChild(sp); sp.addEventListener(MouseEvent.CLICK, sp.playSnd);` – Ivan Chernykh Aug 06 '13 at 08:09
  • @silvith another way to attach the listener is: `this.addEventListener(MouseEvent.CLICK, playSnd);` – Ivan Chernykh Aug 06 '13 at 08:49
  • thank you, I think I understand a little more of this matter now. – silvith Aug 06 '13 at 11:31
  • @silvith great! Inheritance is a "corner stone" of Object Oriented Programming , so it is very important to be patient and tenacious when you're starting to learn it.. – Ivan Chernykh Aug 06 '13 at 11:38