Download presentation
Presentation is loading. Please wait.
Published byBrianna Hutchinson Modified over 8 years ago
1
Enhancing JavaScript Using Application Programming Interfaces (APIs), Lecture #3
2
Goals By the end of this lecture, you should understand … How to trap keyboard events. How to use GameLib to manage sound How to improve sprite animation How to detect sprite collisions
3
Handling Keyboard Events – Step 1 The first thing we need to do in order to react to keyboard events is to import the keyboard library from GameLib: </script> </script>
4
Handling Keyboard Events – Step 2 Next, we need to activate a function to act as a “watchdog” for any keyboard events From the GameLib, we’ll use the function called Kb_trapkey(…): a = Kb_trapkey(“a”); b = Kb_trapkey(“b”); c = Kb_trapkey(“c”);
5
Handling Keyboard Events – Step 3 Although the Kb_trapkey(…) function detects keyboard actions, it doesn’t handle what to do in reaction to an event. To do this, we’ll need to write those reactions. This is typically done in a function and hooked to a timer using Gl_hook(…), usually in init():Gl_hook(“mainLoop()”);Gl_start();
6
Handling Keyboard Events – Step 4 The mainLoop() is written to react to keyboard events. we can use decision structures to check the letter.pressed property or we can use the global property called Kb_lastkey to “remember” the last key pressed:if(a.pressed){ //action for a //action for a} if(Kb_lastkey == c) { //action for c //action for c}
7
Handling Keyboard Events Examples http://www.cs.iupui.edu/~rmolnar/n341 /examples/API3/keybdTrapIE.html http://www.cs.iupui.edu/~rmolnar/n341 /examples/API3/keybdTrapIE.html For Internet Explorer: http://www.cs.iupui.edu/~rmolnar/n341 /examples/API3/keybdTrapIE.html http://www.cs.iupui.edu/~rmolnar/n341 /examples/API3/keybdTrapIE.html http://www.cs.iupui.edu/~rmolnar/n341 /examples/API3/keybdTrapNav.html http://www.cs.iupui.edu/~rmolnar/n341 /examples/API3/keybdTrapNav.html For Netscape Navigator: http://www.cs.iupui.edu/~rmolnar/n341 /examples/API3/keybdTrapNav.html http://www.cs.iupui.edu/~rmolnar/n341 /examples/API3/keybdTrapNav.html
8
The Sound Object GameLib includes a sound object. To work with the sound object, we need to import the appropriate library: </script> </script>
9
Instantiating New Sound Objects To create new sound objects, we use the Sd_add_sound(..) constructor: function init() { sndBang = new Sd_add_sound(“bang.wav”); sndBang = new Sd_add_sound(“bang.wav”); sndBang = new Sd_add_sound(“canyon.mid”); sndBang = new Sd_add_sound(“canyon.mid”);}
10
GameLib Sound Methods To play a sound:sound.play(); To stop playing a sound:sound.stop(); To play a sound:sound.loop(3);sound.play(); Specifies the number of times a sound should loop. Examples:
11
Advanced Sprite Animation Last time, we examined how to “animate” using a single sprite comprised of multiple “clips”, which served as frames of an animation. In the previous image, the composite sprite was a single row. What if one wanted to include more advanced animation, like having movement within a single “frame”? GameLib allows us to include a multidimensional matrix for animation.
12
Advanced Sprite Animation Examples Image: http://wally.cs.iupui.edu/n341-client/jsab10/plane1.gif http://wally.cs.iupui.edu/n341-client/jsab10/plane1.gif Animation: http://wally.cs.iupui.edu/n341-client/jsab10/simplePlane.html http://wally.cs.iupui.edu/n341-client/jsab10/simplePlane.html
13
Questions?
14
Next Time … More on responding to keyboard input Adding additional sprites Collision detection
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.