Author Topic: FlxText not removing?  (Read 1381 times)

cramcram

  • New Member
  • *
  • Posts: 3
  • Karma: +0/-0
    • View Profile
FlxText not removing?
« on: Sat, Nov 19, 2011 »
Hi, I'm a total beginner at flixel and for fun was trying to modify the HelloWorld tutorial such that the text will only appear after a button is pressed, and if the button is pressed again the text will be removed. However, for whatever reason the text isn't being removed as it should. Anyone care to help? :)

Code: [Select]
package 
{
import org.flixel.*;

public class PlayState extends FlxState
{

private var currentText:FlxText;
private var button:FlxButton;

override public function create():void
{

button =  new FlxButton(160, 120, "Push Me!", displayText);
add(button);

}

public function displayText():void {
currentText = new FlxText(0, 0, 100, "Thanks for pushing!");
add(currentText);

remove(button);
button = new FlxButton(160, 120, "Remove Text?", removeText);
add(button);

}

public function removeText():void {

remove(button);
remove(currentText);

}



}

}

The odd part for me about the removeText function is that the button is removed but not the text. Thanks in advance!

wg/funstorm

  • Global Moderator
  • Key Contributor
  • *****
  • Posts: 596
  • Karma: +0/-0
    • View Profile
    • Funstorm
Re: FlxText not removing?
« Reply #1 on: Sat, Nov 19, 2011 »
Stick this in displayText() and you will see that the function also gets executed when the second button is clicked.

Code: [Select]
trace("displayText");

It looks like somehow even though you are removing the first button's graphic, it is still reacting to clicks.

You can fix this by also calling kill on the button like so:

Code: [Select]
public function displayText():void {

currentText = new FlxText(0, 0, 100, "Thanks for pushing!");
add(currentText);

remove(button);
button.kill();
button = new FlxButton(160, 120, "Remove Text?", removeText);
add(button);

}

But really, I think killing and creating a new button is a little unnecessary, you could try just updating the first button eg

Code: [Select]
public function displayText():void {

currentText = new FlxText(0, 0, 100, "Thanks for pushing!");
add(currentText);

button.onUp = removeText;
button.label.text = "Remove Text?");

}
« Last Edit: Sat, Nov 19, 2011 by wg/funstorm »

cai

  • Contributor
  • ****
  • Posts: 465
  • Karma: +0/-0
  • the illest of villains
    • View Profile
    • Brandon Cash
Re: FlxText not removing?
« Reply #2 on: Sun, Nov 20, 2011 »
Whoops... there should be a post here, but I maaaay have accidentally hit the delete instead of approve.  My bad.

Quote from: cramcram
Killing the button did the trick, thanks!
Follow me on Twitter | Come join us at #flixel on irc.freenode.net!

xyroclast

  • Contributor
  • ****
  • Posts: 389
  • Karma: +0/-0
    • View Profile
Re: FlxText not removing?
« Reply #3 on: Mon, Nov 21, 2011 »
I've had several problems with FlxButtons still doing their job even after they theoretically shouldn't exist anymore, and it makes me wonder whether there truly is a bug in FlxButton that's causing this to happen.

wg/funstorm

  • Global Moderator
  • Key Contributor
  • *****
  • Posts: 596
  • Karma: +0/-0
    • View Profile
    • Funstorm
Re: FlxText not removing?
« Reply #4 on: Mon, Nov 21, 2011 »
What did you do to end their existence Xyro?

The issue as cramcram described it makes sense, I think.

1) Every new button adds an on mouse up event to flash
2) This event can be removed or disabled by:
     - calling destroy()
     - setting button.visible=false
     - setting button.active=false
     - setting button.exists=false
3) Calling remove(button) does not remove the callback, because remove does not do any of those things.
4) Calling button.kill() does the trick because kill sets visible & active = false;

I think the confusion may stem from here: when you remove a button, you can no longer SEE the button. But this is not the same as setting button.visible=false, even though the result may look the same on the surface.

The button code is super-duper-ultra simple... if there's a bug in here with active/visible/exists/status then I would expect it to also screw up a lot of other things since so much relies on those variables.

Code: [Select]
protected function onMouseUp(event:MouseEvent):void
{
if(!exists || !visible || !active || (status != PRESSED))
return;
if(onUp != null)
onUp();
if(soundUp != null)
soundUp.play(true);
}

Anyways, that's my take on it but I could always be wrong so if it's still happening for you I'd love to see a code example.
« Last Edit: Tue, Nov 22, 2011 by wg/funstorm »