所以我已经在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);
}
}
}
我希望有人过来帮助你,但我也想问你是否考虑过使用其他IDE来开发AS3代码?如果你对舞台不熟悉,并且以多种(有时候是奇怪的)方式处理实例,也许你最好使用Flex/FlashBuilder(IDE而非框架)或FDT(Flash Developer Tools)或类似的东西只是代码(因为它看起来很适合那部分)。如果你使用动画的时间线等,你仍然可以做一个导入动画swfs在代码中使用。 – shaunhusain
不,我没有。我的意思是......说实话,我只是在这几个星期,而且我比我开始的时候好得多。我的意思是我几乎有一个完整的工作游戏。所以我不认为使用这个框架是一个非常糟糕的框架,我认为它有一个陡峭的学习曲线。我经常喜欢它的结构,但理解它将会花更多的时间,我认为,这很好。我认为学习另一个IDE在这一点上会有点浪费。在我看来,这个结构没有任何问题,并且在正确使用时效果很好。 – spaderdabomb
对不起@约翰认为你误读我的沟通,或者我写错了......我并不是说要使用其他框架或语言,只是说可能使用Flash IDE(Adobe用于编写AS3的两个主要工具之一isn因为你看起来非常以代码为中心,并且IDE倾向于更加直观地关注(通过时间线进行时间视觉翻译),而不是没有时间和仅基于代码中的事件/逻辑。你现在可以理解为可以在Flash中使用,无论哪种情况,你都必须处理不在舞台上的构造函数中 – shaunhusain