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

#1 2008-01-06 19:29:47

B-Reyn428
Scratcher
Registered: 2007-09-08
Posts: 55

"forever" script problem

I am making a project with enemies that give experience when they die. The problem is that I need the forever script so that it will work more than once but then it keeps repeating the part where it gives me exp. As a script I tried:

When green flag clicked
Forever if [enemy hp] = 0 or [enemy hp] < 0
broadcast "enemy dead"

When I receive "enemy dead"
change exp by 5

How do I make the script always work after the green flag is clicked, but only happen once?


http://img233.imageshack.us/img233/7682/mybannerblinkyd0675d390.gif
http://scratch.mit.edu/projects/B-Reyn428/673836

Offline

 

#2 2008-01-06 19:33:19

funkymonkey
Scratcher
Registered: 2007-06-03
Posts: 1000+

Re: "forever" script problem

i think its like this:
<when green flag clicked>
<forever>
<if> <( <{ ememy HP }> <<> 1 )>
<change{ exp }by( 5

the difference between mine and yours is you used the <forever if>
block and i used
<forever>
<if>

Last edited by funkymonkey (2008-01-06 19:34:10)


http://i243.photobucket.com/albums/ff67/hprules_photos/banner2.jpg
Kuzimu: Dawn of a New Age                                                                                                  Coming May 2010

Offline

 

#3 2008-01-06 19:38:09

kevin_karplus
Scratcher
Registered: 2007-04-27
Posts: 1000+

Re: "forever" script problem

funkymonkey, all that your change does is to make the script slower, it does not change the meaning.

There are two solutions, depending on whether the enemy hit points ever go back up.
If not, then you can do

when greenflag clicked
wait until enemy HP < 1
change exp  by 5

If the hitpoints can go back up, then you can do

when greenflag clicked
forever
   wait until enemy HP < 1
   change exp by 5
   wait until enemy HP > 0

Offline

 

#4 2008-01-06 19:46:04

funkymonkey
Scratcher
Registered: 2007-06-03
Posts: 1000+

Re: "forever" script problem

kevin_karplus wrote:

when greenflag clicked
wait until enemy HP < 1
change exp  by 5

but then it only does it once, so whats the point? there are most likley going to be more ememies than just one


http://i243.photobucket.com/albums/ff67/hprules_photos/banner2.jpg
Kuzimu: Dawn of a New Age                                                                                                  Coming May 2010

Offline

 

#5 2008-01-07 08:19:44

EdnaC
Scratcher
Registered: 2007-08-28
Posts: 100+

Re: "forever" script problem

B-Reyn,
The issue with your script is that once the enemy is dead (its HP variable is < 0), your forever loop will keep piling on "experience" by broadcasting "enemy dead".  From the information that you've given, Kevin's solution looks like a very nice way to handle the problem.

If there are multiple enemies active at the same time, a broadcast is one way the enemies could notify the "Hero" that he/she made a kill.  In this case, each enemy could have its actions driven by a forever loop, and could keep track of their own health.  When an enemy's health gets to less than 1, it would broadcast "EnemyKilled", then stop its script (so that the broadcast only gets sent once).

When I receive "AnimateEnemy1"
Go to (start x and Y)
Show
Forever
(Do mean things that enemies do)
  If MyHealth < 1
  Hide
  Broadcast "EnemyKilled"
Stop Script

Another way to do the same thing would be to make the Hero's experience a variable "for all sprites".  Then, instead of having the enemy broadcast to the Hero, the enemy could just add to the experience variable when it "dies".

Funky,
The second script that Kevin proposed will address having another enemy, once the enemy is alive again (HP > 0) the script will go back to waiting for the next "kill".

Offline

 

Board footer