Fjas;Dlkfjas;Dlfjas

Total Page:16

File Type:pdf, Size:1020Kb

Fjas;Dlkfjas;Dlfjas

Production: Set underlined text in a monospace (computer) font Please replace the following code: [md] = em dash [nd] = en dash [ts] = times (multiplication) symbol

??Andy: Just as a reminder for the CD, all associated html files for this chapter include absolute paths. Michael

Melody/Author: I’d like to rescind one of my editing decisions in Chapter 9. In that chapter, I decided to set references to the layer object in monospace as a keyword (i.e., “the layer object”). Basically, I assumed that creating a layer object required a layer keyword (just as creating a document object requires use of a document keyword). After reading this chapter, however, I’ve seen the light. It doesn’t appear that a layer keyword is necessary to create a layer in the code, must like creating a sprite object apparently doesn’t require the use of a sprite keyword. So if you could simply remove the underlining for “layer” that I used in Chapter 9 (and also make sure that the term is capitalized in any headings), that chapter will be consistent with this one and the next two. Sorry for the inconvenience and the boneheaded editorial judgment. – andy the copyeditor

It's a little crazier than that. In chapter eight, there actually was a layer keyword. It is an HTML tag used only in Netscape. Gamelib also has a layer object, but you do not need to use the term "layer" to invoke it. -Andy H.

Chapter 10 Using Other GgamelLib Features: The Dogfight Game

[H1]Introduction

Sprite graphics are a very powerful tool. With a few more enhancements, you are well on your way to building sophisticated 2D games. In this chapter, you will learn about some of the other great tools that gameLlib gives you for building better games. In particular, you will learn how to do the following:

***PD: begin unnumberbulleted list

*How to Build multiplayer games

*How to Trap keyboard events

*How to Manage sound

*How to Generate layers

*How to Add missiles

*How to Improve your animations

***PD: end unnumberbulleted list

[H1]The Dogfight Game

To demonstrate these features, you will build a simple two- player arcade game. The game is simple: Two players sit at the same keyboard and control little biplanes. The players try to blast each other out of the sky. Figures 10.1 and 10.2 show the game’s interface.

***PD: Please insert Figure jg10Fig01.pcx dogfight main screen Figure 10.1 Two planes enjoying a blissful afternoon.

***PD: Please insert Figure jg10Fig02.pcx dogfight shooting Figure 10.2 Take that, Red Baron!

The players can shoot each other down

The game has a number of interesting features that might not all be apparent from the screen shots.

First, this is a two-player game. Although While it might seem that writing a game for two players is would be more difficult than writing one for a single player, this is not always the case. The hardest part of "player vs.versus the computer" style games is developing the artificial intelligence for the computer player. With a two- player game, this is not a problem, as the computer does not need to store any strategies, but simply just respond to two sets of input.

The game uses keyboard input. Both versions of the document object model provide techniques to read mouse and keyboard events, but these two models are (surprise) very different in the browser implementations. The gameLlib API provides a simpler multi-browser interface, which makes managing keystrokes reasonably straightforward. Keyboard input provides a much smoother type of input for many types of games than the techniques that you have used previously in the earlier parts of this book.

***PD: begin notehint

SinceBecause JavaScript is an Internet technology, you might be tempted to write a two- player game with the players on different computers. Sadly, JavaScript does not have the communication functions to enable you to accomplish do this. You would probably need a language like Java forto write multi-computer games.

***PD: end hintnote

The Dogfight game features sound effects when the players shoot at each other, and when they score hits. The gameLlib library encapsulates the techniques demonstrated in Chapter eight 8 for generating sound. You might recall from the chapter that generating sound can be generation was quite challenging in that chapter, but it is reasonably easy using gameLlib.

To create the Dogfight game’s The documentation and scorekeeping parts of the game, I used were described using gameLlib layers. A layer is gameLlib's way of specifying an object likesuch as a Netscape layer or CSS element. In fact, you will see that with liberal use of layer and sprite objects, you can write the entire program was written as a series of gamelLib objects. The HTML in the program is almost nonexistent.

You will notice some new animation techniques in the Dogfight game. The planes can shoot out bullets, and if you look very carefully, you will note that the propellers appear to spin.

???Andy: You mentioned another OOP technique above (encapsulation). Would this concept be a candidate for a “In the real world” segment or a sidebar? Michael [H1]Using Layers and the Keyboard

To Begin your tour of the new gamelLib features, start by looking at a program that demonstrates keyboard input and gamelLib layers. Figure 10.3 shows the program’s interface.

***PD: Please insert Figure jg10Fig03.pcx LayerKey showing A Figure 10.3 When the user presses the A key, the letter "A" appears in a yellow box.

[H2]Creating the Keyboard and LayerKey Demo Program

Author: Can we delete this heading? The sections are so brief and intertwined, and the headings so similar, that presenting both headings seems unnecessary and odd. –andy the copyeditor

***PD: Please insert Figure jg10Fig03.pcx LayerKey showing A Figure 03 When the user presses the A button, the letter "A" appears in a yellow box.

The program traps for the "A", "B", and "C" keys. The yellow box is a layer. It's content can be changed dynamically, and it will appears to be part of the original page.

[H2]Setting Up the PlaygroundProgram Author: I’m not sure what you mean by “playgound.” The program doesn’t seem to include a playground. Is this a term that your audience needs to be familiar with? If so, please define it. – andy the copyeditor

As usual, you will begin by importing gamelLib modules and setting up some variables:

***PD: Begin Code