0

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

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

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

原来我收到一个错误“未定义的属性 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));
        }
    }

}
4

1 回答 1

2

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

例如:

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

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

如果你想在一个类上调用一个方法而不实例化那个类,你需要一个静态方法。

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

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

Ball.bounce() // traces "boing!"
于 2013-07-22T19:58:10.013 回答