How do I create a button that will create/destroy a sprite

0 favourites
  • 11 posts
From the Asset Store
Fully commented source code/event sheet & sprites to create a space shooter game
  • I'm new to C2, so I apologize if this question has a very simple, obvious answer I should have found in my searching and tutorial-reading!

    I'm trying to make a create/destroy button which will create an object if there is no objectsprite on the screen and destroy the object if the objectsprite is onscreen.

    Basically a show/hide button; click once and the object appears, click again and it disappears, click a third time and it appears again, etc.

    Here's what I've tried:

    To create the object:


    On mouse click on buttonsprite


    buttonsprite variable1=0 (which is default value)

    System create objectsprite

    buttonsprite add 1 to variable1


    To destroy the object:


    On mouse click on buttonsprite


    buttonsprite variable1=1 (which shows it's been clicked once, so the objectsprite has been created)

    objectsprite destroy

    buttonsprite set variable1 to 0 (ready to create object again on next mouse click)


    In my head this logic should work, but onscreen...nothing happens. Is the button click instantly setting the variable to 1, thus also triggering the destroy event with that initial button click?

    I have searched for a solution to this but either I haven't found it or am too dense to realize the answer is staring in my face. Any suggestions or links to relevant tutorials would be greatly appreciated! Thanks!

  • There are two options to switch the state. First using modulo and second is 'toggle boolean'.

    Assume that:

    state=1 , create an object

    state=0 , destroy it.

  • Thank you for the suggestions! I'm not sure how to use modulo yet; I'll have to read up on that more. With your advice and this tutorial () I managed to get things working the way I wanted. Thanks again!

  • Just a question, Are you doing this switch for only one object in the layout?

    If yes then you don't need a variable for that at all.

    If no, then instead of modulo you can do simply "1-variable" (1-0= 1, 1-1=0 - simple switch), but if you are using an instance variable then Boolean is the nicest option.

    alextro, there are lot more then two options to switch a state

  • shinkan

    Pardon my lack of experience, just trying to give a quick suggestion. Glad to know there is more handy "switch" to use then I can imagine.

  • I'm doing it for several objects, but each object has its own "toggle" button. Right now what I have is:


    onMouseClick on "buttonsprite"

    system add 1 to variable [local static number]


    system variable > 1

    system set variable to 0


    system variable = 0

    objectsprite destroy


    system variable = 1

    system create objectsprite


    It seems to be working, though I feel like there out to be a more streamlined solution...?

  • ...or....

  • also you can use visibility (visible , invisible)

  • korbaach Ahhh! I'd tried something similar, but was inverting "sprite is onscreen" instead of using "else", which didn't work--now it works! Thank you!

    Related question: if I have two toggle buttons triggering create/destroy for two different sprites, can I keep sprite1 from being created if sprite2 already exists, and vice versa?

    For example:

    sprite1 is created

    sprite2 cannot be created until sprite1 is destroyed

    if sprite2 is then created, sprite1 cannot be re-created until sprite2 is destroyed, etc, etc.

    I haven't quite hit on the right combination to get this working...I can get it to work for one create/destroy round, or for one sprite, but not both sprites for numerous repetitions. Will keep reading and experimenting.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • There Can Only Be One..... <img src="{SMILIES_PATH}/icon_e_biggrin.gif" alt=":D" title="Very Happy">

    capx: ... 1199932939

  • Haha!

    That is exactly what I was trying to do. Thank you!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)