Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 (e.g. 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!

like image 366
Tristan Schenkhuysen Avatar asked Apr 07 '26 05:04

Tristan Schenkhuysen


1 Answers

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.

like image 104
Michael Avatar answered Apr 08 '26 23:04

Michael