2013-07-21 24 views
0

所以我已经在actionscript 3几个星期了,但我仍然是一个完整的newb。我遇到的最困难的事情是将类链接到我的文档类。例如,我将拥有一个非常出色的类,它可以很好地完成任务(我可以将其作为另一个FLA的文档类插入,并提供该特定功能所需的所有功能),但现在当我必须插入它作为一个普通的类...我想“继承”文档类,都会下地狱。Actionscript 3.0:帮助链接文档类和音频滑块类

我知道你必须改变变量并实例化它以使其工作,并且我有点理解,但它有时只是让我头脑发热,我觉得他们应该是一个简单的解决方案,如果我已经有了一个完整的工作班。似乎常常有十亿件事需要改变。

无论如何,我有一个具体的例子,我希望有人能够帮助解释,并让我走了一段。我去网上找了一些滑块的代码,然后花了几个小时编辑它来包含我想要的MP3,循环它等。现在它在指定的FLA上效果很好......我只是将它作为文档类和弹出一个设计的音频滑块,改变音量,循环和一切。现在我想将这个滑块添加到我一直在努力的一个简单游戏中,但是不知道从哪里开始或做什么。现在我会保持简单。

说我只有我的空白文档类和我的音频滑块类。现在,当我运行我的游戏时,它会运行文档类,然后从那里直接运行我的音频滑块类。我认为如果我只是解决这个问题,我将能够将其实施到我的游戏中。所以这里是我的空白文档类和我的音频滑块类!谢谢您的帮助!

我已经试过

我试图在文档类的精灵和滑块创建公共变量,然后创建一个新的精灵/滑块一旦文档类运行。我认为要走在正确的轨道上,但是之后它开始看起来像我将不得不为音频滑块类中的几乎所有变量所做的那样。我也想......为什么我不能在文档类中运行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); 
       } 

     } 

} 
+0

我希望有人过来帮助你,但我也想问你是否考虑过使用其他IDE来开发AS3代码?如果你对舞台不熟悉,并且以多种(有时候是奇怪的)方式处理实例,也许你最好使用Flex/FlashBuilder(IDE而非框架)或FDT(Flash Developer Tools)或类似的东西只是代码(因为它看起来很适合那部分)。如果你使用动画的时间线等,你仍然可以做一个导入动画swfs在代码中使用。 – shaunhusain

+0

不,我没有。我的意思是......说实话,我只是在这几个星期,而且我比我开始的时候好得多。我的意思是我几乎有一个完整的工作游戏。所以我不认为使用这个框架是一个非常糟糕的框架,我认为它有一个陡峭的学习曲线。我经常喜欢它的结构,但理解它将会花更多的时间,我认为,这很好。我认为学习另一个IDE在这一点上会有点浪费。在我看来,这个结构没有任何问题,并且在正确使用时效果很好。 – spaderdabomb

+0

对不起@约翰认为你误读我的沟通,或者我写错了......我并不是说要使用其他框架或语言,只是说可能使用Flash IDE(Adobe用于编写AS3的两个主要工具之一isn因为你看起来非常以代码为中心,并且IDE倾向于更加直观地关注(通过时间线进行时间视觉翻译),而不是没有时间和仅基于代码中的事件/逻辑。你现在可以理解为可以在Flash中使用,无论哪种情况,你都必须处理不在舞台上的构造函数中 – shaunhusain

回答

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

从那里你可以去与一切照旧,并根据需要获取类范围内开展工作只是改变你的变量值播放器环境/文档类的限制。

+0

@M Sost谢谢YOUUUUU。很好的解释,完美的工作(除了你的舞台功能第二次没有大写!!! = pp)。我想我有点了解如何现在实施更好一点的课程。只需创建它们的一个实例并确保它们被添加到舞台中,并且该类中的功能根据已定义的内容进行操作。 THANKSSS – spaderdabomb

+0

哎呀,赶上!编辑来解决这个问题。另外,我会稍微改变一下:“简单地创建它们的一个实例并确保它们的功能符合已定义的内容。”如果你不需要对舞台的引用(也许是一个工具类或其他不是基于舞台的),那么你就不需要等到舞台上了:)。很高兴我能帮上忙! –