Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » Variable multiplying when it only needs to change by 5
- Cyks
- Scratcher
100+ posts
Variable multiplying when it only needs to change by 5
I wasn't really sure what to title this post.
Anyway in my project here, in the sprites “enemy1”, and “PUShow” I am having a problem. The variable “Points” is supposed to change by 5 if one of the enemy1's clones's health gets to 0 (dies). If you shoot and destroy the enemies, the variable “points” shoots up to a really high number when it is only supposed to change by 5 for each clone destroyed. Does anyone know how to fix/what is going on?
Anyway in my project here, in the sprites “enemy1”, and “PUShow” I am having a problem. The variable “Points” is supposed to change by 5 if one of the enemy1's clones's health gets to 0 (dies). If you shoot and destroy the enemies, the variable “points” shoots up to a really high number when it is only supposed to change by 5 for each clone destroyed. Does anyone know how to fix/what is going on?
- lederniersamourai
- Scratcher
500+ posts
Variable multiplying when it only needs to change by 5
I have difficulties to play with your game and get the enemy1 being to health 0.
I shoot on big red circles but they never go to 0.
I have put in the PUShow this:
when I start as a clone
I shoot on big red circles but they never go to 0.
I have put in the PUShow this:
when green flag clickedDo not add another instance of
delete (all v) of [tests v]
when I start as a clone
add (join [pushow ] (length of [tests v])) to [tests v]
... the rest of your script
when I start as a clone
Last edited by lederniersamourai (Nov. 3, 2014 18:18:04)
- Cyks
- Scratcher
100+ posts
Variable multiplying when it only needs to change by 5
I have the “health” variable for enemy1 set to 2, so you need to shoot a clone twice to destroy it. I have difficulties to play with your game and get the enemy1 being to health 0.
I shoot on big red circles but they never go to 0.
I have put in the PUShow this:when green flag clickedDo not add another instance of
delete (all v) of [tests v]
when I start as a clone
add (join [pushow ] (length of [tests v])) to [tests v]
... the rest of your script
when I start as a clone
What exactly is this code supposed to do?
- lederniersamourai
- Scratcher
500+ posts
Variable multiplying when it only needs to change by 5
To show you at which stage clones are created. If there are too many of them, you will see it.
You can add also items to this list in otehr sprites. You will have a rough idea of the sequence of code execution.
You will see when it blows up and also see the items just before it blows up.
It is called tracing your program.
Quite easy to do in Scratch.
You can add also items to this list in otehr sprites. You will have a rough idea of the sequence of code execution.
You will see when it blows up and also see the items just before it blows up.
It is called tracing your program.
Quite easy to do in Scratch.
- Cyks
- Scratcher
100+ posts
Variable multiplying when it only needs to change by 5
Ohh ok. That is really helpful! Thanks!
Using this tracing code I was able to narrow the problem down to this code in the sprite “PUShow”:
Something I noticed was that if you destroy a clone of enemy1 and wait like 4 seconds or so and repeat, the points increment the way they are supposed to. But if you destroy them as fast as you can the multiplying thing happens. Again, does anyone know how to fix/what is going on?
Using this tracing code I was able to narrow the problem down to this code in the sprite “PUShow”:
when I receive [+5P v]
switch costume to [+5point v]
create clone of [myself v]
change [Points v] by (5)
Something I noticed was that if you destroy a clone of enemy1 and wait like 4 seconds or so and repeat, the points increment the way they are supposed to. But if you destroy them as fast as you can the multiplying thing happens. Again, does anyone know how to fix/what is going on?
- DesolateTen
- Scratcher
20 posts
Variable multiplying when it only needs to change by 5
Ohh ok. That is really helpful! Thanks!
Using this tracing code I was able to narrow the problem down to this code in the sprite “PUShow”:when I receive [+5P v]
switch costume to [+5point v]
create clone of [myself v]
change [Points v] by (5)
Something I noticed was that if you destroy a clone of enemy1 and wait like 4 seconds or so and repeat, the points increment the way they are supposed to. But if you destroy them as fast as you can the multiplying thing happens. Again, does anyone know how to fix/what is going on?
I haven't had a chance to look at your code yet, but two things could be happening. You could be hitting a lot more than one clone at once, or it could be triggering
change [score v] by (5)
multiple times per hit.
to keep the sprite from creating clones of clones, you could add this:
when I receive [create clone v]
if <(cID) = [0]> then
set [cID v] to [1 ]
clone
set [cID v] to [0 ]
end
end
Last edited by DesolateTen (Nov. 3, 2014 20:19:37)
- lederniersamourai
- Scratcher
500+ posts
Variable multiplying when it only needs to change by 5
DesolateTen is correct. It is a kind of “race conditon” between scripts.
Maybe scratch first sends a message.
Then the creatd clone is not killed before the next message is sent due to another hit.
You should trace the creation of message (at first line o when I receive and also at first line of the creation of clones (first line of when I start as a clone and also at the kill of lones (last line before killing the clone).
You might observe that some messages are coming before each clone is killed.
In my opinion, your code is clean and well structured.
The problem of displaying the +5p could be solved as follow:
the master sprite is identified as 0 in a local variable called id.
when you create a clone, you change this id to 1.
Then you frame the receive message with an “if 0 = id then” … which measn that only the master will create clones and add 5 points.
Now, you have the problem of being a multiple of too quick hits. In that case, your “receive message” will be “cut” at half execution.
The only best means is to use a list that you use as a buffer containing all hits request (you feed with “add ”hit 5“ or add ”hit 10“, …)
Then your PUShow will consume those lines in your list, ”eating" the first line only:
when i receive the message
do until list lenght = 0
create a clone
wait list length is decreased by one
when I start as a clone
I read and use the first line of the list
then I delete that first line.
then I continue to make some animation for displaying the points.
Maybe scratch first sends a message.
Then the creatd clone is not killed before the next message is sent due to another hit.
You should trace the creation of message (at first line o when I receive and also at first line of the creation of clones (first line of when I start as a clone and also at the kill of lones (last line before killing the clone).
You might observe that some messages are coming before each clone is killed.
In my opinion, your code is clean and well structured.
The problem of displaying the +5p could be solved as follow:
the master sprite is identified as 0 in a local variable called id.
when you create a clone, you change this id to 1.
Then you frame the receive message with an “if 0 = id then” … which measn that only the master will create clones and add 5 points.
Now, you have the problem of being a multiple of too quick hits. In that case, your “receive message” will be “cut” at half execution.
The only best means is to use a list that you use as a buffer containing all hits request (you feed with “add ”hit 5“ or add ”hit 10“, …)
Then your PUShow will consume those lines in your list, ”eating" the first line only:
when i receive the message
do until list lenght = 0
create a clone
wait list length is decreased by one
when I start as a clone
I read and use the first line of the list
then I delete that first line.
then I continue to make some animation for displaying the points.
Last edited by lederniersamourai (Nov. 3, 2014 20:51:12)
- Discussion Forums
- » Help with Scripts
- » Variable multiplying when it only needs to change by 5