This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2013-01-04 15:04:16

NHAE
New Scratcher
Registered: 2012-10-11
Posts: 18

Activate/deactivate sprites by clicking

Being somewhat new to Scratch, my attempts at a script, which may be a bit beyond my capacity thus far, have so far been only semi-successful.

I would like to have the ability to mouse click a sprite to activate it, then do the same to deactivate it.

Essentially, I'd like to have multiple sprites on the screen which the user can click at any given time and have them deactivate each one at any given time as well.

Unfortunately, what I have so far works only somewhat; when clicked, the specific sprite is activated and when clicked again, it's deactivated, but I would like to have the ability for the user to click on and off repeatedly as necessary.

Here's a link if you'd like to see what my script looks like so far:

http://www.watermans.org/scratch.jpg

Offline

 

#2 2013-01-04 15:11:54

rdococ
Scratcher
Registered: 2009-10-11
Posts: 1000+

Re: Activate/deactivate sprites by clicking

Replace this section:

forever if <not <mouse down>>
set tempo to (133) bpm
switch to costume [costume2 v]
play note (96 v) for (0.5) beats
if <<touching [mouse-pointer v]> and <mouse down>>
switch to costume [costume1 v]
stop script
end
with:
forever if <not <mouse down>>
set tempo to (133) bpm
if <<touching [mouse-pointer v]> and <mouse down>>
switch to costume [costume1 v]
else
switch to costume [costume2 v]
play note (96 v) for (0.5) beats
end
and it will work.

Offline

 

#3 2013-01-04 15:14:40

Kileymeister_test
Scratcher
Registered: 2010-01-31
Posts: 100+

Re: Activate/deactivate sprites by clicking

I think employing some variables and broadcasting can help quite a bit:
http://i45.tinypic.com/t0knte.gif
@rdococ:  I don't think that will fully remedy the situation as the sprite will resume activation upon releasing the mouse, even though it is supposed to deactivate on click.

Last edited by Kileymeister_test (2013-01-04 15:16:19)


Kileymeister's test account.  I upload engines and demos and stuff for public use.
Currently Working on: Grapple Arms!

Offline

 

#4 2013-01-04 15:26:22

NHAE
New Scratcher
Registered: 2012-10-11
Posts: 18

Re: Activate/deactivate sprites by clicking

Kileymeister_test, this is great! I haven't had a chance to experiment yet with broadcasting, but your scripts really give me a better understanding of it for sure.

This works wonderfully so far in my script. Is there any way to hide the variable, or do you simply place your sprite in front of it?

Thanks so much, everyone!

Offline

 

#5 2013-01-04 15:32:04

NHAE
New Scratcher
Registered: 2012-10-11
Posts: 18

Re: Activate/deactivate sprites by clicking

Actually, it appears I overlooked the "hide variable..." function, so that seems to have worked.

Also, now that I have input 2 different sprites with two different sounds, they seem to activate simultaneously with the above script, regardless of my clicking one or the other. Any thoughts?

Offline

 

#6 2013-01-04 15:41:53

NHAE
New Scratcher
Registered: 2012-10-11
Posts: 18

Re: Activate/deactivate sprites by clicking

Okay it looks like I've figured out how to keep them separately activated by changing the name of the broadcast for each.

NOW, though, it seems after the first sprite is activated, I have to double click all subsequent sprites to activate them. Not a terrible problem as this is light years from where I was. Just updating on my progress as it happens.

Offline

 

#7 2013-01-04 17:06:25

Kileymeister_test
Scratcher
Registered: 2010-01-31
Posts: 100+

Re: Activate/deactivate sprites by clicking

Instead of changing the name of the broadcast, you can do something else which I should have told you in the first post (sorry!).

Variables can be public (meaning they apply to all sprites) or private (meaning they apply to a single sprite).  To make a private variable, check the "For this sprite only" box when creating it and writing its name.

You can tell a variable is private if it is separated from other variables by a line like this:
http://i49.tinypic.com/1039b8z.jpg

If you give each sprite a private variable, and use that private variable in its scripts, they should all react independently from each other even though they are all using the same broadcast name.

Last edited by Kileymeister_test (2013-01-04 17:06:50)


Kileymeister's test account.  I upload engines and demos and stuff for public use.
Currently Working on: Grapple Arms!

Offline

 

#8 2013-01-07 08:21:21

NHAE
New Scratcher
Registered: 2012-10-11
Posts: 18

Re: Activate/deactivate sprites by clicking

Wonderful. With all your help, I've definitely figured out exactly what I need in a pretty compact script. Thanks so much!

Offline

 

Board footer