一个 android游戏框架 转载.docx
- 文档编号:10076552
- 上传时间:2023-05-23
- 格式:DOCX
- 页数:17
- 大小:21.50KB
一个 android游戏框架 转载.docx
《一个 android游戏框架 转载.docx》由会员分享,可在线阅读,更多相关《一个 android游戏框架 转载.docx(17页珍藏版)》请在冰点文库上搜索。
一个android游戏框架转载
Android游戏框架
楼主
发表于2011-7-1308:
50:
22|查看:
1849|回复:
16
AndroidSDK中游戏示例代码的框架也剥出来共享给大家,自称为真正实用的游戏框架。
已拿其开发过一款游戏,感觉还可以。
其主要“实用”之处在于:
1.框架整体很小,上手快,做大游戏的公司没多少,做小游戏的个人开发者铺天盖地,相比动则上升到引擎级的庞然大物,小型的框架更加实用,更加考虑大多数开发者的利益。
2.小不等于不完善,之所以剥SDK的游戏示例代码的框架出来,就是学习后发现它对于系统事件的处理比较完善,另外我个人还修正了它对于来电处理等异常事件不完美之处,使其更加完善。
R.layout.main布局文件
java代码:
1.
xmlversion="1.0"encoding="utf-8"?
>
2.
3. 4.xmlns: android= 5. 6. 7.android: orientation="vertical" 8.android: layout_width="match_parent" 9.android: layout_height="match_parent"> 10. 11. 12.android: id="@+id/gameview" 13.android: layout_width="match_parent" 14.android: layout_height="match_parent"/> 15. 16. 17.android: layout_width="match_parent" 18.android: layout_height="match_parent"> 19. 20. 21.android: id="@+id/textview" 22.android: layout_width="wrap_content" 23.android: layout_height="wrap_content" 24.android: layout_centerInParent="true" 25.android: gravity="center_horizontal" 26.android: textColor="#88ffffff" 27.android: textSize="24sp"/> 28. 29. 30. 31.
复制代码
Activity类
java代码:
1.packageeoe.demo;
2.
3.importcom.mstanford.gameframe.R;
4.importandroid.app.Activity;
5.importandroid.os.Bundle;
6.importandroid.util.Log;
7.importandroid.view.Menu;
8.importandroid.view.MenuItem;
9.importandroid.widget.TextView;
10.
11.publicclassGameActivityextendsActivity{
12.
13.privateGameSurfaceViewgameSurfaceView;
14.privateGameThreadgameThread;
15.
16.@Override
17.publicvoidonCreate(BundlesavedInstanceState){
18.super.onCreate(savedInstanceState);
19.setContentView(R.layout.main);
20.
21.gameSurfaceView=(GameSurfaceView)this.findViewById(R.id.gameview);
22.gameThread=gameSurfaceView.getThread();
23.gameSurfaceView.setTextView((TextView)findViewById(R.id.textview));
24.
25.if(savedInstanceState==null){
26.//游戏第一次启动时,初始化游戏状态
27.gameThread.doStart();
28.Log.w(this.getClass().getName(),"SISisnull");
29.}else{
30.//从其他应用界面切回游戏时,如果Activity重新创建,则恢复上次切出游戏时的各项数据
31.gameThread.restoreState(savedInstanceState);
32.Log.w(this.getClass().getName(),"SISisnonnull");
33.}
34.}
35.
36./**
37.*当Activity被切换到后台时调用,存储Activity重新创建时需要恢复的游戏数据
38.*/
39.
40.@Override
41.protectedvoidonSaveInstanceState(BundleoutState){
42.super.onSaveInstanceState(outState);
43.gameThread.saveState(outState);
44.Log.w(this.getClass().getName(),"SIScalled");
45.}
46.
47./**
48.*当Activity被切换到后台时调用,在这里对游戏做"暂停"的处理
49.*/
50.
51.@Override
52.protectedvoidonPause(){
53.super.onPause();
54.//暂停游戏
55.gameSurfaceView.getThread().pause();
56.}
57.
58./**
59.*当Activity切换到前台时调用
60.*/
61.
62.@Override
63.protectedvoidonResume(){
64.super.onResume();
65.//游戏结束暂停状态,游戏正常进行
66.gameSurfaceView.getThread().unpause();
67.}
68.
69./**
70.*创建游戏菜单
71.*/
72.
73.@Override
74.publicbooleanonCreateOptionsMenu(Menumenu){
75.//TODOAuto-generatedmethodstub
76.returnsuper.onCreateOptionsMenu(menu);
77.}
78.
79./**
80.*定义游戏菜单的点击事件处理
81.*/
82.
83.@Override
84.publicbooleanonOptionsItemSelected(MenuItemitem){
85.//TODOAuto-generatedmethodstub
86.returnsuper.onOptionsItemSelected(item);
87.}
88.
89.@Override
90.protectedvoidonDestroy(){
91.super.onDestroy();
92.Log.v(this.getClass().getName(),"onDestroy");
93.
94.//停止游戏
95.gameThread.setRunning(false);
96.booleanretry=true;
97.while(retry){
98.try{
99.//阻塞Activity的主线程直到游戏线程执行完
100.gameThread.join();
101.retry=false;
102.}catch(InterruptedExceptione){
103.}
104.}
105.}
106.
107.}
复制代码
SurfaceView类
java代码:
1.packageeoe.demo;
2.
3.importandroid.content.Context;
4.importandroid.os.Handler;
5.importandroid.os.Message;
6.importandroid.util.AttributeSet;
7.importandroid.util.Log;
8.importandroid.view.KeyEvent;
9.importandroid.view.SurfaceHolder;
10.importandroid.view.SurfaceView;
11.importandroid.view.SurfaceHolder.Callback;
12.importandroid.widget.TextView;
13.
14.publicclassGameSurfaceViewextendsSurfaceViewimplementsCallback{
15.privateGameThreadgameThread;
16.privateTextViewtextview;
17.
18.publicGameSurfaceView(Contextcontext,AttributeSetattrs){
19.super(context,attrs);
20.SurfaceHolderholder=getHolder();
21.holder.addCallback(this);
22.
23.gameThread=newGameThread(holder,context,newHandler(){
24.
25.@Override
26.publicvoidhandleMessage(Messagem){
27.textview.setText(m.getData().getString("text"));
28.}
29.});
30.
31.//设置可获得焦点,确保能捕获到KeyEvent
32.setFocusable(true);
33.}
34.
35./**
36.*获取一个Activity传来的View协助SurfaceView显示游戏视图,View的具体类型可以根据游戏需要来定
37.*/
38.
39.publicvoidsetTextView(TextViewview){
40.this.textview=view;
41.}
42.
43.publicGameThreadgetThread(){
44.returngameThread;
45.}
46.
47.@Override
48.publicbooleanonKeyDown(intkeyCode,KeyEventevent){
49.returngameThread.doKeyDown(keyCode,event);
50.}
51.
52.@Override
53.publicbooleanonKeyUp(intkeyCode,KeyEventevent){
54.returngameThread.doKeyUp(keyCode,event);
55.}
56.
57./**
58.*当SurfaceView得到或失去焦点时调用,使游戏暂停/恢复运行,
59.*/
60.
61.@Override
62.publicvoidonWindowFocusChanged(booleanhasWindowFocus){
63.if(!
hasWindowFocus){
64.gameThread.pause();
65.}else{
66.gameThread.unpause();
67.}
68.}
69.
70.@Override
71.publicvoidsurfaceChanged(SurfaceHolderholder,intformat,intwidth,intheight){
72.Log.v(this.getClass().getName(),"surfaceChanged()");
73.gameThread.setSurfaceSize(width,height);
74.gameThread.setRunning(true);
75.
76.if(gameThread.isAlive()){
77.Log.v(this.getClass().getName(),"unpausegameThread");
78.gameThread.unpause();
79.}else{
80.Log.v(this.getClass().getName(),"startgameThread");
81.gameThread.start();
82.}
83.
84.}
85.
86.@Override
87.publicvoidsurfaceCreated(SurfaceHolderholder){
88.Log.v(this.getClass().getName(),"surfaceCreated()");
89.}
90.
91./**
92.*为防止surface还会被创建(比如来电)导致gameThread再次启动出现错误,且Activity的onPause方法中已做暂停处理,
93.*这边不对gameThread做处理
94.*@paramholder
95.*/
96.
97.@Override
98.publicvoidsurfaceDestroyed(SurfaceHolderholder){
99.Log.v(this.getClass().getName(),"surfaceDestroyed");
100.}
复制代码
游戏线程类
java代码:
1.packageeoe.demo;
2.
3.importandroid.content.Context;
4.importandroid.graphics.Canvas;
5.importandroid.os.Bundle;
6.importandroid.os.Handler;
7.importandroid.util.Log;
8.importandroid.view.KeyEvent;
9.importandroid.view.SurfaceHolder;
10.
11.publicclassGameThreadextendsThread{
12.//游戏状态值:
ready
13.publicfinalstaticintGS_READY=0;
14.//游戏线程每执行一次需要睡眠的时间
15.privatefinalstaticintDELAY_TIME=100;
16.//上下文,方便获取到应用的各项资源,如图片、音乐、字符串等
17.privateContextcontext;
18.//与Activity其他View交互用的handler
19.privateHandlerhandler;
20.//由SurfaceView提供的SurfaceHolder
21.privateSurfaceHoldersurfaceHolder;
22.//游戏线程运行开关
23.privatebooleanrunning=false;
24.//游戏状态
25.privateintgameState;
26.//当前surface/canvas的高度,在surfaceChanged方法中被设置
27.privateintmCanvasHeight=1;
28.//当前surface/canvas的宽度,在surfaceChanged方法中被设置
29.privateintmCanvasWidth=1;
30.
31./**
32.*游戏是否暂停
33.*/
34.
35.privatebooleanisPaused=false;
36.publicGameThread(SurfaceHolderholder,Contextcontext,Handlerhandler){
37.this.surfaceHolder=holder;
38.this.context=context;
39.this.handler=handler;
40.}
41.
42./**
43.*设置游戏状态
44.*@parammode游戏状态
45.*/
46.
47.publicvoidsetState(intmode){
48.synchronized(surfaceHolder){
49.setState(mode,null);
50.}
51.}
52.
53./**
54.*设置游戏状态
55.*@parammode游戏状态
56.*@parammessage设置游戏状态时的附加文字信息
57.*/
58.
59.publicvoidsetState(intmode,CharSequencemessage){
60.synchronized(surfaceHolder){
61.}
62.
63.}
64.
65./**
66.*暂停游戏
67.*/
68.publicvoidpause(){
69.synchronized(surfaceHolder){
70.isPaused=true;
71.}
72.
73.}
74.
75./**
76.*恢复运行游戏
77.*/
78.publicvoidunpause(){
79.//如果游戏中有时间,别忘记应将其在这里调整到正常
80.synchronized(surfaceHolder){
81.isPaused=false;
82.}
83.}
84.
85./**
86.*当Activity因销毁而被重新创建时,在这里恢复游戏上次运行的数据
87.*@paramsaveStateActivity传来的保存游戏数据的容器
88.*/
89.publicvoidrestoreState(BundlesaveState){
90.}
91.
92./**
93.*在Activity切到后台时保存游戏的数据
94.*@paramoutState保存游戏数据的容器
95.*/
96.
97.publicvoidsaveState(BundleoutState){
98.}
99.
100./**
101.*设置游戏线程运行开关
102.*@paramb开/关
103.*/
104.
105.publicvoidsetRunning(booleanb){
106.running=b;
107.}
108.
109./**
110.*处理按下按键的事件
111.*@paramkeyCode按键事件动作值
112.*@parammsg按键事件对象
113.*@return是否处理完
114.*/
115.publicbooleandoKeyDown(intkeyCode,KeyEventmsg){
116.synchronized(surfaceHolder){
117.returnfalse;
118.}
119.}
120.
121./**
122.*处理弹起按键的事件
123.*@paramkeyCode按键事件动作值
124.*@parammsg按键事件对象
125.*@return是否处理完
126.*/
127.
128.publicbooleandoKeyUp(intkeyCode,KeyEventmsg){
129.synchronized(surfaceHolder){
130.}
131.
132.returnfalse;
133.}
134.
135./**
136.*设置surface/canvas的宽度和高度
137.*@paramwidth由SurfaceHolder传来的宽度
138.*@paramheight由SurfaceHolder传来的高度
139.*/
140.
141.publicvoidsetSurfaceSize(intwidth,intheight){
142.//synchronizedtomakesuretheseallchangeatomically
143.synchronized(surfaceHolder){
144.mCanvasWidth=width;
145.mCanvasHeight=height;
146.//不要忘记每次画布的宽度和高度改变时,在这里对图片等资源做缩放等相关适配屏幕的处理
147.
148.}
149.
150.}
151.
152
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 一个 android游戏框架 转载 android 游戏 框架