【问题标题】:AS3 I don't understand the different treatment of an extended movieclip class vs extended simplebutton classAS3 我不明白扩展的movieclip 类与扩展的simplebutton 类的不同处理方式
【发布时间】:2013-08-06 07:24:18
【问题描述】:

我最近在 actionscript 3 中发现了自定义类。我开始在我的最新项目中使用它们,但我发现很难思考它是如何工作的。
我创建了两个不同的类来测试。
一个是名为“Persoon”的扩展影片剪辑,另一个是名为“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");
    }
}

}


然后我有我的主要:

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 像我预期的那样工作,显示良好且跟踪正确。

SpeakerBtn donna 不显示且不正确跟踪。我在 SpeakerBtn 包 中注释掉了 addChild,因为如果我打开它,我会收到错误 1061: Call to a possible undefined method addChild through a reference with静态类型 SpeakerBtn

我注意到,当我在 Main 中为 speakerBtn 定义 x 和 y 和 addChild 时,它确实有效。但我不想在 Main 中定义所有这些,我希望我的 SpeakerBtn 完成所有这些。

我检查了this question,但它没有为我提供答案。有人可以向我解释发生了什么,或者将我链接到一个易于理解的教程(一个不太注重技术语言的教程,更像是一个向我解释——我是 5 岁)?谢谢!

更新
我忘记在我的库中添加一个带有 SpeakerBtn 类的按钮,所以没有什么可显示的。现在修复了这个问题,使用此代码,按钮确实出现在舞台上,只有 x 和 y 值未注册,它出现在 0,0 上。此外,事件 playSnd 不会触发跟踪,我认为它不起作用。

解决方案 在 Cherniv 的信息的帮助下,我为我的 SpeakerBtn 找到了以下解决方案。

主要是这样做的:

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);
        }
    }   
}

SpeakerBtn 这样做:

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();
        }
    }   
}

所以我所做的就是为按钮添加到舞台时添加一个 EventListener,然后设置所有变量,如 x 位置、y 位置和名称。

【问题讨论】:

    标签: actionscript-3 flash class flash-cs6 simplebutton


    【解决方案1】:

    那是因为继承。您的 SpeakerBtn 没有从他的祖先那里继承 addChild 方法,因为正如我们在 SimpleButtondocumentation 中看到的那样,它是 DisplayObject 的继承者,而不是 DisplayObjectContainer 的继承者,它确实有 @ 987654322@ 并将其传递给他的所有继承人,包括MovieClipPersoon

    【讨论】:

    • 文档还说~DisplayObject类支持对象的x和y位置等基本功能~当我通过构造函数传递位置值时,为什么它会出现在位置0,0上?以及为什么 eventListener 不起作用?
    • @silvith 你不需要在SpeakerBtn 中创建SimpleButton,因为你的SpeakerBtn SimpleButton 因为它扩展了SimpleButton。所以你需要在Main构造函数中附加一个监听器,比如:sp = new SpeakerBtn(50,50,"donna"); addChild(sp); sp.addEventListener(MouseEvent.CLICK, sp.playSnd);
    • @silvith 附加监听器的另一种方式是:this.addEventListener(MouseEvent.CLICK, playSnd);
    • 谢谢你,我想我现在对这件事有点了解了。
    • @silvith 太棒了!继承是面向对象编程的“基石”,所以在开始学习它时保持耐心和顽强是非常重要的。
    猜你喜欢
    • 2013-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多