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

#1 2010-03-08 17:58:19

MammothPrincess
Scratcher
Registered: 2010-03-07
Posts: 2

Variable Wont Change

I am making a game for a school project, i have everything programmed already and everything works, until i make one of the sprites move. The player controls one sprite while the other is moved by the random operator. I have the program set up where if the space key is pressed then the sword sprite moves across the screen, and if the sword sprite is touching the bad guy sprite then the bad guy health variable will go down by 1, and this works perfectly fine if the bad guy sprite does not move. But when I add the script to make the bad guy sprite move the variable does not change if the sword sprite is touching it. I have tried the "if touching color__", "if color __ is touching color __", "if touching __".

Please help and i hope i have explained this easy enough to understand. Thanks in advance :) (FYI Sprite 10 in the script is the sword sprite) Heres what i have as the script:
<forever>
<if><( BAD GUY <>>0  )>
<glide( 1 )secs to x:(<pick random(-230  )to(230   )y:(<pick random( 170 )to(-170  )
<end>
<if><( BAD GUY  <<>0  )>
<broadcast[ Hide]
<broadcast[ Lose]<end>
<if><touching[ Sprite10]
<change{ BAD GUY }by(-1)
<end>

Last edited by MammothPrincess (2010-03-08 17:59:10)

Offline

 

#2 2010-03-08 18:38:00

infinitum3d
Scratcher
Registered: 2009-05-10
Posts: 100+

Re: Variable Wont Change

I don't know if this matters, but you have If BADGUY less than 0 and also if BADGUY greater than 0, but nothing for if BADGUY = 0  ?

Offline

 

#3 2010-03-08 18:42:27

Larry828
Scratcher
Registered: 2007-05-30
Posts: 100+

Re: Variable Wont Change

My experience with the *glide* command is that once a sprite starts gliding it doesn't pay attention to anything until it has reached the end of its glide.

So if it touches the sword in mid-glide it probably won't pause to recognize it!!

I just did a test of your script in a quick project and here's what happened:
        The BadGuy (given a score of +2 to start) glided around the screen over and over again, touching the sword lots of times, but the variable never changed.
        It never changed UNTIL it just happened to randomly choose to end one of its glides right on the sword!!  Then the variable dropped by 1.
        The BadGuy kept gliding until it happened to end a glide on the sword two more times (giving it a Score of -1) and it immediately disappeared. 

Generally I solve the problem by changing the *glide* to a *repeat* block where the sprite would move a few steps, check to see if it's touching the sword, then repeat that process.  (You could include a "if on edge, bounce" to keep the BadGuy moving continuously.)

In other words, write the script so as the BadGuy moves it is continuously checking to see if it's touching the sword!  (The glide doesn't allow that.)

Good luck!

Last edited by Larry828 (2010-03-08 21:43:48)

Offline

 

#4 2010-03-08 21:01:39

MammothPrincess
Scratcher
Registered: 2010-03-07
Posts: 2

Re: Variable Wont Change

Thank you for the reply it does work now  smile

Offline

 

Board footer