2013-07-22 34 views
0

我對引用它的引用之外的類引用函數有點困惑。我一直認爲,如果一個函數是一個公共函數,我可以繼續從另一個類中運行它。例如...Actionscript 3.0:引用來自其他類的函數

我試圖運行我的新遊戲音頻滑塊。當我開始遊戲時音樂就很好。不幸的是,我的gameScreen類是文檔類的子類。這意味着我的音頻剪輯目前沒有添加到舞臺上,直到我開始遊戲。我希望用戶能夠在播放遊戲之前關閉聲音(雖然它還沒有播放,但我認爲這是一個很好的練習)。這意味着,如果他們直接進入選項屏幕,我需要我的音頻滑塊在那裏!現在,如果我開始玩,它只會添加到舞臺上。

我的計劃來解決,這是進入我的選擇屏幕的類,然後如果運行選項屏功能,它添加音頻剪輯階段。這意味着我必須運行一個eventlistener。在這個eventlistener中,我想參考我的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)); 
     } 
    } 

} 
+0

只發布有關該問題的代碼。 SO不是論壇。 – Pier

+0

另外,addToStage定義在哪裏? – Pier

回答

2

公共功能只對一個已經在內存中實例化的實例訪問。

例如:

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

這工作,因爲我在內存有Ball一個實例,以及其中包含的方法的有效路徑。如果我省略了這個實例,它會拋出一個錯誤。

如果要調用一個類的方法,而不實例化類,需要一個靜態方法。

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

既然該方法是靜態的,我可以直接調用該類的方法,而不是該類的一個實例。

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

嗯好吧......我想我已經進步了,幾乎明白你在說什麼......我改變了我在主要問題主體中的optoins課程中所做的事情。它或者說我沒有足夠的輸入參數(對於我所擁有的),或者如果我輸入參數,它也會返回一個錯誤。我會在括號中加入什麼? è?不,不喜歡那種......我不知道它在尋找什麼樣的輸入參數 – spaderdabomb

+0

Atriace在說什麼(in engligh)是你的對象在你使用'new'之前不存在。另外,使用'static'意味着變量/函數駐留在類實體中,而不是在實例中。該實例是您在使用'new'時創建的對象。 – Pier

+0

我建議打開「許可調試」(http://www.edchipman。CA /資產/博客/ 2008-08-09/publishsettings.png)。這會告訴你代碼的哪一行有問題。此外,嘗試通過按住Ctrl + Shft + Enter在調試模式下進行編譯。您調用的方法顯然需要比您傳遞的更多參數。例子'myFunc()'可能需要兩個參數,所以我通過調用'myFunc(arg1,arg2)'來解決這個問題。這是一個外部問題(不在函數內部)。 'e:Event'只是標記參數變量,這是一個實踐問題。你可以很容易地命名你的事件var myEvent:Event' – Atriace