I (like a lot of people) wanted to find a way to create and delete sprites in a script. After a bit of searching, it seems that it's not possible in Scratch at the moment. Then I had a look at the great things Jens is doing hacking and extending Scratch, and I knew I had to find a way, and I have!
Disclaimer: while this works, it creates a new version of not-quite Scratch. If you do this, the Scratch projects you create won't be compatible with other people's Scratch, nor with the Java applet.
Step 1: Modifying the code
I'm not going to list all of the steps here. Instead, look at Jens' project Flip: http://scratch.mit.edu/projects/Jens/75626
Do those things, but when it comes to adding commands to blockSpecs, do this:
1) make the changes in ScratchSpriteMorph instead of ScriptableScratchMorph. These new blocks only make sense for sprites.
2) add these commands to the blockSpecs:
'control' ('clone this sprite' - duplicateNoAttach) ('delete this sprite' - undoableDeleteSprite)
(before 'motion' - there's no 'control' bit in ScratchSpriteMorph)
duplicateNoAttach and undoableDeleteSprite are both methods that already exist, so you don't have to follow that part of the Flip example, just fileOut the changes and start Scratch to see the new blocks.
Step 2: Making use of the new blocks.
They're fully functional at this point, but to use them well requires some nice scripting. As it stands, you can create and delete sprites, but the sprites don't know whether they're master sprites or clones.
This is an outline of one way of making multiple clones of a sprite, where each clone has a unique identifier.
1) in your master sprite, create local ("For this sprite only") variables: isMaster, and instanceNumber
2) set the value of isMaster in the master sprite to 1 (make it visible, turn it into a slider, then drag it to 1 is one way)
3) create these scripts in the master sprite:
when I receive createClones if isMaster = 1 set isMaster to 0 repeat 5 set instanceNumber to 0 clone this sprite broadcast increaseInstance and wait set isMaster to 1
when I receive increaseInstance change instanceNumber by 1
when I receive deleteClones if isMaster = 0 delete this sprite
When you broadcast "createClones" the master (and only the master, if there are already some clones) will clone itself (5 times in the example above). The master will end up with it's isMaster set to 1, the clones will have isMaster = 0. The 5 clones will have different values of instanceNumber, ranging from 1 to 5, so you can get each of them to do something different if you want. And finally, when you broadcast "deleteClones" the clones will delete themselves but the master will live on.
I hope someone out there finds this useful and/or interesting. A big "Thank You!" goes to Jens, who showed the way.
Chris
Last edited by ChrisCrouch (2008-05-02 21:19:55)
Offline
Excellent, ChrisCrouch!
It's very clever to make use of already existing methods in the Scratch source code, and I love how you explain about master-sprites and clones. Thanks for sharing this cool idea!
Offline
Why I didn't know about this?!?!?!? I'm going to immediately try to integrate it in Scratch-with-lists created by our excellent Jens and see how it works.
Offline
Thats Awesome!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Offline
Is this going to be in the next version of Scratch? It would be very useful feature to have in the world of Virtual (Math) Manipulatives that I (and I am sure others) would like to create in scratch. You would just create one sprite and would have a clone mechanism to replicate it.
Offline
Oops. I had not used my applet for a while. The applet does not seem to work any more. I think they must not be working with the latest version of Java.
You have to imagine what the applet did ;=< (the small, medium and large buttons produced a small, medium and large triangle and then the user could produce as many as they wanted and even magnetize them together and clone the combined objects.) The delete button would delete them. This was done ten years ago but I can do all of this now myself without programmers in scratch;=> But I would really like to have clone and delete to limit my sprite making.
Offline
It turned out that in the end I didn't need the wonderful listhack by Jens. I finished my first clonehack project (you can download the hack from http://www.sitibs.com/files/stefano/Scratch1.2.1plusClone.zip). Well, I guess this is not the most efficient way of implementing arrays. But it works, and it was fun to work on it!
http://scratch.mit.edu/projects/s_federici/198523
Comments, improvements and critisisms are welcome.
Last edited by s_federici (2008-07-07 15:17:45)
Offline
I will actually try this now. I would consider myself a noob hacker at best......so I will just follow your instructions!
Offline
s_federici I downloaded your version. Clone and delete work fine. However I can not go to the presentation mode in your version:=>Do you know why? I am in Windows XP.
All,
How do we get the scratch team to include it in the next version now that the work is done. I also think that these belong in the "Looks" block and not "Control" this is debatable.
Offline
I Guess this is the reason that presentation mode does not work;=> I always had trouble reading instructions and diclaimers;=>
From Chris' first post
Disclaimer: while this works, it creates a new version of not-quite Scratch. If you do this, the Scratch projects you create won't be compatible with other people's Scratch, nor with the Java applet.
Offline
DrSuper wrote:
I Guess this is the reason that presentation mode does not work [...]while this works, it creates a new version of not-quite Scratch. If you do this, the Scratch projects you create won't be compatible with other people's Scratch, nor with the Java applet.
I don't think this is the reason. But thanks for pointing this out as I hadn't noticed it (and it is some time that I'm using this hack! but I usually don't use presentation mode...).
As this is not regular Scratch, it won't work on the website. But I guess that the reason why it doesn't work in presentation mode is due to the fact that the behaviour of "clone/delete this sprite" is taken from procedures that only work in design mode (the same procedures you activate by right-clicking sprites on the stage or in the library). So when you are in presentation mode something must be missing.
I will try to investigate some more. In the meantime, other Scratchers help is welcome
Offline
??????? I'm confused, couldn't you just use the
[blocks]<hide>[/blocks]
block?
But the code is pretty impressive, too!
Last edited by joeyman (2008-07-09 12:59:58)
Offline
joeyman wrote:
??????? I'm confused, couldn't you just use the
[blocks]<hide>[/blocks]
block?
But the code is pretty impressive, too!
What this is doing is copying a sprite. I looked at it. It gives 2 new blocks: Clone and Delete
Offline
great job! note: presentation mode (and other features) under Windows will work just fine if you put your altered image file into a new folder together with a copy of the vm (Scratch.exe) and of the two dll's (ScratchPlugin.dll and Mpeg3Plugin.dll).
Offline
Jens wrote:
presentation mode (and other features) under Windows will work just fine if you put your altered image file into a new folder together with a copy of the vm (Scratch.exe) and of the two dll's (ScratchPlugin.dll and Mpeg3Plugin.dll).
This is what I did. I have everything in a new folder. But when I write a very simple script, for example
when Sprite1 Clicked
clone this sprite
it only works when in design mode. If I click on the sprite when in presentation mode the sprite won't be duplicated.
Offline
...
Offline
How do you use it???
Offline
registeel wrote:
How do you use it???
Do you mean the clone hack? Download and unzip the whole hack folder from http://www.sitibs.com/files/stefano/Scratch1.2.1plusClone.zip. Then run the Scratch.exe you will find in the folder you unzipped. This hack has two new blocks in the "Control" category, namely "clone this sprite" and "delete this sprite". You can try the simple script I have shown before. Unfortunately, till now, it only works when in design mode. And, needless to say, it wont't work when uploaded to the site.
Offline