Actionscript 3.0 - 检查是否留下了瓷砖

时间:2013-06-02 18:28:56

标签: actionscript-3 flash actionscript flash-cs5 flashdevelop

我是学生,我正在研究我的最终项目,但我被困住了。我的动作脚本中有一点我无法找到解决问题的方法。我搜索过网络,几乎阅读了所有可以帮助我的网站。但这是我的问题。

我必须在动作脚本中制作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);
    }
}}

2 个答案:

答案 0 :(得分:0)

我的想法是,因为你将tile放在tiles数组中,你可以拥有一些东西,当用户点击tile时你用splice属性从Array中删除了这些东西,所以当array.length属性是零,你可以结束游戏。

p.s不确定splice属性是否是您需要的属性但是这里是actionscript网站,它告诉您有关数组及其属性的所有信息ActionScript 3 Site

答案 1 :(得分:0)

我认为这更像是一个快速'黑客',但既然你知道了瓷砖的数量,你就可以创建一个计数器。然后,每次从板上移除一对瓷砖时,将2添加到计数器(每个瓷砖一个)。当计数器与您在开始时拥有的瓷砖数量相匹配时,这意味着所有瓷砖都已匹配,因此从电路板上移除。