所以我已经在动作脚本3上进行了几个星期但是我仍然是一个完整的新手。我遇到的最大困难是将类链接到我的文档类。例如,我将有一个非常好的类,可以很好地完成任务(我可以将它作为另一个FLA的文档类插入,它将提供我需要的特定功能的所有功能),但现在当我必须插入它作为一个普通的类...我猜“继承”文档类,一切都到了地狱。
我知道你必须改变变量并实例化它才能让它发挥作用我有点理解,但有时它只是让我头脑发热,我觉得它们应该是一个简单的解决方案,如果我已经有了全班工人。似乎经常需要转换十亿件事。
无论如何,我有一个具体的例子,我希望有人可以帮助解释并引导我一点点。我上网并找到一些滑块的代码,然后花了最后几个小时编辑它来包含我想要的mp3,循环它等等。现在它在指定的FLA上工作得很好...我只是运行它作为文档类和向上弹出一个设计的音频滑块,可以改变音量,循环和一切。现在我想将这个滑块添加到我一直在做的简单游戏中,但是不知道从哪里开始或者做什么。现在我会保持简单。
说我只有我的空白文档类和我的音频滑块类。现在当我运行我的游戏时,它当然会运行文档类,从那里,我希望它直接运行我的音频滑块类。我想如果我解决这个问题,我将能够在游戏中实现它。所以这是我的空白文档类和我的音频滑块类!谢谢你的帮助!
我做了什么
我试图在精灵和滑块的文档类中创建公共变量,然后在文档类运行时创建一个新的精灵/滑块。我认为这是在正确的轨道上,但后来开始看起来我将不得不为音频滑块类中的几乎所有变量做到这一点。我也想过......为什么我不能在Document Class中运行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 :(得分:2)
看起来你的Volume
课程就像你说的那样,大部分都是完整且独立的。这很好,因为它将使您在文档类中实例化它的新实例更容易。
在文档类中,要实例化一个新类,您可以执行以下操作:
var new_volume:Volume = new Volume();
addChild(new_volume);
重要的是要注意,stage
类不在Volume
类范围内,直到您从其父类中将其添加到舞台中为止(在此case,它的父类是文档类。)
所以这两行:
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();
}
从那里你可以继续照常营业,只需根据需要改变你的变量值,让课程在你的玩家环境/文档类的范围内工作。