Introduction to Events http://www.alice.org/resources/lessons/introduction-to-events/
Introduction to Events Interactivity Interactive programming means that the order of the program or outcome is not predetermined when we press play. In Alice there are two different ways to make an Alice world there are two different ways to make the outcome of the world depend on what happens during runtime: Include conditional statements that use randomized functions to determine the direction of a movement or outcome causing the program to perform differently every time it is executed - randomization functions Include user input such as mouse clicks or key presses that influence the way that the program executes - user input functions and events
Introduction to Events Events An event is something that happens. In Alice events can be constructed to happen based on one of the following major categories: Scene Activation/ Time - events driven by time elapsing Keyboard Inputs - events that are driven by user key presses Mouse - events that are driven by mouse clicks and interactions Position/ Orientation - events that are driven by the orientation of objects to each other such as objects colliding, being near or viewing each other.
Introduction to Events Event Listener The first things you need to do is to make your program aware that it needs to pay attention for something to happen. Until you do this the program will not bother to listen for a mouse click or a keypress or pay attention to whether an object came close to or hit another object. In Alice we call this setting up an event listener. These listeners run concurrently to the other processes in your program.
Introduction to Events Event Handler Once your program knows an event happened you still need to tell the computer what to do. We call these directions the event handler. In Alice 3 you can program the event handler directly into the event listener procedure but we recommend you write event handling methods. This will help you manage and organize your program.
Event Listener Types
Event Listener Types Scene Activation/ Time SceneActivationListener - This listener is setup to trigger in Alice when you press the Run button. By default this listener triggers myFirstMethod to begin your program when the Run button is clicked. TimeListener - This listener can be set up to fire off at different time increments by making use of the computers internal clock.
Event Listener Types Keyboard Events Keyboard events will listen for different keypresses and will be trigger when a key is pressed and can also support functions that will return and pass along which individual key was pressed to be used for any number of scene manipulations or object controls.
Event Listener Types Mouse Events The mouse event listeners track the movement of the mouse and the mouse click. These also work for built in trackpads. The listener will also check for information related to the mouse click like where the mouse carrot is located on the screen or what objects are under the mouse carrot when clicked.
Event Listener Types Position / Orientation Events Another very useful event listener constructed for you in Alice doesn’t check for external information instead it monitors and triggers based on events inside your world. These events are based on tracking the position and orientation of objects and triggering event you tell it to watch for happen such as two object colliding or when you can view and object.
Creating an Event
Creating an Event Initialize Event Listener To add events to you program first you navigate to a special procedural method in Alice called initializeEventListeners. You will see that one listener already exists that will call myFirstMethod. It is possible to add other procedures to this listener to have them run in sequence to myFirstMethod or within a do together to have them run concurrently.
Creating an Event Add Event Listener From the drop down select the type of listener you would like to add. As with other procedural methods some may require that you input default parameters to add them to your program.
Creating an Event Create Event Handler It is a good idea to create the directions you want to occur when the event is triggered in a separate method. Be sure to name it something that is easy for you to remember and for someone to understand. Separating the handler from the listener will make it easier for you to find and edit it later. It may also turn out that you want multiple events to call the same method.
Creating an Event Call Event Handler You need to then add the custom procedure into the event handler so that when the trigger happens it will run your directions. Some types of listeners will require further refinement to achieve the desired functionality. In this example any key press will cause the hare to say hello. We probably want to program a specific key to do that to allow other keys to do other things.
Modify Event Listeners There are a couple ways to refine the way that event listeners function. You may need to use these to limit the scope of the trigger, give more detail as to how the or when the handler should be initiated, or to pass required information to the handler. You may want to do this for things like: To know which key was pressed to determine which handler to trigger. To have mouse clicks only affect certain objects in the scene.
Creating an Event Add Details to Listener Just like in other built in procedural methods there are optional parameters under add detail for some of the different listeners. You can use these to affect things like: Set Of Visual - This will affect the object that can be affected by the event listener - object array Multiple Event Policy - This will affect how your program handles if multiple events of this type are triggered at the same - combine, ignore, or que Held Key Policy - What to do if the key is held down - multiple triggers or one trigger on press or release
Creating an Event Use Listener Functions Different listeners have special built in functions that will also allow you to use them to return more information about the event. These can then be used to inform the event handler in specific ways via conditionals in the event listener or by passing the information into the handler through a parameter. Some very useful ones are: isKey (???) - allows you build conditional methods for different keys to have them call different methods getModelAtMouseLocation - which can allow you to pass the selected model into the method as a parameter to have it respond differently to different objects
Built In Event Handlers
Built In Event Handlers Add Object Move For One of our built in fully developed event handlers is addObjectMoveFor. This will allow you to easily add keyboard interactions to a world. To add the default addObjectMoveFor select it from the drop down and then select the object you want the keypresses to manipulate from the nested menu. The handler for this event maps several keypresses to the basic move procedures for the object you selected: Up Arrow / W Keys - Move forward 1 Down Arrow / S Keys - Move Backward 1 Left Arrow / A Keys - Turn Left 1 Right Arrow / D Keys - Turn Right 1
Built In Event Handlers Add Default Model Manipulation Another fully implemented event handler is the default model manipulation. This is a global handler when added to your world will enable this manipulation for all objects in the world. The underlying system does the following: Listener - Get the object at the location of the mouse click Handler - While clicked move object to track mouse location
Tips and Tricks
Tips and Tricks First Person Camera The easiest way to set up an interactive world with a first person camera is to use the add object mover for and select camera as your object. This will make your camera move based on the keyboard inputs similar to many first person games. An added trick for a 3rd person camera is to add the main character and adjust the camera to a point over the shoulder and use the setVehicle property in the scene editor to vehicle the camera to the object. Then add the movement to the object and the camera will follow it around.
Tips and Tricks Using Say To Debug Since Alice renders your code in the virtual world we don’t a simple text based way to easily track if an event is triggering when you expect it to. A good trick is to use the say command for a character to visualize when something has happened.