[MWForum]Re: MIA, Ask an Expert: t1 wont shoot

Daniel Ajoy mwforum@lists.mathcats.com
Tue, 05 Oct 2004 22:11:40 -0500


On 5 Oct 2004 at 20:23, askanexpert@openworldlearning.org wrote:

> from: maxlev@levinenet.net
> date: Tue, 5 Oct 2004 20:23:39 -0600
> 
> In my game, tractorr is a tractor facing right, and 
> tractorl is a tractor facing left.  And bullet, is a turtle 
> which is, well, a bullet.  It's instructions are: repeat 
> 200 [glide 1 1] cancel [touching? "bullet "t2] ht.  t2 is 
> what i want the bullet to hit, and stop.   Here is my 
> program:
>
> to
> startup
> t2, clickon
> make "key ascii readchar
> forever [
> if "key = 32 [shoot]
> ]
> end
> 
> to move
> t1,
> if "key = 39 [setsh "tractorr seth 90 glide 30 1]
> if "key = 37 [setsh "tractorl seth 270 glide 30 1]
> end 
> 
> to shoot
> bullet,
> setpos ask "t1 "pos
> seth ask "t1 "heading
> st clickon
> 
> when [touching? "bullet "t2] [
> bullet, clickoff ht
> stopme
> ]
> end
> 
> For some reason, t1 wont shoot, or move. Please help!




Well, here you have some problems:

> to
> startup
> t2, clickon
> make "key ascii readchar
> forever [
> if "key = 32 [shoot]
> ]
> end
> 
> to move
> t1,
> if "key = 39 [setsh "tractorr seth 90 glide 30 1]
> if "key = 37 [setsh "tractorl seth 270 glide 30 1]
> end 


The readchar instruction should be inside the forever.
Otherwise it will run once and never again. 

And "key = 32 will always be false because
the word "key" will never be equal to the word "32".

What you might want to test is if the *value* of
the variable whose name is "key" is equal to 32.
You use : for that.

You could use the structure in:

How do I steer the turtle with keyboard keys?
http://mia.openworldlearning.org/howdoi/swk.htm


Daniel
OpenWorld Learning (OWL)