1

我有一个简单的集中游戏 5x4 瓷砖(我有一个符号(tile_movieclip),有 11 帧 - 10 帧,数字 1-10 和第 11 个背景(衬衫卡))。

我希望一开始是可见的数字,几秒钟后瓷砖必须变成背景(第 11 帧)。在我的代码中,我首先看到了所有数字,并且在 1 秒内只转动了一个(右下角)图块。请帮忙。

PS Find //Error can be HERE!!!!!! - 我认为问题隐藏在那里,但我不确定。

这里的来源:

   package { 

    // importing classes

import flash.events.Event;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.utils.Timer;
// end of importing classes
public class Main extends Sprite {
    private var pickedTiles:Array = new Array();  
    private const NUMBER_OF_TILES:uint=20;
    private var pause_game:Timer;
    private var canPick:Boolean=true;
    private var tiles:Array = new Array();
    private var tile:tile_movieclip;
    private var i:uint = 0;

    public function Main() {
        // variables and constants
        // no more NUMBER_OF_TILES here

        const TILES_PER_ROW:uint=5;
        // end of variables and constants
        // tiles creation loop
        for (i=0; i<NUMBER_OF_TILES; i++) {
            tiles.push(Math.floor(i/2));
        }
        trace("My tiles: "+tiles);
        // end of tiles creation loop
        // shuffling loop
        var swap,tmp:uint;
        for (i=NUMBER_OF_TILES-1; i>0; i--) {
            swap=Math.floor(Math.random()*i);
            tmp=tiles[i];
            tiles[i]=tiles[swap];
            tiles[swap]=tmp;
        }
        trace("My shuffled tiles: "+tiles);
        // end of shuffling loop
        // tile placing loop

        for (i=0; i<NUMBER_OF_TILES; i++) {

            tile=new tile_movieclip();
            addChild(tile);

            tile.cardType=tiles[i];
            tile.x=5+(tile.width+5)*(i%TILES_PER_ROW);
            tile.y=5+(tile.height+5)*(Math.floor(i/TILES_PER_ROW));

            tile.gotoAndStop(tiles[i]+1); // This shows all number at first

            canPick = false;

            tile.buttonMode=true;
            tile.addEventListener(MouseEvent.CLICK,onTileClicked);

        }
        pause_game=new Timer(1000,1);
            pause_game.start();
        pause_game.addEventListener(TimerEvent.TIMER_COMPLETE,hideTiles);

    }
        // end of tile_placing_loop
                                      //Error can be HERE!!!!!
    private function hideTiles(e:TimerEvent) {
            pause_game.removeEventListener(TimerEvent.TIMER_COMPLETE,hideTiles);

            tile.gotoAndStop(NUMBER_OF_TILES/2+1); //This turns to background just one tile (the lower right corner)

            canPick = true;
          }
    private function onTileClicked(e:MouseEvent) {
        if(canPick){

            var picked:tile_movieclip=e.currentTarget as tile_movieclip;
            trace("you picked a "+e.currentTarget.cardType);
            // checking if the current tile has already been picked
            if (pickedTiles.indexOf(picked)==-1) {
                pickedTiles.push(picked);
                picked.gotoAndStop(picked.cardType+1);
            }
            // end checking if the current tile has already been picked
            // checking if we picked 2 tiles
            if (pickedTiles.length==2) {
                canPick = false;
                pause_game=new Timer(1000,1);
                pause_game.start();
                if (pickedTiles[0].cardType==pickedTiles[1].cardType) {
                    // tiles match!!
                    trace("tiles match!!!!");
                    pause_game.addEventListener(TimerEvent.TIMER_COMPLETE,removeTiles);
                } else {
                    // tiles do not match
                    trace("tiles do not match");
                    pause_game.addEventListener(TimerEvent.TIMER_COMPLETE,resetTiles);
                }
                // no more pickedTiles = new Array();
            }
            // end checking if we picked 2 tiles
        }
    }
    private function removeTiles(e:TimerEvent) {
        pause_game.removeEventListener(TimerEvent.TIMER_COMPLETE,removeTiles);
        pickedTiles[0].removeEventListener(MouseEvent.CLICK,onTileClicked);
        pickedTiles[1].removeEventListener(MouseEvent.CLICK,onTileClicked);
        removeChild(pickedTiles[0]);
        removeChild(pickedTiles[1]);
        pickedTiles = new Array();
        canPick = true;
    }
    private function resetTiles(e:TimerEvent) {
        pause_game.removeEventListener(TimerEvent.TIMER_COMPLETE,resetTiles);
        pickedTiles[0].gotoAndStop(NUMBER_OF_TILES/2+1);
        pickedTiles[1].gotoAndStop(NUMBER_OF_TILES/2+1);
        pickedTiles = new Array();
        canPick = true;
    }
}

}

4

1 回答 1

1

您有一组图块,而在您的侦听器中,您引用的tile是单个图块。您需要交换添加到显示列表中的所有图块,为此您可以使用以下结构:

for (var i:int=numChildren-1;i>=0;i--) {
    var mc:DisplayObject=this.getChildAt(i);
    var picked:tile_movieclip=mc as tile_movieclip;
    if (picked) picked.gotoAndStop(NUMBER_OF_TILES/2+1); // now, show the back
}

这应该替换您在函数中的tile.gotoAndStop(NUMBER_OF_TILES/2+1);声明。hideTiles

于 2013-03-26T10:17:01.487 回答