Discuss Scratch

user432109
Scratcher
22 posts

Help me again

Hi, I'm working on a idle tower defence upgrade tycoon type game and I need help
Here's what I have so far: https://scratch.mit.edu/projects/868524426/
1. I need help with the turret shooting the clones after the first one is dead
2. I need help so that the clones also have 5 hit points
3. I need help so that once the clone is dead it doesn't double
4. Everytime the bullet disapears more clones are made

Last edited by user432109 (Feb. 26, 2025 01:23:40)

imfh
Scratcher
1000+ posts

Help me again

It looks like one of your biggest problems is a misunderstanding of how clones and broadcasts work. Remember: Clones can receive broadcasts.

If you have code like this:

whenIreceiveGet Hurtcreatecloneofmyself

You will create a big problem. Every time you broadcast Get Hurt, every clone will clone itself. This will make your sprites start doubling.

To get around this, you can create variables which have “for this sprite only” enabled. These variables are different for every clone you make. Your health variable should be for this sprite only instead of for all sprites.

With a for this sprite only variable, you can do this:

whenclicked Only runs for the original spritesetis clone?tofalsewhenIstartasaclone Only runs for clonessetis clone?totruewhenIreceivebroadcast1ifisclone?=falsethencreatecloneofmyselfelse. . .

I would fix your code by doing something like this:

whenIstartasaclone In the bullet sprite. . .foreveriftouchingEnemy?then. . .broadcastGet Hurtandwait and wait is important so this doesn't get deleted yet. . .deletethisclonewhenIreceiveGet Hurt In the enemy spriteiftouchingBullet?then. . .changehealthby-1 Make sure health has "For this Sprite Only" checked!ifhealth<1thendeletethisclone

I also notice that your bullet clones are sending broadcasts whenever they get deleted to tell the turret to do the firing animation. Similarly, the enemy sprite creates a clone when it gets deleted. This is kind of confusing. Instead, I would a single loop in the original sprites to handle making clones. This will make it easier to eliminate bugs.

I hope this gives you some ideas!

whenclicked In the turret spriteswitchcostumetocostume1foreverrepeat9nextcostume Does the animation just like your codecreatecloneofbulletwhenIstartasaclone In the bullet spritegotoTurretpointtowardsMouse You will probably need a better way to auto aimshowrepeatuntiltouchingenemy?ortouchingedge?move2stepsbroadcastCheck if hurtandwaitdeletethisclonewhenclicked In the enemy spriteforevercreatecloneofmyselfwait5secswhenIstartasaclone In the enemy spritesethealthto5 Make sure health has "For this Sprite Only" checked!gotox:240y:pickrandom-150to150showrepeatuntilxposition<200move1stepsbroadcastEnemy Reached EdgeandwaitdeletethisclonewhenIreceiveGet Hurt In the enemy spriteiftouchingBullet?thenchangehealthby-1ifhealth<1thendeletethisclone
user432109
Scratcher
22 posts

Help me again

So do I delete all my original code and replace it with this?
user432109
Scratcher
22 posts

Help me again

imfh wrote:

It looks like one of your biggest problems is a misunderstanding of how clones and broadcasts work. Remember: Clones can receive broadcasts.

If you have code like this:

whenIreceiveGet Hurtcreatecloneofmyself

You will create a big problem. Every time you broadcast Get Hurt, every clone will clone itself. This will make your sprites start doubling.

To get around this, you can create variables which have “for this sprite only” enabled. These variables are different for every clone you make. Your health variable should be for this sprite only instead of for all sprites.

With a for this sprite only variable, you can do this:

whenclicked Only runs for the original spritesetis clone?tofalsewhenIstartasaclone Only runs for clonessetis clone?totruewhenIreceivebroadcast1ifisclone?=falsethencreatecloneofmyselfelse. . .

I would fix your code by doing something like this:

whenIstartasaclone In the bullet sprite. . .foreveriftouchingEnemy?then. . .broadcastGet Hurtandwait and wait is important so this doesn't get deleted yet. . .deletethisclonewhenIreceiveGet Hurt In the enemy spriteiftouchingBullet?then. . .changehealthby-1 Make sure health has "For this Sprite Only" checked!ifhealth<1thendeletethisclone

I also notice that your bullet clones are sending broadcasts whenever they get deleted to tell the turret to do the firing animation. Similarly, the enemy sprite creates a clone when it gets deleted. This is kind of confusing. Instead, I would a single loop in the original sprites to handle making clones. This will make it easier to eliminate bugs.

I hope this gives you some ideas!

