Thread: Key events
View Single Post
Super Moderator
Status: Offline
Posts: 250
Join Date: Dec 2012
Location: Frosno, Ca
Default 05-25-2014, 09:03 AM

Originally Posted by jrrTolkien View Post
Okay, so I'm trying to make a racing game in Alice, but I have encountered an error in my programming. I have "while UP is pressed, do" I made it "realistically" accelerate (I think it's realistic, anyway.) Then, I have "after (the key is pressed), do car.stop." Then, it slows down. I attached my program so you can see how it works and everything. The problem I have is that when I let go of the button, it slows down until the mph hits 0. If I try to speed up again, it won't until mph reaches 0. Is there some way to speed up while keeping the acceleration and deceleration? If I'm not being clear, please say so.

Go to the car.stop method and remove the enclosing while loop.
From your other code, I assume you understand how the events work but liking the sound of my own typing, I'll say the following:
As long as the event is active (the key is pressed, XXX is true ...) the event works like loop. Each time the triggered method ends Alice checks to see if the event is still active and if so calls the method again.
The difference between this and a method with an infinite loop, besides the fact that there is an event to test, is that Alice controls the scheduling. This means that other events and methods can happen "at the same time." A method with an infinite loop "blocks" keeping anything else from happening until it is done (which is never.)


Mark Henwood
Reply With Quote