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

#1 2008-05-02 02:20:49

ChrisCrouch
Scratcher
Registered: 2008-05-02
Posts: 1

Hacking Scratch for dynamic sprites

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:

Code:

    '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:

Code:

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

Code:

when I receive increaseInstance
change instanceNumber by 1

Code:

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

 

#2 2008-05-02 04:07:39

Paddle2See
Scratch Team
Registered: 2007-10-27
Posts: 1000+

Re: Hacking Scratch for dynamic sprites

Absolutely fascinating!  Great work.


http://i39.tinypic.com/2nav6o7.gif

Offline

 

#3 2008-05-02 16:10:15

fullmoon
Retired Community Moderator
Registered: 2007-06-04
Posts: 1000+

Re: Hacking Scratch for dynamic sprites

Uhhh...wow...I'm going to faint now.

Last edited by fullmoon (2008-05-02 16:10:34)


http://i302.photobucket.com/albums/nn100/fullmoon32/wow.jpg

Offline

 

#4 2008-05-05 04:31:52

Jens
Scratcher
Registered: 2007-06-04
Posts: 1000+

Re: Hacking Scratch for dynamic sprites

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!


Jens Mönig

Offline

 

#5 2008-05-23 04:56:10

redsooty9
Scratcher
Registered: 2008-03-06
Posts: 69

Re: Hacking Scratch for dynamic sprites

What an excellent demonstration!


Redsooty9

Offline

 

#6 2008-06-18 16:10:43

s_federici
Scratcher
Registered: 2007-12-18
Posts: 500+

Re: Hacking Scratch for dynamic sprites

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

 

#7 2008-06-18 17:59:34

Cyclone103
Scratcher
Registered: 2008-03-20
Posts: 500+

Re: Hacking Scratch for dynamic sprites

Thats Awesome!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!


All your base are belong to us

Offline

 

#8 2008-06-18 23:22:19

DrSuper
Scratcher
Registered: 2008-06-10
Posts: 37

Re: Hacking Scratch for dynamic sprites

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

 

#9 2008-06-18 23:29:39

DrSuper
Scratcher
Registered: 2008-06-10
Posts: 37

Re: Hacking Scratch for dynamic sprites

You can see an example of what I am talking about at:
http://www.galaxy.gmu.edu/~devsuper/test8/geojrmain.html

Offline

 

#10 2008-06-18 23:46:32

DrSuper
Scratcher
Registered: 2008-06-10
Posts: 37

Re: Hacking Scratch for dynamic sprites

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

 

#11 2008-06-19 13:56:03

DrSuper
Scratcher
Registered: 2008-06-10
Posts: 37

Re: Hacking Scratch for dynamic sprites

It must have been a problem at my home PC my applet does work;=>

Offline

 

#12 2008-06-26 17:26:50

s_federici
Scratcher
Registered: 2007-12-18
Posts: 500+

Re: Hacking Scratch for dynamic sprites

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

 

#13 2008-07-06 15:32:47

Cyclone103
Scratcher
Registered: 2008-03-20
Posts: 500+

Re: Hacking Scratch for dynamic sprites

I will actually try this now. I would consider myself a noob hacker at best......so I will just follow your instructions!


All your base are belong to us

Offline

 

#14 2008-07-09 12:13:28

DrSuper
Scratcher
Registered: 2008-06-10
Posts: 37

Re: Hacking Scratch for dynamic sprites

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

 

#15 2008-07-09 12:17:18

DrSuper
Scratcher
Registered: 2008-06-10
Posts: 37

Re: Hacking Scratch for dynamic sprites

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

 

#16 2008-07-09 12:30:01

s_federici
Scratcher
Registered: 2007-12-18
Posts: 500+

Re: Hacking Scratch for dynamic sprites

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  smile

Offline

 

#17 2008-07-09 12:31:06

adriangl
Scratcher
Registered: 2007-07-02
Posts: 1000+

Re: Hacking Scratch for dynamic sprites

Wow! Good thinking!  big_smile


Scratchin' since 2007

Offline

 

#18 2008-07-09 12:59:16

joeyman
Scratcher
Registered: 2008-03-28
Posts: 1000+

Re: Hacking Scratch for dynamic sprites

??????? 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)


Joeyman: On Scratch since March 28, 2008.
Over 142 projects! (And recently a Brony, and PROUD of it!)

Offline

 

#19 2008-07-09 13:02:16

Bluestribute
Scratcher
Registered: 2008-01-24
Posts: 1000+

Re: Hacking Scratch for dynamic sprites

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


http://img247.imageshack.us/img247/1204/bluestributett4.jpg
That's my PSN ID. I know tons of COD4 glitches. Add me as your friend. Oh, and get a headset

Offline

 

#20 2008-07-09 22:27:46

joeyman
Scratcher
Registered: 2008-03-28
Posts: 1000+

Re: Hacking Scratch for dynamic sprites

Awesome!


Joeyman: On Scratch since March 28, 2008.
Over 142 projects! (And recently a Brony, and PROUD of it!)

Offline

 

#21 2008-07-10 04:00:00

Jens
Scratcher
Registered: 2007-06-04
Posts: 1000+

Re: Hacking Scratch for dynamic sprites

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).


Jens Mönig

Offline

 

#22 2008-07-10 12:33:10

s_federici
Scratcher
Registered: 2007-12-18
Posts: 500+

Re: Hacking Scratch for dynamic sprites

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

 

#23 2008-07-10 12:44:52

joeisawesome
Scratcher
Registered: 2008-06-10
Posts: 1000+

Re: Hacking Scratch for dynamic sprites

...


http://image.hotdog.hu/user/Alex-21/Zenekarok-Ensiferum.jpg

Offline

 

#24 2008-07-10 12:52:24

registeel
Scratcher
Registered: 2008-04-27
Posts: 500+

Re: Hacking Scratch for dynamic sprites

How do you use it???

Offline

 

#25 2008-07-10 13:44:09

s_federici
Scratcher
Registered: 2007-12-18
Posts: 500+

Re: Hacking Scratch for dynamic sprites

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

 

Board footer