Now that you have lots of great features like your user interface controls and boats, you need to add them to the screen. To do so, the onDraw()
function needs an adjustment. Listing 9-11 contains the entire code for the function.
Ensure that your onDraw function looks exactly like Listing 9-11 or the images will not be drawn to the screen.
@Override
public void onDraw(Canvas canvas) {
canvas.drawColor(Color.BLUE);
ground.draw(canvas);
//the user controls
dock.draw(canvas);
marker.draw(canvas);
trash.draw(canvas);
cannonleftsmall.draw(canvas);
cannonrightsmall.draw(canvas);
cannondownsmall.draw(canvas);
cannonupsmall.draw(canvas);
for(int i = 0; i < pier_count; i++){
pier[i].draw(canvas);
}
for(int i = 0; i < boat_count; i++){
boat[i].draw(canvas);
}
for(int i = 0; i < cannon_count; i++){
cannon[i].draw(canvas);
}
for(int i = 0; i < 50; i++){
bullets[i].draw(canvas);
}
castle.draw(canvas);
}
Check out the group of sprites under the heading “user controls.” These include the dock, the marker, and the trash and cannon icons that users can select. The important note here is that the dock is obviously drawn first, then the marker, and then the icons. This way, you can always see the dock in the background. The marker is then free to highlight all of the icons from behind. Figure 9-1 shows how the dock looks.
At the end of the function, four for
loops go through the lists of sprites. Finally, the castle is drawn.
You always draw every bullet, even though they may or may not be moving at the current time. This is taken care of by the SpriteObject
class when it checks to ensure that sprites are alive before drawing them. With bullets ready to destroy the boats, we must create and keep track of the oncoming enemies. The next section covers the ins and outs of handling the boats.
13.58.81.111