0

我正在创建一个测验,其中用户将对象(电影剪辑)与播放的声音剪辑匹配。声音存储在一个数组中,并随机选择一个。然后动态创建 4 个随机影片剪辑,其中包含对象图像。我需要一种将声音剪辑链接到电影剪辑的方法,以检查是否单击了正确的剪辑。这是到目前为止的代码:

var randSound = Math.round(Math.random()*1);         // Rand no 0-4
var sounds:Array = [cat, doorCreek];                 // Sound array
var soundClip:Sound = new sounds[randSound];         // Create random sound

sound_btn.addEventListener(MouseEvent.CLICK, playSound);    // Re-play sound button
function playSound(e:MouseEvent) { soundClip.play();  }

var clips:Array =[cat, door, wind, water];      // Movie clip array (will be longer)

// Add objects to stage
for(var i=0; i<4; i++)
{   
    var rand = Math.round(Math.random()*4);     // 4 clips as answer options

    var myRandClip:MovieClip=new clips[rand];      // Create random movieclip 

    // Create listener for the movieclip
    myRandClip.addEventListener(MouseEvent.CLICK, getIndex);

    function getIndex(e:MouseEvent)
    {
        trace(rand);
    }

    this.addChild(myRandClip);
}

当然,目前这个获取影片剪辑索引的函数只是获取最后生成的兰特数。我需要一种将某种 id 嵌入到生成的影片剪辑中的方法。然后我可以简单地测试它是否与例如声音剪辑索引相同。然后我将对每个问题做同样的事情(总共 10 个)

希望这很清楚,有人可以提供帮助。非常感谢

4

1 回答 1

0

我不明白你的问题。你是你的代码的老板。只有我们 OOP - 确实有很多正确的方法:)

好的,最简单的之一:

// in constructor or AddToStage event handler, or just in timeline

soundIndex = Math.round(Math.random()*soundArray.length);
playSoundFormArray(soundIndex);

var btn:MyButton;
for(var i:uint=0; i< buttonsArray.length; i++){
   btn = MyButton(index, checkFunction);
   // changee position, etc.
}

//.....

private function checkFunction(index:uint):void
{
    if(soundIndex == btnIndex){
      // your code
    }
}

///MyButton.as
// Simple Sprite extended class with click event handler
package{
    import flash.events.Event;
    import flash.display.Sprite;
    import flash.events.MouseEvent;
    import flash.text.TextField;

    public class MyButton extends Sprite 
    {
        private var _callBack:Function;
        private var _index:uint;

        public function Main(index:uint, callBack:Function, label:String="btn") 
        {
            _index = index;
                    _callback = callBack;
                    addEventListener(Event.ADDED_TO_STAGE, init);

                    // customizing button
                    var textField:TextField = new TextField();
                    textField.text = label;
                    // or your code

        }

        private function init(e:Event):void 
        {
            addEventListener(Event.Click, clickHandler);
        }

            private function clickHandler(e:MouseEvent):void 
        {
            _callBack(_index);
        }

    }
}

就这样。对不起,最终的错误。写在浏览器中。未测试。

于 2009-10-12T13:55:39.200 回答