Starting from scratch, the android game programming (second edition) Chapter 11th actors and viewwindows started the presentation.

Source: Internet
Author: User
Tags map class
Chapter 2 actors and viewwindow

The content of this chapter is closely related to chapter 7 and chapter 8. If you are not familiar with the content of these chapters, please read chapter 7 and chapter 8 first, and then read back to this chapter.

An actor is an interface that is used to unify the behavior of classes (readers can read the article about the facede mode ). Let's use a metaphor to explain that actors have their own scripts, and the director says to all actors: Do the next action! Actors will act separately. Instead of telling the Director that you want to do this, he wants to do it. In the program, Frame Animation and dynamic graph block operations call completely different functions, which is not conducive to consistent processing in the game loop. So we let them all implement the actor interface. As long as they call the functions defined by the interface, they will make their own actions. The definition of actor interface is simple:

Public InterfaceActor {

Public VoidTick ();

}

For any type that implements the actor interface, only one tick command is sent.

See the example below:

We will create two classes, tank and map, inherited from Sprite and tiledlayer, which both implement the actor interface. Create Frame Animation for tank, create dynamic graph blocks for map, and display them in scenemain.

First, we can copy the example in Chapter 10 to create a new project and copy the org. yexing. Android. Games. Common package used in Chapter 8 to the project.

Create a tank class that inherits from Sprite and implements the actor interface.

Play Frame Animation in the tick Function

Public VoidTick (){

//TodoAuto-generated method stub

Nextframe ();

}

Create a map class inherited from tiledlayer to implement the actor interface.

Play a dynamic graph block in the tick Function

Public VoidTick (){

//TodoAuto-generated method stub

If(Getanimatedtile (-1) = 4 ){

Setanimatedtile (-1, 5 );

}Else{

Setanimatedtile (-1, 4 );

}

}

Finally, create the instances of these two classes in scenemain and display them.

Layer layers [] =NewLayer [2];

IntMapdate [] [] = {0, 0, 0, 2, 0, 0, 0, 2, 0, 0, 0 },

{0, 1, 0, 2, 0, 0, 0, 1, 0, 1, 0, 1, 0 },

{0, 1,-1,-1,-1, 0, 1, 1, 0, 1, 2, 1, 0 },

{0, 0, 0, 1, 0, 0, 0, 0, 0, 2, 0, 0 },

{3, 0, 0, 1, 0, 0, 2, 0, 0, 1, 3, 1, 2 },

{3, 3, 0, 0, 0, 1, 0, 0, 2, 0, 3, 0, 0 },

{0, 1, 1, 1, 3, 3, 3, 2, 0, 0, 3, 1, 0 },

{0, 0, 0, 2, 3, 1, 0, 1, 0, 1, 0, 1, 0 },

{2, 1, 0, 2, 0, 1, 0, 1, 0, 0, 0, 1, 0 },

{0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 2, 1, 0 },

{0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0 },

{0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0 },

{0, 1, 0, 1, 0, 1, 6, 1, 0, 1, 1, 1, 0 },};

PublicScenemain (){

Paint =NewPaint ();

Paint. setcolor (color.White);

Paint. settextalign (align.Center);

Bitmap BMP tank = bitmapfactory.Decodefile("/Sdcard/player1.png ");

Bitmap BMP tile = bitmapfactory.Decodefile("/Sdcard/tile.png ");

Tank tank =NewTank (BMP tank, 32, 32 );

Tank. setframesequence (New Int[] {0, 1 });

Map map =NewMap (13, 13, BMP tile, 32, 32 );

Map. createanimatedtile (4 );

For(IntY = 0; y

For(IntX = 0; x

Map. setcell (Y, X, mapdate [x] [Y]);

}

}

Layers [0] = map;

Layers [1] = tank;

}

@ Override

Public VoidUpdate (canvas c ){

//TodoAuto-generated method stub

C. drawargb (255, 0, 0, 0 );

C. drawtext ("scenemain", gameview.Width/2, gameview.Height/2, paint );

For(IntI = 0; I

(Actor) layers [I]). Tick ();

Layers [I]. Paint (C );

}

}

Readers need to pay attention to the call to tick in the update method. Run the program and we can see the animation effect.

Note that the bitmap file is placed in the root directory of the sdcard. You can find these two files in the Res/drawable project and push them to sdcard.

Next let's look at viewwindow. We can understand it as the front-end of the stage or the camera's viewfinder. The audience can only see the actor at the front-end. The actors step down and disappear from the audience's sight. Usually, a screen is a viewport, because we can't see anything outside the screen anyway. Sometimes, we need to customize a smaller view and local changes. As a matter of fact, we have already implemented the function of the viewport. It is in layermanager, but we have not used it before, so we have not introduced it.

Next let's take a look at the functions of layermanager. We know that Sprite and tiledlayer are inherited from the layer, so layermanager, as its name suggests, is used to manage the layer. Specifically, the management layer displays. To use layermanager, we first call the append () or insert () function to add layers to layermanager, just as we put the paper into a folder, then, you can call paint to display all layers at a time. Layer has a property: Z, which indicates the zcoordinate of the layer,

Zcoordinate points from the screen to the outside. That is to say, the larger the zcoordinate, the closer it is to the user. The front layer will cover the later layer.

We rewrite the previous program and use layermanager to replace the layer array.

PublicScenemain (){

Super();

......

Layermanager. append (MAP );

Layermanager. insert (tank, 100 );

......

Public VoidUpdate (canvas c ){

......

For(IntI = 0; I

(Actor) layermanager. getlayerat (I). Tick ();

}

Layermanager. Paint (C, 0, 0 );

}

Now that we are familiar with layermanager, let's learn the viewport. We can think of the layer as a very large canvas. They are placed in the layermanager container. The viewport is to open a small window on the container so that users can only see the part of the window, other regions are invisible.

Let's take a look at the layermanager constructor.

PublicLayermanager (){

Setviewwindow (0, 0, integer.Max_value, Integer.Max_value);

}

We can see that a viewport has been defined here. This is very large. We can imagine that he and the layer are generally large, so we cannot see his effect. But what if we want to narrow down the viewport?

Public VoidUpdate (canvas c ){

......

Layermanager. setviewwindow (0, 0,100,100 );

Layermanager. Paint (C, 0, 0 );

}

Run the program and you can see that only the part of the video is displayed.

The difficulty of viewwindow lies in coordinates. Let's look back

Layermanager. setviewwindow (X, Y, width, height );

Layermanager. Paint (C, x, y );

These two functions use coordinates, which have different meanings. First, let's take a look at the paint. The X and Y represent the coordinates of the upper left corner of the view relative to the screen. As we have said before, the viewport can be understood as a window on layermanager. By changing the coordinates, the entire layermanager will move along. Let's modify the program and display the viewport at (100,100 ).

Layermanager. Paint (C, 100,100 );

We can see that all layers are moved to the (100,100) position in the upper left corner.

Let's take a look at setviewwindow, where X and Y represent the coordinates of the viewport relative to the upper left corner of the layer. Let's modify their values to see the effect.

Layermanager. setviewwindow (50, 50,100,100 );

As you can see, the tank in the upper left corner is gone.

The use of setviewwindow can easily achieve the scrolling effect.

This chapter sample program http://u.115.com/file/f198eeda92

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.