Game Development Reference
In-Depth Information
} else if (Touch.touching && !Touch.containsTouch(rect)) {
var opposite = Touch.position.y - this.position.y;
var adjacent = Touch.position.x - this.position.x;
this.rotation = Math.atan2(opposite, adjacent);
}
};
First you retrieve the rectangle that represents the part of the cannon you can touch to select a
different color. You add a simple property for calculating this rectangle. Then you check whether the
rectangle contains a touch press. If that is the case, you change the color. By using an if instruction,
you can easily cycle through the three colors.
If the player is touching the screen somewhere, but not inside the rectangle, you change the aim of
the cannon to be directed at that position. Also check out the Ball class to see how it handles touch
input! Finally, another nice thing to do is load different sprites depending on whether the application
is running on a tablet or on a desktop computer:
if (Touch.isTouchDevice)
sprites.gameover = loadSprite("spr_gameover_tap.png");
else
sprites.gameover = loadSprite("spr_gameover_click.png");
When the game is running on a touch device, the overlay has the text “Tap to continue”; otherwise it
says “Click to continue”.
Note Many modern laptops contain both a touch screen and a keyboard, and it's not always obvious how
to automatically determine whether a player wants to use touch or keyboard input. Players might want to use
the touch screen for some games, and the keyboard for others. One possible solution is to accept both inputs
at the same time. Another solution is to let the player make his own choice of input method through a
menu setting.
What You Have Learned
In this chapter, you have learned:
How to automatically adapt the dimensions of the game screen according to
different devices
How to automatically correct the mouse position according to the dimensions of
the game screen
What an array is and how to use one
How to use a touch interface to control what is happening in the game
 
 
Search WWH ::




Custom Search