0

我对 AS3 还很陌生,我正在创建一个基于文本的游戏,但我很难从屏幕上清除文本。我正在使用 FlxText 和 FlxButtons。当我使用 clear() 时,它会删除所有内容。这是它的样子(对不起,如果这超出了我应该说的,我很着急,代码工作也可能很糟糕。):

public function DragonState() 
		{
			
			FlxG.mouse.show();
			menuButton = new FlxButton(240, 220, "Menu", menu);
			add(menuButton);
			swordButton = new FlxButton(0, 220, "Slash", sword);
			add(swordButton)
			shieldButton = new FlxButton(80, 220, "Sheild Bash", shield);
			add(shieldButton)
			bowButton = new FlxButton(160, 220, "Shoot", bow);
			add(bowButton)
			
		}
		private function menu():void
		{
			FlxG.mouse.hide();
			FlxG.switchState(new MenuState);
		}
		public function sword():void 
		{
			
			playerAttack = Math.floor(Math.random() * (2 - 0 +2) * 7);
			dragonAttack = Math.floor(Math.random() * (1 - 0 + 1) * 7);
			add(new FlxText(0, 0, 320, "The Dragon hit you for " + String(dragonAttack)));
			add(new FlxText(0, 10, 320, "You hit the Dragon for " + String(playerAttack)));
			updateHealth()
		}
		public function shield():void
		{
			playerAttack = Math.floor(Math.random() * (2 - 0 +2) * 2);
			dragonAttack = Math.floor(Math.random() * (1 - 0 + 1) * 2);
			add(new FlxText(0, 0, 320, "The Dragon hit you for " + String(dragonAttack)));
			add(new FlxText(0, 10, 320, "You hit the Dragon for " + String(playerAttack)));
			updateHealth()
		}
		public function bow():void
		{
			playerAttack = Math.floor(Math.random() * (2 - 0 + 2) * 4);
			dragonAttack = Math.floor(Math.random() * (2 - 0 + 2) * 4);
			add(new FlxText(0, 0, 320, "The Dragon hit you for " + String(dragonAttack)));
			add(new FlxText(0, 10, 320, "You hit the Dragon for " + String(playerAttack)));
			updateHealth()
		}
		public function updateHealth():void
		{
			dragonHealth = dragonHealth - playerAttack
			playerHealth = playerHealth - dragonAttack
			add(new FlxText(0, 20, 320, "The Dragon has " + String(dragonHealth) + " health left."));
			add(new FlxText(0, 30, 320, "You have " + String(playerHealth) + " health left."));
			
			if (dragonHealth <= 0) {
				add(new FlxText(0,0,320,"Good Job"));
			}else if (playerHealth<=0) {
				add(new FlxText(0,0,320,"Oh No!"));
			} else {
				add(new FlxText(0,0,320,"What will you do?"));	
			}
		}

4

2 回答 2

1

使用 Flixel 时,最好在状态的create()方法中实例化您的元素,而不是使用类构造函数。它将通过节省内存和避免额外的垃圾收集器运行来提高性能。

对您在屏幕中呈现的任何元素都有一个引用/属性也是一个好主意,就像您使用按钮所做的那样。这样你就可以在状态的任何方法中访问它们,例如,这对于清除文本很有用。

按照这种做法,您应该为屏幕中所需的每个文本都有一个属性:

class DragonState {
     // Properties for buttons
      private var menuButton :FlxButton;
      private var swordButton :FlxButton;
      private var shieldButton :FlxButton;

      // Properties for texts
      private var enemyAttack :FlxText;
      private var enemyHealth :FlxText;
      private var playerAttack :FlxText;
      private var playerHealth :FlxText;


      override public function create():void {
          // instantiate buttons
          FlxG.mouse.show();
          menuButton = new FlxButton(240, 220, "Menu", menu);
          add(menuButton);
          swordButton = new FlxButton(0, 220, "Slash", sword);
          add(swordButton);
          shieldButton = new FlxButton(80, 220, "Sheild Bash", shield);
          add(shieldButton);
          bowButton = new FlxButton(160, 220, "Shoot", bow);
          add(bowButton);

          // instantiate texts (they are empty by default)
          enemyAttack = new FlxText(0, 0, 320);
          enemyHealth = new FlxText(0, 20, 320,);
          playerAttack = new FlxText(0, 10, 320);
          playerHealth = new FlxText(0, 30, 320);

          add(enemyAttack);
          add(enemyHealth);
          add(playerAttack);
          add(playerHealth);
      }


      public function sword():void {
          playerAttack = Math.floor(Math.random() * (2 - 0 +2) * 7);
          dragonAttack = Math.floor(Math.random() * (1 - 0 + 1) * 7);

          // Change the content of texts
          enemyAttack.text = "The Dragon hit you for " + String(dragonAttack);
          playerAttack.text = "You hit the Dragon for " + String(playerAttack);
          updateHealth()
      }

      public function updateHealth():void {
          dragonHealth = dragonHealth - playerAttack
          playerHealth = playerHealth - dragonAttack
          enemyHealth.text = "The Dragon has " + String(dragonHealth) + " health left.";
          playerHealth.text = "You have " + String(playerHealth) + " health left.";

          // (...)
     }

      public function clearTexts():void {
          enemyAttack.text = "";
          playerAttack.text = "";
          enemyHealth.text = "";
          playerHealth.text = "";
     }
}

如果要隐藏所有文本,可以将它们添加到 aFlxGroup并隐藏该组。隐藏组将隐藏组中包含的所有内容。它可以实现为:

class DragonState {
      // (...)

      // Properties for texts
      private var enemyAttack :FlxText;
      private var enemyHealth :FlxText;
      private var playerAttack :FlxText;
      private var playerHealth :FlxText;

      // Group containing all texts
      private var texts :FlxGroup;

      override public function create():void {
          // (...)

          // create the texts group
          texts = new FlxGroup();

          // instantiate texts (they are empty by default)
          enemyAttack = new FlxText(0, 0, 320);
          enemyHealth = new FlxText(0, 20, 320,);
          playerAttack = new FlxText(0, 10, 320);
          playerHealth = new FlxText(0, 30, 320);

          // add all texts to the group
          texts.add(enemyAttack);
          texts.add(enemyHealth);
          texts.add(playerAttack);
          texts.add(playerHealth);

          // Add the group to the state
          add(texts);
      }


      public function sword():void {
          // (...)

          // Change the content of texts
          enemyAttack.text = "The Dragon hit you for " + String(dragonAttack);
          playerAttack.text = "You hit the Dragon for " + String(playerAttack);
          updateHealth();
      }

      public function updateHealth():void {
          // (...)
          enemyHealth.text = "The Dragon has " + String(dragonHealth) + " health left.";
          playerHealth.text = "You have " + String(playerHealth) + " health left.";
          // (...)
     }

     public function hideTexts():void {
          texts.visible = false;
     }

     public function showTexts():void {
          texts.visible = true;
     }
}
于 2014-10-01T12:50:27.210 回答
0

首先,您要跟踪要添加到 State 对象的 FlxText 对象。您可以将每个变量保存为单独的变量,也可以在类级别的数组中分组。

var flxTextObjs:Array;

当您添加 FlxText 对象时,您应该持有对它们的引用。

前任:

var fTxt = add(new FlxText(0, 0, 320, "The Dragon hit you for " + String(dragonAttack)));
flxTextObjs.push(fTxt);

现在您可以引用这些对象,您可以调用 remove() 而不是 clear()。

for(var x:int; x < flxTextObjs.length; x ++){
    remove(flxTextObjs[x]);
}

这是一个草率的例子,但我希望你应该从中得到想法。

于 2014-09-30T15:32:38.847 回答