使用数组、循环和推送函数

发布于 2024-10-30 05:48:19 字数 152 浏览 1 评论 0原文

我想做的是让 4 个影片剪辑(leaf1、leaf2、leaf3、leaf4)在将其拖到另一个影片剪辑(NatureTarget)上并按下播放按钮时播放附加到它们的声音,以便声音按照它们被拖动的顺序播放。我知道我需要使用数组、推送函数和循环......但我迷路了。任何帮助将非常感激。谢谢。

what I'm trying to do is to get 4 movieclipps (leaf1, leaf2, leaf3, leaf4) to play the sound that is attached to them when is dragged onto another movieclip (NatureTarget) and the play button is pressed so that the sounds are played in the order they are dragged on. I know I need to use an array and the push function and a loop...but I am lost. Any help would be very much appreciated. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如此安好 2024-11-06 05:48:19

我做了一个粗略的例子,希望能让您了解如何解决您的问题。如果您发布一些代码将会有所帮助,因为我可以针对它定制这个答案。无论如何,只需将代码复制并粘贴到文档根目录即可查看它的工作原理。

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

I made a crude example, hopefully giving you an idea of how to go about solving your problem. It would have helped if you posted some of your code as I could have tailored this answer to it. Anyway just copy and paste the code into you document root to see it work.

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文