0

所以我已经在 actionscript 3 上进行了几个星期了,但我仍然是一个完整的新手。我遇到的最大困难是将类链接到我的文档类。例如,我将有一个很棒的类,它做得非常好(我可以将它作为另一个 FLA 的文档类插入,它会提供该特定功能所需的所有功能),但现在当我必须插入它作为一个普通类......我想“子类化”文档类,一切都下地狱了。

我知道您必须更改变量并实例化事物才能使其正常工作,我有点理解这一点,但有时它只是让我无法理解,如果我已经拥有一个完整的工人阶级,我觉得它们应该是一个简单的解决方案. 似乎经常有十亿件事情我需要切换。

无论如何,我有一个具体的例子,我希望有人可以帮助解释并引导我了解一下。我上网找到了一些滑块的代码,然后花了最后几个小时对其进行编辑以包含我想要的 mp3、循环播放等等。现在它在指定的 FLA 上运行良好......我只是将它作为文档类和向上弹出一个设计的音频滑块,可以改变音量、循环和所有内容。现在我想将此滑块添加到我一直在开发的一个简单游戏中,但不知道从哪里开始或做什么。不过现在我会保持简单。

假设我只有空白文档类和音频滑块类。现在,当我运行我的游戏时,它当然会运行文档类,然后我希望它直接运行我的音频滑块类。我想如果我能解决这个问题,我就能将它应用到我的游戏中。所以这是我的空白文档类和我的音频滑块类!谢谢您的帮助!

我试过的

我尝试在文档类中为精灵和滑块创建公共变量,然后在文档类运行后创建一个新的精灵/滑块。我认为这是在正确的轨道上,但后来看起来我必须对音频滑块类中的几乎所有变量都这样做。我还想……为什么我不能在 Document Class 中运行 Volume() 呢?仍然让我有些困惑,为什么这不起作用,但事实并非如此。

空白文档类

package  {

    import flash.display.MovieClip;
    import flash.display.Sprite;


    public class ASDocumentClass extends MovieClip {

        public function ASDocumentClass() {

        }
    }

}

这是音频滑块类

