简体   繁体   English

AS3:if-function不会在另一个类中侦听布尔值

[英]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)). 这个布尔值在一个类(Board.as)中,它导入所有国际象棋的所有类(例如QueenW.as(白皇后))。

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. 我在一个棋子类中跟踪了布尔值,并在一个ENTER_FRAME函数中跟踪了Board类。 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. 问题是:Flash不会删除棋子并用不可点击的对象替换它们,即使它应该发生的类(Board.as)在跟踪时会听取布尔值。 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) Board类(我的.fla文件的Documentclass)

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: 还有我的QueenWclass.as类:

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). 我的项目没有真正的时间限制,因为我已经在一小时前完成了它(但由于努力,我还有6/10这个相当复杂的游戏)。 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. 只运行一次,当“Board”创建时,它将不会在“turn”状态发生变化时运行。

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. 检查布尔值的代码位于构造函数中,而实际更改是在MouseEvent.CLICK事件侦听器中完成的。 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. 你必须实现一个通过Event.ENTER_FRAME监听,SetInterval()或TimerEvent.TIMER(带有计时器)重复调用的函数,或者实现一个公共可用的属性作为函数,它将检查它是哪个转向并做相应的动作。 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 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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