whenclicked In the turret spriteswitchcostumetocostume1foreverrepeat9nextcostume Does the animation just like your codecreatecloneofbulletwhenIstartasaclone In the bullet spritegotoTurretpointtowardsMouse You will probably need a better way to auto aimshowrepeatuntiltouchingenemy?ortouchingedge?move2stepsbroadcastCheck if hurtandwaitdeletethisclonewhenclicked In the enemy spriteforevercreatecloneofmyselfwait5secswhenIstartasaclone In the enemy spritesethealthto5 Make sure health has "For this Sprite Only" checked!gotox:240y:pickrandom-150to150showrepeatuntilxposition<200move1stepsbroadcastEnemy Reached EdgeandwaitdeletethisclonewhenIreceiveGet Hurt In the enemy spriteiftouchingBullet?thenchangehealthby-1ifhealth<1thendeletethisclone
So do I delete all my original code and replace it with this?
user432109
Scratcher
22 posts

Help me again

Is the person who sent this going to answer me?
imfh
Scratcher
1000+ posts

Help me again

user432109 wrote:

So do I delete all my original code and replace it with this?
No, don't do that. It probably won't work if you do that. You can update your code based on what I said, but I would not start over using the code I gave you.

The code I gave is just to highlight what your problem is and some ways you can fix it. It isn't meant to totally replace your code.
thisisagoodway
Scratcher
34 posts

Help me again

imfh wrote:

user432109 wrote:

So do I delete all my original code and replace it with this?
No, don't do that. It probably won't work if you do that. You can update your code based on what I said, but I would not start over using the code I gave you.

The code I gave is just to highlight what your problem is and some ways you can fix it. It isn't meant to totally replace your code.
Got it, okay, (user432109 is the same person as me, this is my main)
user432109
Scratcher
22 posts

Help me again

imfh wrote:

It looks like one of your biggest problems is a misunderstanding of how clones and broadcasts work. Remember: Clones can receive broadcasts.

If you have code like this:

whenIreceiveGet Hurtcreatecloneofmyself

You will create a big problem. Every time you broadcast Get Hurt, every clone will clone itself. This will make your sprites start doubling.

To get around this, you can create variables which have “for this sprite only” enabled. These variables are different for every clone you make. Your health variable should be for this sprite only instead of for all sprites.

With a for this sprite only variable, you can do this:

whenclicked Only runs for the original spritesetis clone?tofalsewhenIstartasaclone Only runs for clonessetis clone?totruewhenIreceivebroadcast1ifisclone?=falsethencreatecloneofmyselfelse. . .

I would fix your code by doing something like this:

whenIstartasaclone In the bullet sprite. . .foreveriftouchingEnemy?then. . .broadcastGet Hurtandwait and wait is important so this doesn't get deleted yet. . .deletethisclonewhenIreceiveGet Hurt In the enemy spriteiftouchingBullet?then. . .changehealthby-1 Make sure health has "For this Sprite Only" checked!ifhealth<1thendeletethisclone

I also notice that your bullet clones are sending broadcasts whenever they get deleted to tell the turret to do the firing animation. Similarly, the enemy sprite creates a clone when it gets deleted. This is kind of confusing. Instead, I would a single loop in the original sprites to handle making clones. This will make it easier to eliminate bugs.

I hope this gives you some ideas!

whenclicked In the turret spriteswitchcostumetocostume1foreverrepeat9nextcostume Does the animation just like your codecreatecloneofbulletwhenIstartasaclone In the bullet spritegotoTurretpointtowardsMouse You will probably need a better way to auto aimshowrepeatuntiltouchingenemy?ortouchingedge?move2stepsbroadcastCheck if hurtandwaitdeletethisclonewhenclicked In the enemy spriteforevercreatecloneofmyselfwait5secswhenIstartasaclone In the enemy spritesethealthto5 Make sure health has "For this Sprite Only" checked!gotox:240y:pickrandom-150to150showrepeatuntilxposition<200move1stepsbroadcastEnemy Reached EdgeandwaitdeletethisclonewhenIreceiveGet Hurt In the enemy spriteiftouchingBullet?thenchangehealthby-1ifhealth<1thendeletethisclone
Is the “when I recieve broadcast1” is that supposed to be when I recieve hurt or somthing?
user432109
Scratcher
22 posts

Help me again

imfh wrote:

user432109 wrote:

So do I delete all my original code and replace it with this?
No, don't do that. It probably won't work if you do that. You can update your code based on what I said, but I would not start over using the code I gave you.

The code I gave is just to highlight what your problem is and some ways you can fix it. It isn't meant to totally replace your code.
Heres what I have from what you gave me: https://scratch.mit.edu/projects/868524426/
user432109
Scratcher
22 posts

Help me again

imfh wrote:

user432109 wrote:

So do I delete all my original code and replace it with this?
No, don't do that. It probably won't work if you do that. You can update your code based on what I said, but I would not start over using the code I gave you.

The code I gave is just to highlight what your problem is and some ways you can fix it. It isn't meant to totally replace your code.
Are you there?

Powered by DjangoBB