无法扩展Sound对象原型

时间:2012-10-19 23:31:12

标签: actionscript-3 flash object audio prototype

我是编程新手。我需要向Sound对象添加新属性,但我无法使其工作。这就是我正在做的事情:

S31 = new Sound();
S31.load(new URLRequest("mp3/S31.mp3"));
Sound.prototype.correctas = 0;
trace(S31.correctas);

我收到此错误消息:

“1119:通过静态类型flash.media:Sound”的引用访问可能未定义的属性。

我不知道该怎么办。

感谢您的时间。

1 个答案:

答案 0 :(得分:1)

原型类确实没有在as3中使用(如果我在这里错了,请有人纠正我,我知道它的包含但是我不确定你为什么要使用它。)

您可以创建dynamic类,允许您在运行时向它们添加属性,但在这种情况下,我会坚持使用OOP。

您要做的是创建一个扩展Sound的类,并保存您想要包含的任何扩展功能。这个新类将inherit基本Sound类的所有功能。

尝试creating a new AS3 class并让它扩展声音

package src {
import flash.media.Sound;
import flash.media.SoundLoaderContext;
import flash.net.URLRequest;

    public class MySound extends Sound{

    public var correctas:Number; //assuming you are using a Number here          

    //sound takes two params in its constructor 
    public function MySound(stream:URLRequest=null, context:SoundLoaderContext=null){
    //super passes these params to the super class
        super(stream, context);
     }
}

现在使用它你将创建一个新的MySound对象而不是Sound

 var s31:MySound = new MySound();
 s31.load(new URLRequest("mp3/S31.mp3"));
 s31.correctas = 0;
 trace(s31.correctas) //will be 0
相关问题