簡體   English   中英

Actionscript 3.0-檢查是否剩余圖塊

[英]Actionscript 3.0 - Check if tiles are left

我是一名學生,正在完成我的最后一個項目,但遇到了麻煩。 我的動作腳本中沒有找到解決問題的方法。 我搜索了網絡,幾乎閱讀了所有可以幫助我的網站。 但是,這是我的問題。

我必須用動作腳本制作一個Flash游戲。 我快完成了,但是我找不到如何添加代碼來驗證沒有剩下的瓷磚可供選擇。

在我必須將代碼放置在什么地方以檢查舞台上是否沒有瓷磚的情況下,可以提供一些幫助嗎? 還有我必須使用什么代碼?

而且,當它檢查是否沒有剩余的瓷磚並且沒有剩余的瓷磚時,它必須轉到第二個幀,要求再次播放或回到主屏幕。

我的代碼:

package {
// importing classes
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.utils.Timer;
import flash.text.*;
// end of importing classes
public class Main extends Sprite {
    private var pickedTiles:Array = new Array();  
    private const NUMBER_OF_TILES:uint=24;
    private var pause_game:Timer;
    private var canPick:Boolean=true;
    // scorebord
    private static const pointsForMatch:int = 100; 
    private static const pointsForMiss:int = -5; 
    private var gameScoreField:TextField; 
    private var gameScore:int; 
    public function Main() {
        gameScoreField = new TextField();
        addChild(gameScoreField); 
        gameScoreField.x = 810; 
        gameScoreField.y = 50;
        gameScoreField.width = 200;
        var format:TextFormat = new TextFormat();
        format.size = 50;
        gameScoreField.defaultTextFormat = format;
        gameScoreField.textColor = 0xFF0000;

        // variables and constants
        // no more NUMBER_OF_TILES here
        const TILES_PER_ROW:uint=6;
        var tiles:Array=new Array();
        var tile:tile_movieclip;
        // end of variables and constants
        // tiles creation loop
        for (var i:uint=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 (var i:uint=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(NUMBER_OF_TILES/2+1);
            tile.buttonMode=true;
            tile.addEventListener(MouseEvent.CLICK,onTileClicked);
        }
        // end of tile_placing_loop
    }
        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();
            gameScore += pointsForMatch; // +100 points
            showGameScore(); // shows new score
            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();
            gameScore += pointsForMiss; // -5 points
            showGameScore(); //shows new score
            canPick = true;
        }
    public function showGameScore() 
    {
        gameScoreField.text = String(gameScore);
    }
}}

我的想法是,由於將切片保留在tile數組中,因此當用戶單擊切片時,可以使用splice屬性從Array中刪除項目,因此當array.length屬性為零時,您可以結束游戲。

ps不確定splice屬性是否是您需要的屬性,但是這里是actionscript網站,該網站向您介紹有關數組及其屬性的所有信息ActionScript 3網站

我認為這更多是一種快速的“破解”,但是由於您知道磁貼的數量,因此可以創建一個計數器。 然后,每次從板上卸下一對瓷磚時,將2加到計數器上(每個瓷磚一個)。 當計數器與開始時的圖塊數量匹配時,這意味着所有圖塊均已匹配,因此已從板上移除。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM