AS3清除FlxText

时间:2014-09-29 03:08:47

标签: actionscript-3 flash flixel

我对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?"));	
			}
		}
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分: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 = "";
     }
}

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

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;
     }
}

答案 1 :(得分: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]);
}

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