我是一名学生,我正在做我的最后一个项目,但我被困住了。我在我的动作脚本中找不到解决问题的方法。我搜索了网络,几乎阅读了所有可以帮助我的网站。但这是我的问题。
我必须在动作脚本中制作一个 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);
}
}}