package {

        import flash.display.Sprite;
        import flash.display.Graphics;
        import flash.events.MouseEvent;
        import flash.events.Event;
        import flash.net.URLRequest;
        import flash.media.Sound;
        import flash.media.SoundChannel;
        import flash.media.SoundTransform;
        import flash.geom.Rectangle;

        public class Volume extends Sprite {

                public var snd:Sound = new Sound();
                public var channel:SoundChannel = new SoundChannel();
                //URLRequest=new URLRequest("solitude.wav");
                //Make sure you pass URLRequest an audio file on your computer.
                public var req:BackgroundMusic = new BackgroundMusic();
                public var boundary:Rectangle;
                public var sprite:Sprite;
                public var slider:Sprite;
                public var xPos:Number=stage.stageWidth/2;
                public var yPos:Number=stage.stageHeight/2;
                public var vol:Number;

                /*
                Our request is loaded into the sound object and plays through
                our channel. Volume is initially set at 50% and passed as a
                transformation to our our channels soundTransform property
                (a fancy way of saying volume). The init() function is called.  
                */

                public function Volume() {
                        channel=req.play();
                        channel.addEventListener( Event.SOUND_COMPLETE, onBackgroundMusicFinished,false,0,true );
                        vol=.5;
                        channel.soundTransform=new SoundTransform(vol);
                        init();
                }

                /*

                The init function creates and draws a rectangle and circle
                to the stage and centers them based on the height and
                width of the stage. In addition, a rectangle object is
                created to 'contain' the sliding circle, like an imaginary box.
                We pass -100 as the x value because it is added relative
                to our sprite. If we set its x value at 0, or the sprites default x
                value,the boundary would stop and start at the slider sprite. Change
                -100 to 0 in the rectangle object to get a better idea of its use.

                */

                public function init():void {
                        sprite = new Sprite();
                        sprite.graphics.beginFill(0x999999);
                        sprite.graphics.drawRect(xPos,yPos,200,5);
                        sprite.graphics.endFill();
                        addChild(sprite);
                        sprite.x-=sprite.width/2;
                        slider = new Sprite();
                        slider.graphics.beginFill(0xFF0000);
                        slider.graphics.drawCircle(xPos,yPos, 20);
                        slider.graphics.endFill();
                        addChild(slider);
                        slider.addEventListener(MouseEvent.MOUSE_DOWN, dragSlider);
                        stage.addEventListener(MouseEvent.MOUSE_UP, stopSlider);
                        boundary=new Rectangle(-100,0,200,0);
                }

                /*

                dragSlider runs when the use holds the mouse button down. A
                startDrag method is used on our sprite where we specify boundary
                as our dragging limits. A new event handler designed
                to change the mouse volume is subsequenlty called per frame, where
                the slider.x property determines volume.

                */

                public function dragSlider(event:MouseEvent):void {
                        slider.startDrag(false,boundary);
                        slider.removeEventListener(MouseEvent.CLICK, dragSlider);
                        slider.addEventListener(Event.ENTER_FRAME, changeVolume);
                }

                /*

                Stops dragging and removes the event listener to save on space. Again,
                volume will be based on the sliders current x position, which is
                constantly being recalculated per frame because we used an
                ENTER_FRAME event.

                */

                public function stopSlider(event:MouseEvent):void {
                        slider.stopDrag();
                        slider.removeEventListener(MouseEvent.MOUSE_UP, stopSlider);
                }

                /*

                This function is constantly recalculating the vol variable
                based on the sliders x position, relative to the length of
                our rectangle. Creates a decimal range from 0 to 1, where 1
                represents 100% volume and 0 represents mute. Anything exceeding
                100% causes distortion.

                */

                public function changeVolume(event:Event):void {
                        vol=.5+Math.round(slider.x)/200;
                        channel.soundTransform=new SoundTransform(vol);
                }

                public function onBackgroundMusicFinished(event:Event):void
                {
                    channel = req.play();
                    channel.addEventListener( Event.SOUND_COMPLETE, onBackgroundMusicFinished );
                }

        }

}
4

1 回答 1

2

看起来你的Volume课程就像你说的那样,大部分都是完整的和独立的。这很好,因为它将使在您的文档类中实例化它的新实例变得更加容易。

在文档、类中,要实例化一个新类,您可以执行以下操作:

var new_volume:Volume = new Volume();
addChild(new_volume);

重要的是要注意,直到您从它的父类中将它添加到阶段(在这种情况下,它的父类是文档类),它stage才会进入您的Volume类的范围内。

所以这两行:

public var xPos:Number=stage.stageWidth/2;
public var yPos:Number=stage.stageHeight/2;

不工作,因为舞台在那里未定义。要等到你知道stage被定义,你可以使用Event.ADDED_TO_STAGE事件监听器。所以你可以重新编写你的Volume类,让它看起来更像这样:

package {

    /* Imports here */

    public class Volume extends Sprite {
            /* Other vars here */
            public var xPos:Number;
            public var yPos:Number;

            public function Volume(){       
                    /* Other assignments that are not stage-dependant can go here */
                    this.addEventListener(Event.ADDED_TO_STAGE, onStage);
            }

            private function onStage(e:Event):void{
                    //We remove it immediately so that it doesn't get called multiple times
                    //As the instance is added to the display list tree
                    this.removeEventListener(Event.ADDED_TO_STAGE, onStage);

                    xPos = stage.stageWidth/2;
                    yPos = stage.stageHeight/2;

                    /* Now that we have a reference to the stage, let's go ahead and create our slider */
                    init();
            }

从那里您可以照常进行业务,只需根据需要更改您的变量值,以使该类在您的播放器环境/文档类的范围内工作。

于 2013-07-21T20:25:56.330 回答