Actionscript 3.0:引用其他类的函数

时间:2013-07-22 19:34:29

标签: actionscript-3 function

我对引用它之外的类的引用函数感到有点困惑。我一直认为如果一个函数是一个公共函数,我可以继续从另一个类运行它。例如......

我正在尝试在新游戏中运行音频滑块。当我开始游戏时,音乐会很好。不幸的是,我的gameScreen类是文档类的子类。这意味着在我开始游戏之前,我的音频剪辑目前尚未添加到舞台上。我希望用户能够在玩游戏之前关闭声音(是的,即使它尚未播放,我认为这是一个很好的练习)。这意味着,如果他们直接进入选项屏幕,我需要我的音频滑块就在那里!现在,如果我开始玩,它只会添加到舞台上。

我解决这个问题的计划是进入我的选项屏幕类,然后如果运行选项屏幕功能,它会将音频剪辑添加到舞台上。这意味着我必须运行eventlistener。在这个eventlistener中,我想从我的Volume()类中引用一个函数....基本上是addToStage。我已经在我的Volume()课程中已经有了这个,我认为养成将功能从一个类复制到另一个类的习惯可能是浪费时间。所以我想,嘿,让我们去做那个功能,因为它是一个公共功能!

原来我收到错误“undefined property addToStage。”

那么我该怎样做才能使我不必将此功能直接重新复制到我的optionScreen类中?谢谢,我将在下面提供一些代码

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;
                public var yPos:Number;
                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);
                }

                /*

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

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

        }

}

OptionScreen类

   package  {

import flash.display.MovieClip;
import flash.display.SimpleButton;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.ui.Mouse;
import flash.net.SharedObject;


public class OptionScreen extends MovieClip {

    public var mainMenuButton:SimpleButton;
    private var new_Volume:Volume;

    public function OptionScreen() {

        Mouse.show();
        new_Volume = new Volume();
        new_Volume.onStage();
            mainMenuButtonOptions.addEventListener(MouseEvent.CLICK, onClickMainMenu,false,0,true);
        }

        public function onClickMainMenu(mouseEvent:MouseEvent):void
        {
            dispatchEvent( new NavigationEvent(NavigationEvent.MAINMENU));
        }
    }

}

1 个答案:

答案 0 :(得分:2)

公共函数只能在已经在内存中实例化的实例上访问。

例如:

var redBall:Ball = new Ball();
redBall.bounce();

这是有效的,因为我在内存中有一个Ball的实例,以及包含在其中的方法的有效路径。如果我省略了实例,它会抛出一个错误。

如果要在不实例化该类的情况下调用类上的方法,则需要静态方法。

package {   
    public class Ball {
        public static bounce():String {
            return "boing!"
        }
    }
}

既然方法是静态的,我可以直接在类上调用方法,而不是该类的实例。

Ball.bounce() // traces "boing!"