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

#1 2012-10-08 23:15:39

shacker
New Scratcher
Registered: 2012-10-08
Posts: 3

A simple stop/start button

OK, I've looked at the tutorials and docs but am still not able to figure out something that is probably very basic.

Given a Button1 and a Sprite1, I want Button1 to start animation of Sprite1, and if clicked again, to stop animation of Sprite1.

But each sprite has its own script. I see the "Stop Script" control for Sprite1 but it only seems to affect Sprite1. How in the world do I make a click on Button1 affect the animation of Sprite1? They're in different script windows that seem to have no way to talk to each other.

All i want is this:

If Sprite1 is moving
  If Button1 is clicked
    Stop Sprite1
Else
  If Button1 is clicked
    Start Sprite1

Is there an example project that shows how to control one sprite by interacting with another?

Believe it or not, I'm an experienced programmer! I could do this easily in Javascript, Flash, or even Python, but can't seem to accomplish it in Scratch :)

Thanks,
Scot

Offline

 

#2 2012-10-08 23:45:49

TorbyFork234
Scratcher
Registered: 2012-03-01
Posts: 1000+

Re: A simple stop/start button

well I'm assuming that in each of those languages it has something built in saying it's playing, so you're going to have to make that yourself with a variable.
so here's the scripts:

when gf clicked //for button 1
set [animation v] to [0]
forever 
wait until <<mouse down?>and<touching [mouse pointer v]?>> //clicked
set [animation v] to <(1)-(animation)> //alternate between 1 and 0 (true and false)
when gf clicked //for sprite 1
switch to costume [starting costume v]
wait (0.5) secs //to allow time for animation to go back to 0, scratch isn't that fast
forever
if <(animation)=[1]> //if clicked
next costume //assuming your animation is a gif
end

Last edited by TorbyFork234 (2012-10-08 23:47:30)

Offline

 

#3 2012-10-09 00:39:19

kayybee
Scratcher
Registered: 2009-12-07
Posts: 1000+

Re: A simple stop/start button

The easiest way is to use a variable, but it may cause a ton of variables for a lot of sprites. Use, for button 1:

when [button 1] clicked
if <(motion)=[1]>
set [motion v] to [0]
else
set [motion v] to [1]
end
and for the sprite 1:
when gf clicked
forever
if <(motion)=[1]>
your animation goes here
end
end
Hope this helps.

Last edited by kayybee (2012-10-09 00:39:35)

Offline

 

#4 2012-10-09 01:33:45

shacker
New Scratcher
Registered: 2012-10-08
Posts: 3

Re: A simple stop/start button

Thanks guys, but I'm not sure this nails it.

In both of your examples, how or where is it specified that the *target* of the click on Button1 is Sprite1?

Sorry, if I'm being dense - I'm just not seeing the *connection* between  clicking on one object and instructions being sent to another.

Thanks.

Offline

 

#5 2012-10-09 02:32:19

Hardmath123
Scratcher
Registered: 2010-02-19
Posts: 1000+

Re: A simple stop/start button

Ah, you want OOP. Well, though luck: Scratch doesn't have great OOP. It doesn't even have data structures or (as of 1.4) procedures. Scratch has no form of "mind control", you cannot control a sprite from another sprite. You can however read data, which is actually enough (even though the code is ugly). Think of it as a server-client code: you cannot have Python running client-side, but you can have JS querying the Python client-side via XHR; and though it's uglier, it's also simpler in a couple of ways.

To solve your issue, the actual event you want is a broadcast; which is like a customized event which can have multiple handlers. So you should do something like this:

when [button v] clicked // on button sprite
broadcast [play/pause v]

when gf clicked // on animation sprite
set [animating? v] to (-1) // OFF

when I receive [play/pause v] // on animation sprite
set [animating? v] to ((animating?) * (-1)) // switch the state of the sprite

when gf clicked // on animation sprite
forever if <(animating) = (1)> // if ON
next costume // or whatever: knock yourself out!
Here, you don't need to specify the target, since any sprite can handle the "play/pause" event. If you absolutely need to specify the sprite, change the event name for each sprite, like "play/pause: Darth Vader" on one sprite and "play/pause: Luke Skywalker" on the other.

I hope that helped, good luck!  smile

Last edited by Hardmath123 (2012-10-09 02:32:53)


Hardmaths-MacBook-Pro:~ Hardmath$ sudo make $(whoami) a sandwich

Offline

 

#6 2012-10-14 12:15:04

shacker
New Scratcher
Registered: 2012-10-08
Posts: 3

Re: A simple stop/start button

Hardmath - Thanks a ton for the response! Sorry for the delay - I've been out sick for a week. Ah, too bad no OOP or simpler mechanisms for inter-sprite control. But your explanation makes good sense - I'll give it a go with my kid as soon as he's better.

Thanks again,
Scot

Offline

 

Board footer