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-正しく行う方法は本当にたくさんあります:)

わかりました、最も単純なものの1つ:

// 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 に答える