我要做的是获取4个movieclipps(leaf1,leaf2,leaf3,leaf4)来播放附加到另一个动画片段(NatureTarget)时附加到它们的声音并按下播放按钮以便声音按照它们被拖动的顺序播放。我知道我需要使用数组和推送功能以及循环......但我迷失了。任何帮助将非常感谢。感谢。
答案 0 :(得分:0)
我做了一个粗略的例子,希望能让你知道如何解决你的问题。如果您发布了一些代码,那将会有所帮助,因为我可以为它量身定制这个答案。无论如何,只需将代码复制并粘贴到您的文档根目录中,看它是否有效。
package
{
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.media.Sound;
import flash.net.URLRequest;
public class Main extends Sprite
{
private var _leafVector:Vector.<Leaf>;
private var _nature:Nature;
private var _playButton:PlayButton;
private var _leafPlayList:LeafPlayList;
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}// end function
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
var soundsXml:XML =
<sounds>
<sound name="sound1" url="sounds/sound1.mp3" />
<sound name="sound2" url="sounds/sound2.mp3" />
<sound name="sound3" url="sounds/sound3.mp3" />
<sound name="sound4" url="sounds/sound4.mp3" />
</sounds>
_leafVector = new Vector.<Leaf>();
for (var i:uint = 0; i < soundsXml.children().length(); i++)
{
var sound:Sound = new Sound(new URLRequest(soundsXml.sound[i].@url));
var leaf:Leaf = new Leaf(String(i+1), sound, soundsXml.sound[i].@name);
leaf.x = i * leaf.width;
addChild(leaf);
leaf.addEventListener(MouseEvent.MOUSE_DOWN, onLeafMouseDown);
leaf.addEventListener(MouseEvent.MOUSE_UP, onLeafMouseUp);
_leafVector.push(leaf);
}// end for
_nature = new Nature();
_nature.x = _leafVector[3].x + _leafVector[3].width;
addChildAt(_nature, 0);
_playButton = new PlayButton();
_playButton.x = _nature.x;
_playButton.y = _nature.height;
addChild(_playButton);
_playButton.addEventListener(MouseEvent.CLICK, onPlayButtonClick);
_leafPlayList = new LeafPlayList();
_leafPlayList.x = _playButton.x;
_leafPlayList.y = _playButton.y + _playButton.height;
addChild(_leafPlayList);
}// end function
private function onLeafMouseUp(e:MouseEvent):void
{
var leaf:Leaf = Leaf(e.currentTarget);
leaf.stopDrag();
if (leaf.dropTarget == _nature)
{
_leafPlayList.addLeafSound(leaf.soundName, leaf.sound);
leaf.removeEventListener(MouseEvent.MOUSE_DOWN, onLeafMouseDown);
leaf.removeEventListener(MouseEvent.MOUSE_UP, onLeafMouseUp);
}// end function
}// end function
private function onLeafMouseDown(e:MouseEvent):void
{
var leaf:Leaf = Leaf(e.currentTarget);
this.setChildIndex(leaf, this.numChildren-1);
leaf.startDrag();
}// end function
private function onPlayButtonClick(e:MouseEvent):void
{
_leafPlayList.playLeafSounds();
}// end function
}// end class
}// end package
import flash.display.Sprite;
import flash.media.Sound;
import flash.text.TextField;
internal class Leaf extends Sprite
{
private var _sound:Sound;
private var _soundName:String;
public function get soundName():String
{
return _soundName;
}// end function
public function get sound():Sound
{
return _sound;
}// end function
public function Leaf(text:String, sound:Sound, soundName:String)
{
_sound = sound;
_soundName = soundName;
graphics.lineStyle(1);
graphics.beginFill(0x00FF00);
graphics.drawCircle(25, 25, 25);
graphics.endFill();
var textField:TextField = new TextField();
textField.text = text;
textField.x = width / 2;
textField.y = height / 2;
textField.selectable = false;
addChild(textField);
}// end function
}// end class
internal class Nature extends Sprite
{
public function Nature()
{
graphics.beginFill(0xFFFF00);
graphics.drawRect(0, 0, 200, 200);
graphics.endFill();
}// end function
}// end class
internal class PlayButton extends Sprite
{
public function PlayButton()
{
graphics.lineStyle(1);
graphics.beginFill(0xFF0000);
graphics.drawRect(0, 0, 100,25);
graphics.endFill();
var textField:TextField = new TextField();
textField.text = "PLAY BUTTON";
textField.selectable = false;
addChild(textField);
}// end function
}// end class
import flash.events.Event;
import flash.media.SoundChannel;
internal class LeafPlayList extends Sprite
{
private var _soundChannel:SoundChannel;
private var _leafSoundVector:Vector.<Sound>;
private var _position:int;
private var _playListTextField:TextField;
public function LeafPlayList()
{
_playListTextField = new TextField();
addChild(_playListTextField);
_soundChannel = new SoundChannel();
_leafSoundVector = new Vector.<Sound>();
}// end function
public function addLeafSound(soundName:String, sound:Sound):void
{
_playListTextField.appendText("\n" + soundName);
_leafSoundVector.push(sound);
}// end function
public function playLeafSounds():void
{
_soundChannel.stop();
_position = 1;
_soundChannel = _leafSoundVector[_position - 1].play();
_soundChannel.addEventListener(Event.SOUND_COMPLETE, onSoundChannelSoundComplete);
}// end function
private function onSoundChannelSoundComplete(e:Event):void
{
if (!(_position == _leafSoundVector.length)) _position++
else _position = 1;
playNexLeafSound();
}// end function
private function playNexLeafSound():void
{
_soundChannel = _leafSoundVector[_position - 1].play();
_soundChannel.addEventListener(Event.SOUND_COMPLETE, onSoundChannelSoundComplete);
}// end function
}// end class