Game Development Reference
In-Depth Information
Moving the car and game world together
When our car moves about the scrolling world, we will want to simulate movement in a manner
very different from how we have done it in games so far in the topic.
The game output screen (called canvasBitmapData in our game) is one 384
384 BitmapData
instance. The world is 50 tiles by 50 tiles or 1,600
1,600 pixels. Again, the trick we need to
perform is to keep the player car in the center of the screen for most of the game. And as we
said, this is simply done by moving the camera position to scroll the world rather than moving the
car about the game screen, but this solution poses a problem when the car is near the edge of
the screen. At that point, we cannot scroll the screen to keep the player in the middle anymore. In
this instance, we will give up control back to the car for only the axis direction that is affected.
How does this work in practice?
Let's say the car is supposed to be at the position 700
900 in the game world. In this
instance, the car sprite would be centered on the output screen at 192
192. The camera
would need to offset 700
900 to find the upper x and y coordinates for it to start. Let's step
through the process:
1.
First, player.worldX equals 700 , and player.worldY equals 900 .
2.
Then camera.x equals player.worldX minus .5 times camera.width , that is, 508. And
camera.y equals player.worldY minus .5 times camera.height , that is, 708.
3.
So the camera would start a 508,708 and need thus show from 508 to 892 on the x
axis and 708 to 1102 the y axis. This example is illustrated in Figure 10-7.
Figure 10-7. Player centered on canvas in the world
Search WWH ::




Custom Search