Game Development Community

Remove a dynamically created sprite by clicking on it

by Zilla · in Torque Game Builder · 08/31/2009 (12:39 pm) · 5 replies

I created a behaviour that loads and displays a static sprite.
When I click the "Help" button again, it will be deleted.
So far so good.

But I want that I can click on the loaded sprite and then it should disapear.

if (!isObject(helpScreenButtonBehavior))
{
   %template = new BehaviorTemplate(helpScreenButtonBehavior);
   
   %template.friendlyName = "Help Screen Button";
   %template.behaviorType = "GUI";
   %template.description  = "Loads a help screen when clicked";

   %template.addBehaviorField(helpScreen, "The help screen (Static Sprite) to load on mouse up", string, "");
}

function helpScreenButtonBehavior::onBehaviorAdd(%this)
{
   %this.owner.setUseMouseEvents(true);
}

function helpScreenButtonBehavior::onMouseUp(%this, %modifier, %worldPos)
{
  	if ($isHelpScreen)
    {
        %helpScreen.safeDelete();
        $isHelpScreen = false;
    }
    else
    {
        %helpScreen = new t2dStaticSprite() { scenegraph = ChordSettings; };
        %helpScreen.setPosition("0 0");
        %helpScreen.setSize("40 40");
	    %spriteName = %this.helpScreen;
        %helpScreen.setImageMap(%spriteName);
        %helpScreen.setLayer(1);
        $isHelpScreen = true;
    }
}

#1
09/01/2009 (3:08 am)
I have another strange issue:

safeDelete() works fine on the PC but has no effect on the Mac...
#2
09/01/2009 (4:51 pm)
I think your problem might be that %helpScreen is undefined in helpScreenButtonBehavior. You create a local variable called helpScreen when you make the image but when you exit the function and come back, it won't remember what %helpScreen refers to. An easy but ugly fix would be to make helpScreen global since you (probably) only have 1 up at a time but there's probably another better way that I'm missing.
#3
09/02/2009 (4:03 am)
@Nate
Thank you for your advice :)
The behaviour above works perfectly (even with the local variable) and it does the following when I "attach" it to a "button":

When I click the "button", it will display my helpScreen image.
When I click the button again, the helpScreen image will disappear.

So far, so good. But what I want is that I can click on the loaded help screen to delete it and not on the button again.
#4
09/03/2009 (2:17 pm)
It does not seems to be proper to use undeclared local variable (and it is undeclared when you get into the code with $isHelpScreen set to true.

Better solution is to set %this.helpScreenObject = new t2dStaticSprite etc. And call it where ever you like inside behavior.

If you like it to disappear just add behavior to the static sprite itself. It may be even the same behavior - but then you would need to keep static's sprite in it's own behavior.

function helpScreenButtonBehavior::onMouseUp(%this, %modifier, %worldPos)
{
  	if ($isHelpScreen)
    {
        %this.helpScreenObject.safeDelete();
        $isHelpScreen = false;
        return true;
}

//code below would execute only if statement above is false - no else needed - used to be less calculations, not sure if important for TS

        %this.helpScreenObject = new t2dStaticSprite(%this.helpScreen) { 
        scenegraph = ChordSettings;
        position = "0 0";
        imageMap = %this.helpScreen;
        size = "40 40";
        layer = "1";
};
        $isHelpScreen = true;

      //creating and adding NEW instance of behavior
        %beh = helpScreenButtonBehavior::createInstance(); //new instance
        %beh.helpScreenObject = %this.helpScreenObject; //copying sprites ID to new bahvior's dynamic field
        %this.helpScreenObject.addBehavior(%beh); //adding behavior to object

}


Something like this should work as you expect.
Sometimes removing object from it's behavior requires to schedule this (%this.schedule(0, safeDelete); - you would notice easily if game would start crashing ;-) )
#5
09/04/2009 (12:47 pm)
@World-Loom
Thank you very much :)))