I am trying to make an object move to a certain set of points if pointing in the correct direction. So far, my script is like this:
When UP ARROW key is pressed
If <DIRECTION = 0>
Glide 2 secs to X:50 Y:50
If <DIRECTION = 90>
Glide 2 secs to X:100 Y:100
If <DIRECTION = 180>
Glide 2 secs to X:150 Y:150
etc.
It looks fine to me but for some reason whenever I press UP the object doesn't move. Any ideas about what I'm doing wrong?
Also, how can I download Scratch 1.1 without wiping my existing files. I don't want a full installation just and incrimental one.
Last edited by ScipioBellorum (2007-07-01 08:42:20)
Offline
Looks like it should work, the only thing that springs to mind is if you are already in that location...
Try uploading the project so we can have a look.
Offline
It half works for me!
e.g. the script responds if the direction is 90 or -90 but the if statements are ignored if the direction is 0, 180 or -180
Looks like a bug.
regards
Simon
Offline
Update - if I insert a "point in direction" block at the start - all if statements work.
The 0, 180,-180 statements only fail if I manually turn the sprite in the area above the script work area.
regards
Simon
Offline
Ahh - probably means your manual turns were not *exactly* to 0,180 etc.
Offline
@Mayem
Try it on your computer and see if you can do any better :-)
Even with Direction being displayed on the stage and indicating 0.0 or -180.0, the if statements don't get executed.
regards
Simon
Offline
One should never do equality tests for floating point numbers. Instead one should test a range of numbers. For example
if abs(direction - 90) < 45 then ....
Offline
I think we need 2 forums - one for PhD Computer Science people and one for the proletariat :-)
I just think ScipioBellorum has just unearthed a little issue that could do with dealing with by the Scratch team. :-)
i.e
Some directions can seemingly be set manually to integers, others can't.
Or as Kevin points out, some directions can be successfully compared to integers but others can't. :-)
Also, while playing with this problem, I've noticed that you can't set direction to be 180 manually either - when pointing down its set to -180. This doesn't match up with the default suggested direction for down as being 180.
regards
Simon
Offline
I see what you mean about rotating manually to 180 and it only registering as -180. And therefore not triggering the "if" statement! I agree to be consistent it should default to 180 not -180.
Also, about manually rotating to 0 and it looking like 0 but probably being stored in the computer with decimal points, so therefore not equaling 0.
I will suggest that Scratch should only store direction as an integer--do you all think that makes sense?
In the meantime, you could use
for up (manually rotated to 0)
"if (direction < 1) and (direction > -1)"
for down (manually rotated to -180)
"if (direction < -179) and (direction > -181)"
Offline
If you wanted to be really comprehensive for down, so that it worked for 180 or -180, you could use:
"if (direction < -179) and (direction > -181) OR if (direction < 181) and (direction > 179)"
But I realize that's terribly long!
Offline
natalie wrote:
I will suggest that Scratch should only store direction as an integer--do you all think that makes sense?
My first thought is that with your relatively small screen, you're going to get some pretty funny paths if you do anything with the direction parameter and it only has integer values. However I haven't played with this much so it might be OK.
Offline
I think that direction should *not* be rounded to an integer---it is much easier for the programmer to put in round(direction)=90 than to try to recover the values that got rounded into oblivion. "Point towards <sprite>" should be done to full precision.
Incidentally, direction does round in squeak (bad) but not in java (last time I checked, which was back around v11 or so).
A different issue is what directions can be manually set. The hand rotation interface should probably round, since it is only for approximate positioning, and hand accuracy is not that great. Rounding to the nearest degree (or even nearest 5 degrees) may be fine.
Offline
I will suggest that Scratch should only store direction as an integer--do you all think that makes sense?
Since the problem only seems to occurs if you set direction manually, then I'd suggest leaving direction as a floating point number otherwise I imagine a few projects might not function as well as before.
If you set direction in a script to be 0 or -180, then an "if direction=0" or "if direction =-180" seem to work fine which all points towards it being a problem with the manual direction and not a real problem with comparing integers to floating points.
regards
Simon
Offline
Indeed - you can only turn the sprite manually in programing mode, anyway. This is a very isolated error.
At the most, fix the manual direction changer so that it operates to teh nearest degree, but please don't change the whole programs ability to operate to decimal places of a degree.
Offline