这是我第一次使用AS3,所以如果这个问题的原因很明显,我会道歉。
我正在下棋,我在棋盘上为每件棋子上课。当玩家1回合时,所有黑棋都要等待点击,但是当我实现它时,它会返回错误#1502。
package chess.sprites{
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class BlackBishop extends MovieClip{
public function BlackBishop(){
trace("bb created");
}
public function waitForClick(){
this.addEventListener(MouseEvent.MOUSE_DOWN, myTurn);
}
protected function myTurn(me:MouseEvent){
trace("Clicked");
}
}
}
BlackBishop中的函数waitForClick()正由另一个类调用。
public function setBlackTurn(){
for(var i:int = 0;i < 8;i++){
blackPawn[i].waitForClick();
}
for(var i:int = 0;i < 2;i++){
blackRook[i].waitForClick();
blackKnight[i].waitForClick();
blackBishop[i].waitForClick();
}
blackQueen.waitForClick();
blackKing.waitForClick();
}
我在错误#1502上一直在阅读,但似乎只有在我在BlackBishop中添加监听器时才会发生。有什么想法吗?
游戏课程:
package chess{
import flash.display.MovieClip;
import chess.sprites.*;
public class Game extends MovieClip{
//Contains all sprite classes
public var blackPawn:Array = new Array(8);
public var blackRook:Array = new Array(2);
public var blackKnight:Array = new Array(2);
public var blackBishop:Array = new Array(2);
public var blackQueen:BlackQueen;
public var blackKing:BlackKing;
public var whitePawn:Array = new Array(8);
public var whiteRook:Array = new Array(2);
public var whiteKnight:Array = new Array(2);
public var whiteBishop:Array = new Array(2);
public var whiteQueen:WhiteQueen;
public var whiteKing:WhiteKing;
public var boardArray:Array = new Array(64);
//Adds sprites to stage
public function addSprites(){
var spacing:int = 46;
for(var i:int = 0;i < 8;i++){
blackPawn[i] = new BlackPawn();
blackPawn[i].x = 112 + (spacing * i); //Sets x-pos of sprite
blackPawn[i].y = 316; //Sets y-pos of sprite
addChild(blackPawn[i]); //Adds sprite to stage
whitePawn[i] = new WhitePawn();
whitePawn[i].x = 112 + (spacing * i);
whitePawn[i].y = 362 - (spacing * 6);
addChild(whitePawn[i]);
}
for(i = 0;i < 2;i++){
blackRook[i] = new BlackRook();
blackRook[i].x = 112 + (spacing * 7 * i);
blackRook[i].y = 362;
addChild(blackRook[i]);
blackKnight[i] = new BlackKnight();
blackKnight[i].x = 158 + (spacing * 5 * i);
blackKnight[i].y = 362;
addChild(blackKnight[i]);
blackBishop[i] = new BlackBishop();
blackBishop[i].x = 204 + (spacing * 3 * i);
blackBishop[i].y = 362;
addChild(blackBishop[i]);
whiteRook[i] = new WhiteRook();
whiteRook[i].x = 112 + (spacing * 7 * i);
whiteRook[i].y = 362 - (spacing * 7);
addChild(whiteRook[i]);
whiteKnight[i] = new WhiteKnight();
whiteKnight[i].x = 158 + (spacing * 5 * i);
whiteKnight[i].y = 362 - (spacing * 7);
addChild(whiteKnight[i]);
whiteBishop[i] = new WhiteBishop();
whiteBishop[i].x = 204 + (spacing * 3 * i);
whiteBishop[i].y = 362 - (spacing * 7);
addChild(whiteBishop[i]);
}
blackQueen = new BlackQueen();
blackQueen.x = 112 + (spacing * 3);
blackQueen.y = 362;
addChild(blackQueen);
blackKing = new BlackKing();
blackKing.x = 112 + (spacing * 4);
blackKing.y = 362;
addChild(blackKing);
whiteQueen = new WhiteQueen();
whiteQueen.x = 112 + (spacing * 3);
whiteQueen.y = 362 - (spacing * 7);
addChild(whiteQueen);
whiteKing = new WhiteKing();
whiteKing.x = 112 + (spacing * 4);
whiteKing.y = 362 - (spacing * 7);
addChild(whiteKing);
}
public function resetArray(){
//Reset all values in array
for (var i:int = 0; i < 64; i++) {
boardArray[i] = false;
}
//Set appropriate values in array for starting positions
for (i = 0; i < 8; i++) {
boardArray[i] = true;
boardArray[i + 8] = true;
boardArray[i + (8 * 6)] = true;
boardArray[i + (8 * 7)] = true;
}
}
//Tells all black pieces to wait to be clicked
public function setBlackTurn(){
for(var i:int = 0;i < 8;i++){
blackPawn[i].waitForClick();
}
for(i = 0;i < 2;i++){
blackRook[i].waitForClick();
blackKnight[i].waitForClick();
blackBishop[i].waitForClick();
}
blackQueen.waitForClick();
blackKing.waitForClick();
}
public function setWhiteTurn(){
for(var i:int = 0;i < 8;i++){
whitePawn[i].waitForClick();
}
for(i = 0;i < 2;i++){
whiteRook[i].waitForClick();
whiteKnight[i].waitForClick();
whiteBishop[i].waitForClick();
}
whiteQueen.waitForClick();
whiteKing.waitForClick();
}
}
}
框架内的代码:
import chess.*;
var turn:int = 1;//1 is black, 2 is white
var gameRunning:Boolean = false;
var game:Game = new Game();
addChild(game);
game.addSprites();
game.resetArray();
gameRunning = true;
while(gameRunning){
if(turn == 1){
game.setBlackTurn();
}else if(turn == 2){
game.setWhiteTurn();
}else{
}
}
答案 0 :(得分:0)
你的问题在于:
gameRunning = true;
while(gameRunning){
....
}
while循环将锁定线程,直到gameRunning
等于false。这可能不会在15秒内发生。 (我认为它不会基于您共享的代码而发生)
您可能习惯使用其他使用游戏循环的语言。在AS3中,游戏循环需要在输入框处理程序或计时器刻度中完成。
代码的外观发生了什么,是你一遍又一遍地翻转,你正在循环中这样做,并尽可能快地运行并锁定UI。
取出while
循环。无论如何,我不知道你是怎么需要它的。你想在玩家移动棋子后改变转弯。
以下是有关如何构建此应用程序的提示:
创建每个部分扩展的基类。这样您就不必编写冗余代码。因此,创建一个名为ChessPiece
的类,并使其扩展MovieClip
(如果您的作品不使用时间轴,则为Sprite
)。然后你的所有作品都扩展到ChessPiece
级。现在他们将拥有你在基类中定义的任何函数和变量(ChessPiece
)。
当你改变转弯时,只需将鼠标事件锁定在属于另一种颜色的棋子上。如果您制作了两个容器,这可能非常简单:
var blackContainer:Sprite = new Sprite();
addChild(blackContainer);
var whiteContainer:Sprite = new Sprite();
addChild(whiteContainer);
//then in your for loops where you create your pieces, add them to the appropriate container
blackContainer.addChild(blackPawn[i]);
//then you can do this when the turn changes to white:
blackContainer.mouseChildren = false; //now none of the pieces can dispatch mouse events / be clicked