简体   繁体   中英

AS3: if-function doesn't listen to a boolean in another class

So... I'm working on a chess-game, and trying to make it so that a "public static boolean" (turn) dictates which player can make a move. This boolean is in a class (Board.as) which imports all the classes for all the chess-pieces (eg QueenW.as (for the White Queen)).

I've tried multiple ways: Trying to make functions not run anymore, and replacing the pieces (which are buttons) to other objects (non-clickable movieclips). Decided to go with the latter. I've traced the boolean in a chess-piece class, as well as the Board-class, in an ENTER_FRAME function. Both seem to trace it correctly when the value changes.

Problem is: Flash doesn't remove the chess-pieces and replaces them with a non-clickable object, even though the class in which it should happen (Board.as) does listen to the boolean when tracing. Anybody knows a solution?

A little piece of my code, which is relative to the problem:

Board class (which is the Documentclass for my .fla file)

package
{
 import QueenWclass; //imports the class used for example.

 public class Board extends MovieClip
 {
  public static var turn:Boolean = new Boolean; //creates public static bool.
  var queenW:QueenWclass = new QueenWclass(); //creates aforementioned chess-piece.

  var queenWnoturn:QueenWnoturn = new QueenWnoturn; //creates a non-clickable object.
 }

 public function Board()
 {
  turn = true;

  this.addEventListener(Event.ENTER_FRAME, frameEnter);

  addChild(queenW); //adds pieces to the screen.
 }

 if (turn == true)
 {

 }

 if (turn == false)
 {
  removeChild(queenW); //Removes chess-piece.
  addChild(queenWnoturn); //Adds a non-clickable object.
 }
}

And my QueenWclass.as class:

package
{
 public class QueenWclass extends MovieClip
 {
  var queenW:QueenW = new QueenW();
 }

 public function QueenWclass()
 {
  addChild(queenW);

  this.addEventListener(MouseEvent.CLICK, CLICKqueenW);
 }

 function CLICKqueenW(event.MouseEvent):void
 {
  Board.turn = false;
 }
}

I hope I wrote this example correctly and understandably. There's no real timelimit to my project as I already had to turn it in an hour ago (but still got a 6/10 because of effort and how far I've come with this rather complex game). I just want to finish it for myself... Thanks in advance!

Maybe the code has not been copied correctly or there is a small problem.

This code:

 if (turn == true)
 {

 }

 if (turn == false)
 {
    removeChild(queenW); //Removes chess-piece.
    addChild(queenWnoturn); //Adds a non-clickable object.
 }

Will only run once, when "Board" is created, it will not run when the state of "turn" changes.

Well, you have nothing that's listening for the boolean's change. The code that's checking the boolean is located in constructor, while the actual change is done in a MouseEvent.CLICK event listener. You have to either implement a function that's called repeatedly via Event.ENTER_FRAME listening, SetInterval(), or TimerEvent.TIMER (with a timer), or implement a publicly available property as a function, that would check which turn is it and do corresponding actions. The latter is a little better, as it works only when something is changed.

private static var _turn:Boolean=false;
public static function get turn():Boolean { return _turn; } // getter part
public static function set turn(value:Boolean):void // setter part
{
    if (_turn==value) return; // no need to change turn
    _turn=value;
    if (_turn) YouGetATurn(); else EnemyGetsATurn();
    // this part is what will get called when you change Board.turn 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM