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

Just started using Construct 2? Post your questions here

Post » Sun Apr 05, 2015 3:08 pm

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
AND
buttonsprite variable1=0 (which is default value)

System create objectsprite
buttonsprite add 1 to variable1
-----------------------------------------------

To destroy the object:
------------------------------------------------
On mouse click on buttonsprite
AND
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!
B
8
S
2
Posts: 16
Reputation: 635

Post » Sun Apr 05, 2015 10:58 pm

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.
ImageImage
B
102
S
45
G
22
Posts: 772
Reputation: 21,493

Post » Mon Apr 06, 2015 1:29 am

Message: mkea is not yet permitted to post plain text URLS (300 rep required). 1 URLS removed. Why?
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!
B
8
S
2
Posts: 16
Reputation: 635

Post » Mon Apr 06, 2015 1:41 am

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 ;)
ImageImageImageImage
B
157
S
64
G
41
Posts: 2,590
Reputation: 34,640

Post » Mon Apr 06, 2015 6:47 am

@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.
ImageImage
B
102
S
45
G
22
Posts: 772
Reputation: 21,493

Post » Tue Apr 07, 2015 2:29 am

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]

subevent:
system variable > 1
system set variable to 0

subevent:
system variable = 0
objectsprite destroy

subevent:
system variable = 1
system create objectsprite
------------------------------------------------------

It seems to be working, though I feel like there out to be a more streamlined solution...?
B
8
S
2
Posts: 16
Reputation: 635

Post » Tue Apr 07, 2015 7:57 am

Image

...or....

Image
Image
B
63
S
23
G
7
Posts: 1,480
Reputation: 10,796

Post » Tue Apr 07, 2015 8:15 am

also you can use visibility (visible , invisible)
B
52
S
18
G
10
Posts: 209
Reputation: 8,466

Post » Tue Apr 07, 2015 2:56 pm

@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.
B
8
S
2
Posts: 16
Reputation: 635

Post » Tue Apr 07, 2015 3:30 pm

Image
B
63
S
23
G
7
Posts: 1,480
Reputation: 10,796

Next

Return to Beginner's Questions

Who is online

Users browsing this forum: No registered users and 1 guest