集中游戏 - 瓷砖旋转

时间:2013-03-26 08:18:42

标签: actionscript-3

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

我希望首先将是可见数字,并且在几秒之后必须转到背景(第11帧)。在我的代码中,我首先看到所有数字,并且1秒钟只转动一个(右下角)平铺。请帮忙。

P.S。找到//错误可以在这里!!!!! - 我认为问题隐藏在那里,但我不确定。

来源:

   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;
    }
}

}

1 个答案:

答案 0 :(得分: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语句。