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
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
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] endand for the sprite 1:
when gf clicked forever if <(motion)=[1]> your animation goes here end endHope this helps.
Last edited by kayybee (2012-10-09 00:39:35)
Offline
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
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.
Last edited by Hardmath123 (2012-10-09 02:32:53)
Offline
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