[Android game development 4] android game framework (a demo of game role walking on the screen)

Source: Internet
Author: User


Li huaming himiOriginal, reprinted must be explicitly noted:
Reprinted from[Heimi gamedev block]Link: http://www.himigame.com/android-game/298.html

Many kids shoes say that after my code is run, clicking home or back will cause a program exception. If you have encountered this, you certainly haven't carefully read the himi blog, in article 19th, himi specifically wrote about the causes and solutions of these errors. I have added my remarks on this blog, and my provincial children's shoes are always confused. Please click here to contact us for further reading:

[Android game development 19th] (required) surfaceview Running Mechanism explanation-analyze back and home buttons and switch to the background to handle exceptions!


Note: surfaceview does have the ondraw method, but surfaceview will not be called by itself !!!

The ondraw or draw in my code is a method defined by myself... Keep calling in the thread. Be sure to pay attention to it !!

 

In fact, the previous article on surfaceview analysis is a simple game framework. Of course, I would like to emphasize that it is a simple game framework, so do not let the experts not spray it out ~

 

This demo is a demo of image operations and button processing and a simple game framework for a group of kids shoes. We will share it with you here ~

 

 

 

 

Package COM. himi; <br/> Import android. content. context; <br/> Import android. content. res. resources; <br/> Import android. graphics. bitmap; <br/> Import android. graphics. bitmapfactory; <br/> Import android. graphics. canvas; <br/> Import android. graphics. color; <br/> Import android. graphics. paint; <br/> Import android. util. log; <br/> Import android. view. keyevent; <br/> Import android. view. surfaceholder; <br/> Import android. view. surfaceview; <br/> Import android. view. surfaceholder. callback; <br/> public class mysurfaceview extends surfaceview implements callback, runnable {<br/> private thread th = new thread (this); <br/> private surfaceholder SFH; <br/> private int SH, SW; <br/> private canvas; <br/> private paint P; <br/> private paint P2; <br/> private resources res; <br/> private bitmap BMP; <br/> private int BMP 100, BMP _y = 100; <br/> private Boolean up, down, left, right; <br/> private int animation_up [] = {3, 4, 5}; <br/> private int animation_down [] = {0, 1, 2 }; <br/> private int animation_left [] = {6, 7, 8}; <br/> private int animation_right [] = {9, 10, 11 }; <br/> private int animation_init [] = animation_down; <br/> private int frame_count; <br/> Public mysurfaceview (context) {<br/> super (context ); <br/> This. setkeepscreenon (true); <br/> res = This. getresources (); <br/> BMP = bitmapfactory. decoderesource (Res, R. drawable. enemy1); <br/> SFH = This. getholder (); <br/> SFH. addcallback (this); <br/> P = new paint (); <br/> P. setcolor (color. yellow); <br/> P2 = new paint (); <br/> p2.setcolor (color. red); <br/> P. setantialias (true); <br/> setfocusable (true); // Note 1 <br/>}< br/> Public void surfacecreated (surfaceholder holder) {<br/> Sh = This. getheight (); <br/> Sw = This. getwidth (); <br/> th. start (); <br/>}< br/> Public void draw () {<br/> canvas = SFH. lockcanvas (); <br/> canvas. drawrect (0, 0, SW, sh, P); // Note 2 <br/> canvas. save (); // Note 3 <br/> canvas. drawtext ("himi", bmp_x-2, bmp_y-10, P2); <br/> canvas. cliprect (BMP _x, BMP _y, BMP _x + BMP. getwidth ()/13, BMP _y + BMP. getheight (); <br/> If (animation_init = animation_up) {<br/> canvas. drawbitmap (BMP, BMP _x-animation_up [frame_count] * (BMP. getwidth ()/13), BMP _y, P); <br/>} else if (animation_init = animation_down) {<br/> canvas. drawbitmap (BMP, BMP _x-animation_down [frame_count] * (BMP. getwidth ()/13), BMP _y, P); <br/>} else if (animation_init = animation_left) {<br/> canvas. drawbitmap (BMP, BMP _x-animation_left [frame_count] * (BMP. getwidth ()/13), BMP _y, P); <br/>} else if (animation_init = animation_right) {<br/> canvas. drawbitmap (BMP, BMP _x-animation_right [frame_count] * (BMP. getwidth ()/13), BMP _y, P); <br/>}< br/> canvas. restore (); // Note 3 <br/> SFH. unlockcanvasandpost (canvas); <br/>}< br/> Public void cycle () {<br/> If (down) {<br/> BMP _y + = 5; <br/>} else if (up) {<br/> BMP _y-= 5; <br/>} else if (left) {<br/> BMP _x-= 5; <br/>} else if (right) {<br/> BMP _x + = 5; <br/>}< br/> If (down | up | left | right) {<br/> If (frame_count <2) {<br/> frame_count ++; <br/>} else {<br/> frame_count = 0; <br/>}< br/> If (down = false & up = false & left = false & Right = false) {<br/> frame_count = 0; <br/>}< br/> @ override <br/> Public Boolean onkeydown (INT key, keyevent event) {<br/> If (Key = keyevent. keycode_dpad_up) {<br/> If (up = false) {<br/> animation_init = animation_up; <br/>}< br/> up = true; <br/>} else if (Key = keyevent. keycode_dpad_down) {<br/> If (down = false) {<br/> animation_init = animation_down; <br/>}< br/> down = true; <br/>} else if (Key = keyevent. keycode_dpad_left) {<br/> If (Left = false) {<br/> animation_init = animation_left; <br/>}< br/> left = true; <br/>} else if (Key = keyevent. keycode_dpad_right) {<br/> If (Right = false) {<br/> animation_init = animation_right; <br/>}< br/> right = true; <br/>}< br/> return Super. onkeydown (Key, event); <br/>}< br/>/* (non-javadoc) <br/> * @ see android. view. view # onkeyup (INT, android. view. keyevent) <br/> */<br/> @ override <br/> Public Boolean onkeyup (INT keycode, keyevent event) {<br/> If (down) {<br/> down = false; <br/>} else if (up) {<br/> up = false; <br/>} else if (left) {<br/> left = false; <br/>}else if (right) {<br/> right = false; <br/>}< br/> return Super. onkeyup (keycode, event); <br/>}< br/> @ override <br/> Public void run () {<br/> // todo auto-generated method stub <br/> while (true) {<br/> draw (); <br/> cycle (); <br/> try {<br/> thread. sleep (100); <br/>} catch (exception ex) {<br/>}< br/> @ override <br/> Public void surfacechanged (surfaceholder holder, int format, int width, int height) {<br/> // todo auto-generated method stub <br/>}< br/> @ override <br/> Public void surfacedestroyed (surfaceholder holder) {<br/> // todo auto-generated method stub <br/>}< br/>}

 

Note 1

This method is used to respond to buttons! If you define a class that inherits the view, and implement the onkeydown method again, the onkeydown method is called only when the view gets the focus, the onkeydown method in actvity is called only when all controls do not process the key event.

 

Note 2

Screen swiping is also performed on the screen. In fact, this is only one kind. I also used the drawrgb method in the previous article. Of course, I can also use fillrect to screen swiping.

So here I want to talk about it. In the inherited view, the ondraw method is automatically called by the system. Unlike surfaceview, The ondraw method is called continuously in the run, in view, we can use the invalidate ()/postinvalidate () Methods to call the ondraw method. This is also different from surfaceview!

 

Note 3

Canvas. Save (); and canvas. Restore (); match each other to save the canvas state and retrieve the Saved state. Here, I will explain a little bit,

When we rotate, zoom, and pan the canvas, we actually want to operate on specific elements, such as slices and rectangles, but when you use the canvas method to perform these operations, the entire canvas is actually operated, and then the elements on the canvas will be affected, therefore, we call canvas before the operation. save () to save the current status of the canvas. After the operation, extract the previously saved status, which will not affect other elements.

 

For canvas. Save (); and canvas. Restore ();, there are still many children's shoes that you don't understand. OK, let me add:

 

Code Segment 1:

Public void draw () {<br/> canvas = SFH. lockcanvas (); <br/> canvas. drawcolor (color. black); <br/> canvas. drawbitmap (BMP 1, 0, 0, paint); <br/> canvas. save (); <br/> canvas. scale (1.5f, 1.5f); <br/> canvas. restore (); <br/> canvas. drawbitmap (BMP 2, 0, 0, paint); <br/> SFH. unlockcanvasandpost (canvas); <br/>} 

Code Segment 2:

Public void draw () {<br/> canvas = SFH. lockcanvas (); <br/> canvas. drawcolor (color. black); <br/> canvas. drawbitmap (BMP 1, 0, 0, paint); <br/> canvas. scale (1.5f, 1.5f); <br/> canvas. drawbitmap (BMP 2, 0, 0, paint); <br/> SFH. unlockcanvasandpost (canvas); <br/>} 

In the above two code snippets, we assume there are two images, BMP 1 and BMP 2, and they are all painted on the canvas!

Code segment 1 and code segment 2 are different:

Code segment 1We saved the canvas status before zooming the canvas, and then pulled out the previously saved status after zooming. This is done to ensure that BMP 2 is normally drawn and not affected by zooming!

Code Segment 2! Then we drew BMP 2, which will also be affected by the scaling !!

Therefore, if we do not want to affect the rendering of other parts when processing an image separately, we should do the following:

Public void draw () {<br/> canvas = SFH. lockcanvas (); <br/> canvas. drawcolor (color. black); <br/> canvas. drawbitmap (BMP 1, 0, 0, paint); <br/> canvas. save (); <br/> canvas. scale (1.5f, 1.5f); <br/> canvas. drawbitmap (BMP 2, 0, 0, paint); <br/> canvas. restore (); <br/> SFH. unlockcanvasandpost (canvas); <br/>} 

 

(We recommend that you subscribe to this blog, because our update speed is very fast ~ Wahaha)

Source code: http://www.himigame.com/android-game/298.html

 

 

 

 

 

Related Article

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.