Discuss Scratch

thinkpeet
New Scratcher
5 posts

Trying to delete last created clone

Dear scratchers,

This is a project I am working on as part of an online course:

https://scratch.mit.edu/projects/182135416/

Don't mind the looks or the fact that the sprite “Erwin” can only move left or right for now: I am trying first to get the folowwing working:

When Sprite Erwin touches the sprite Samsung, he gets money (I am teasing a friend who is buying an overpriced iphone . This money comes in the form of a clone ofm a sprite called “Cash”, which is a stack of dollar bills. The cloning works: everytime Erwin hits the Samsung phone a clone appears. I am planning on making the game more difficult by having the phone sprites move randomly, and then, whenever sprite Erwin touches the iPhone sprite, the last ‘earned’ cash clone should dissapear. This last part I can't get working properly.

So far, I have sprite Erwin broadcast a message ("oh no!') whenever he touches the iphone sprite. Then when the cash sprite receives this message, it should delete the last created clone. I have tried multiple ways of coding with counting variables and if-statements, but so far, the only results I got was that either all clones were deleted, or no clone was deleted

Can anyone send me on the right way? I would really like to understand what I do wrong!

All help appreciated!
deck26
Scratcher
1000+ posts

Trying to delete last created clone

You need to give each clone an ID variable (local) and maintain a clone count (global).

changeclone-countby1 run both these blocks to create a clonecreatecloneofmyselfwhenIstartasaclonesetcloneIDtoclone-count cloneID MUST be local - this sprite only variablewhenIreceiveoh-noifcloneID=clone-countthenwait0secschangeclone-countby-1deletethisclone
I've included the wait to ensure one clone changing the variable doesn't mean the second last one also gets deleted but it may not be necessary.
kl77
Scratcher
21 posts

Trying to delete last created clone

I think I can explain why all clones get deleted. I think you are trying to get the iphone to broadcast a message to the money whenever erwin touches it, and you are trying to get the sprite to delete ONE of the clones. I am afraid this is not possible. Clones also respond to messages broadcasted from other sprites. So when you create this script: “when I receive message,”: delete this clone, then all clones get deleted.

I also looked at your project and know why your iphone and samsung are not moving. You did not place a “when flag clicked” block before it all( events section first block). Then, they will start to move. As for the money, I would suggest that rather than using clones, use separate sprites altogether, one for each stack of money. Make it so that each money sprite has a script that asks it to first when flag clicked, hide and if no of money stacks = a certain no, show. First you create a script for the phones that they will increase or decrease the variable no of money stacks when touching Erwin. Then, for the first money sprite, you create an if statement “if variable no of money stacks is equal to 1, show.” So when it is equal to 1, the first money sprite will show(initially, all of them will hide.) And then like wise you add similar scripts to the other sprites. When it is two, another sprite will show. When it is three, the next sprite will show. So now, it looks like the money stacks are increasing.

I hope you like my idea and take off the words “work in progress” in your project. Good Luck!
deck26
Scratcher
1000+ posts

Trying to delete last created clone

kl77 wrote:

I think I can explain why all clones get deleted. I think you are trying to get the iphone to broadcast a message to the money whenever erwin touches it, and you are trying to get the sprite to delete ONE of the clones. I am afraid this is not possible.
Not the case, my method will work.
thinkpeet
New Scratcher
5 posts

Trying to delete last created clone

Ok first off: thanks to you both! I really appreciate it!

If I understand you both correctly, accoring to kl77 it is not possible to delete just one, last, clone, and according to deck26, it should be?

@ deck26 I will try your tips first, deck26, although I have tried something similar based on previous forum-posts. Can you confirm that it should indeed be possible to just delete the last clone, and let the others exist until the iPhone is touched by Erwin again? In that case, I is my mistake somewhere and I won't give up.

@ kl77 Thanks for your tips too. I will try deck26's tips first, since that would create the script that I want, if possible. As to the phone sprites not moving: I removed the event-block on purpose to have better control. It was my way of ‘commenting out’ the code (to put it on hold for a while). Is there a more elegant way to ‘pause’ code?

Thanks again!
thinkpeet
New Scratcher
5 posts

Trying to delete last created clone

deck26 wrote:

kl77 wrote:

I think I can explain why all clones get deleted. I think you are trying to get the iphone to broadcast a message to the money whenever erwin touches it, and you are trying to get the sprite to delete ONE of the clones. I am afraid this is not possible.
Not the case, my method will work.

Ok I am not giving up then
deck26
Scratcher
1000+ posts

Trying to delete last created clone

Be careful though - if the sprite being cloned is receiving a broadcast as the instruction to make the clone you need to ensure only the sprite makes clones - no clones making clones. So give the sprite cloneID=0 and you can tell it apart from the clones and ensure only it responds to the broadcast.
deck26
Scratcher
1000+ posts

Trying to delete last created clone

Here's a remix https://scratch.mit.edu/projects/187618145/

Note that I've used clicking on the sprites to make things happen - you can code your own sprite movement and touch detection.
thinkpeet
New Scratcher
5 posts

Trying to delete last created clone

Ok thanks deck26, I am going to play with it!
thinkpeet
New Scratcher
5 posts

Trying to delete last created clone

Dear deck26,

It is working! great!

One more question. It seems that if the Erwin sprite has the sensing script, it does not register being touched by the phones when they float around. Alternatively, if I put the sensing code on the phone-sprites, the message is not broadcast if the player lets Erwin touch the phone with key controls. When I put the sensing code it both phones and Erwin, it gets buggy. Do I simply have to make a choice?
deck26
Scratcher
1000+ posts

Trying to delete last created clone

thinkpeet wrote:

Dear deck26,

It is working! great!

One more question. It seems that if the Erwin sprite has the sensing script, it does not register being touched by the phones when they float around. Alternatively, if I put the sensing code on the phone-sprites, the message is not broadcast if the player lets Erwin touch the phone with key controls. When I put the sensing code it both phones and Erwin, it gets buggy. Do I simply have to make a choice?
Both objects can detect the touch if necessary but only one should be making the broadcast. Also don't expect to detect touching while gliding - the code after a glide block is only run after the glide is completed.

So as you have it at present Erwin will detect touching as long as you're moving him as the key press scripts do the detecting but the phones won't do any detecting unless they've just completed a glide. Instead of glide you may want to use move n steps in a loop until touching edge and then point in a new direction. That then means you can get the phones to detect the touch if necessary but you may not actually need them to.

Last edited by deck26 (Nov. 20, 2017 14:10:57)

9supreme
New Scratcher
2 posts

Trying to delete last created clone

Hi I am trying to delete a clone in one level and start a new one on the next as I do not want the clone from level 3 continuing on to level 4 with level 4s clone there as well I have tried a lot of ways to put the delete code in but all never worked Your text to link here…
deck26
Scratcher
1000+ posts

Trying to delete last created clone

9supreme wrote:

Hi I am trying to delete a clone in one level and start a new one on the next as I do not want the clone from level 3 continuing on to level 4 with level 4s clone there as well I have tried a lot of ways to put the delete code in but all never worked Your text to link here…
Please start your own topic rather than necroposting.

Creating clones involves work so deleting one just to recreate it seems pointless. Manage what it is doing in the different levels including as it changes.
blue_meteor74
Scratcher
1 post

Trying to delete last created clone

deck26 wrote:

You need to give each clone an ID variable (local) and maintain a clone count (global).

changeclone-countby1 run both these blocks to create a clonecreatecloneofmyselfwhenIstartasaclonesetcloneIDtoclone-count cloneID MUST be local - this sprite only variablewhenIreceiveoh-noifcloneID=clone-countthenwait0secschangeclone-countby-1deletethisclone
I've included the wait to ensure one clone changing the variable doesn't mean the second last one also gets deleted but it may not be necessary.
thx!

Powered by DjangoBB