0

これは私のゲームのレベルセレクターです。レベル1〜8を選択できます。どちらがクリックされたかを知る必要があります。e.targetは、オブジェクトの名前以外にはあまり教えてくれません。配列内の場所または画像の名前のいずれかを知っている必要があるので、どのレベルをロードするかがわかります。

package 
{
    import flash.display.Bitmap;
    import flash.display.DisplayObject;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;

    public class LevelSelector extends Sprite 
    {
        private var posX:int = 50;
        private var posY:int = 50;

        [Embed(source="../lib/one.png")]
        private var lvl1Class:Class;
        private var lvl1:Bitmap = new lvl1Class();

        [Embed(source="../lib/two.png")]
        private var lvl2Class:Class;
        private var lvl2:Bitmap = new lvl2Class();

        [Embed(source="../lib/three.png")]
        private var lvl3Class:Class;
        private var lvl3:Bitmap = new lvl3Class();

        [Embed(source="../lib/four.png")]
        private var lvl4Class:Class;
        private var lvl4:Bitmap = new lvl4Class();

        [Embed(source="../lib/five.png")]
        private var lvl5Class:Class;
        private var lvl5:Bitmap = new lvl5Class();

        [Embed(source="../lib/six.png")]
        private var lvl6Class:Class;
        private var lvl6:Bitmap = new lvl6Class();

        [Embed(source="../lib/seven.png")]
        private var lvl7Class:Class;
        private var lvl7:Bitmap = new lvl7Class();

        [Embed(source="../lib/eight.png")]
        private var lvl8Class:Class;
        private var lvl8:Bitmap = new lvl8Class();

        private var myArrayBitmaps:Array = new Array;
        private var myArraySprites:Array = new Array;

        private var yCounter:int = 1;

        public function LevelSelector():void 
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);
        }
        private function init(e:Event = null):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);
            // entry point

            myArrayBitmaps.push(false, lvl1, lvl2, lvl3, lvl4, lvl5, lvl6, lvl7, lvl8);

            for (var i:int = 1; i < 9; i++) 
            {
                myArrayBitmaps[i].x = posX;
                myArrayBitmaps[i].y = posY;
                myArrayBitmaps[i].height = 50;
                myArrayBitmaps[i].width = 100;
                //myArrayBitmaps[i].name = "lvl_" + i + "";

                myArraySprites[i] = new Sprite();
                myArraySprites[i].addChild(myArrayBitmaps[i]);
                myArraySprites[i].x = posX;
                myArraySprites[i].y = posY;
                //myArraySprites[i].name = "lvl_" + i + "";

                stage.addChild(myArraySprites[i]);

                myArraySprites[i].addEventListener(MouseEvent.CLICK, function(e:MouseEvent):void {
                    removeThis(e, this);
                });

                posX += 100;
                yCounter += 1;
                if (yCounter == 5) {
                    posY += 100;
                    posX -= 400;
                }
            }
        }
        private function removeThis(e:MouseEvent, temp):void
        {
            //Need to know the number which was passed, 1-8
        }
    }
}
4

1 に答える 1

3

私はこのようなことをします:

//...
private function init(e:Event = null):void 
{
    //...
    for (var i:int = 1; i < 9; i++) 
    {
        //...
        myArraySprites[i] = new Sprite();
        myArraySprites[i].addChild(myArrayBitmaps[i]);
        //...
        stage.addChild(myArraySprites[i]);

        myArraySprites[i].addEventListener(MouseEvent.CLICK, removeThis);


        }
    }
}
private function removeThis(e:MouseEvent):void
{
    var clickTarget:int = myArraySprites.indexOf(e.currentTarget);
    trace("Clicked sprite (id): " + clickTarget);
}
于 2012-06-18T17:16:33.990 に答える