andengine游戏引擎总结基础篇
来源:互联网 发布:有什么类似知乎的平台 编辑:程序博客网 时间:2024/05/16 15:45
其他的游戏引擎知道的不是很对,不过相对于学java的童鞋们来说,那是个不错的选择啦,这个发动机咋样,google去吧。基础篇包括图片,字体,音效,数据读取,会了这点,就会做简单的小游戏啦
对于游戏开发,也就是把静待的图片动态化,同时加点音效什么的。
1.图片
1) 声名
BitmapTextureAtlas mTexturePlayer
this.mBitmapTextureAtlas = new BitmapTextureAtlas(32, 32, TextureOptions.BILINEAR_PREMULTIPLYALPHA);对于这个类,作用就相当于开辟一个内从空间,以后用来盛具体的图片,所以,开辟大小一定要大于图片像素大小
2)加载资源
分两种,一种是TextureRegion这个加载单个图片,另一种是TiledTextureRegion,加载可以分割的图片
TextureRegion:
private TextureRegion mFaceTextureRegion;
this.mFaceTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "Menu.jpg", 0, 0)
TiledTextureRegion
private TiledTextureRegion mPlayerTextureRegion;
this.mPlayerTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mTexturePlayer, this, "player.png",0, 0, 4, 4);
player.png是图片名,4,4是分割方式4*4分割方式
3)注册资源
this.mEngine.getTextureManager().loadTexture(this.mTextureArm0);
如果不注册,显示的是空白区域
只要申请了资源,就一定要注册,就是使用了BitmapTextureAtlas,就一定要把它注册到engine中
4)使用
也分两种,一种是Sprite ,使用的是TextureRegion加载的图片。
this.backSprite=new Sprite(0, 0, mBackgroundTextureRegion);
另一种是AnimateSprinte,这个具备动画效果。
final AnimatedSprite player = new AnimatedSprite(centerX-200, centerY-100,this.mPlayerTextureRegion);//
具体的动画,调用animate()函数,图片可以使用回调函数,产生复杂的效果
final Sprite sprite = new Sprite(pX, pY, this.armsMap.get(pCard)) {boolean mGrabbed = false;@Overridepublic boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {switch(pSceneTouchEvent.getAction()) {case TouchEvent.ACTION_DOWN:this.setScale(1.25f);this.mGrabbed = true;break;case TouchEvent.ACTION_UP:if(this.mGrabbed) {if(choice>=1){armsEditor.putInt("arm", 1);armsEditor.commit();Intent myintent=new Intent(ChoiceArms.this, MenuGame.class);ChoiceArms.this.startActivity(myintent);System.gc();System.exit(0);}else if(usermoney>10){usermoney-=10;this.mGrabbed = false;this.setScale(1.0f);moneyEditor.putInt("money", usermoney);moneyEditor.commit();armsEditor.putInt("arm", 1);armsEditor.commit();choice=1;armsEditor.putInt("choice", choice);armsEditor.commit();Toast.makeText(ChoiceArms.this, "您购买了光弹", Toast.LENGTH_SHORT).show();Intent myintent=new Intent(ChoiceArms.this, MenuGame.class);ChoiceArms.this.startActivity(myintent);System.gc();System.exit(0);}else{Toast.makeText(ChoiceArms.this, "对不起,金钱不足吆", Toast.LENGTH_SHORT).show();}this.setScale(1.0f);}break;}return true;}};
上边代码实现触摸选择购买子弹,其中涉及如何用xml方式读写数据,会在后续进行讲解
4)加载到场景中
this.mScene.attachChild(sprite);
2字体
同样分三种,声明,加载资源,使用。
1)声明,申请内存资源
BitmapTextureAtlas mStrokeFontTexture;
2)加载字体资源
this.mStrokeFont = new StrokeFont(this.mStrokeFontTexture, Typeface.create(Typeface.DEFAULT, Typeface.BOLD), 32, true, Color.BLUE, 2, Color.YELLOW);
字体类型也很多,可以使用系统默认的,也可以使用加载的,可以是带边框的,也可以是不带的
3)注册到engine中
this.mEngine.getFontManager().loadFont( this.mStrokeFont);
4)字体使用
使用好了会帮你解决不少麻烦
final Text textNormal = new Text(100, 100, this.mFont, "Just some normal Text."); 比如下边的可变字体,还有金币字体等
mCurrBossLive=new ChangeableText(0,0, this.mStrokeFont, "♢♢♢♢♢", "♢♢♢♢♢".length());
5)加载到场景中scene.attachChild(textStroke);
3音效使用
分为长的背景音乐(格式一般为mp3)跟短的音效(如.ogg格式,大小不超过1M)。
1)引擎声明使用 Engine中setNeedsMusic(true).setNeedsSound(true));
Engine engine=new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE,new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT),this.mBoundChaseCamera).setNeedsMusic(true).setNeedsSound(true));
2)加载资源
music=MusicFactory.createMusicFromAsset(getMusicManager(), getApplicationContext(), "BlueWorld.mp3");
3)使用
music.play();
也有重复 music.setLooping(true);,暂停等很多功能,只需要一行代码;
4用xml方式读写数据
1)声明
public static SharedPreferences scores;private SharedPreferences.Editor scoresEditor;
scores=getSharedPreferences("scores", MODE_PRIVATE);scoresEditor=scores.edit();
2)使用
scores.getInt("user0",-1)//读数据,读的是user0中的整形数据,如果找不到,用0代替
scoresEditor.putInt("suer0", count);//将整型变量count中的数据存到user0中 scoresEditor.commit();//一定要提交
//SharedPreferences是用来读的,int float string等等
//SharedPreferences.Editor用来写的,写完后一定要提交
- andengine游戏引擎总结基础篇
- andengine游戏引擎总结进阶篇1
- andengine游戏引擎总结进阶篇2
- 游戏引擎Andengine总结(二):AndEngine引擎运行原理
- android游戏引擎 andengine
- AndEngine游戏引擎简介
- 游戏引擎Andengine总结(五):TexturePacker的使用
- 游戏引擎AndEngine总结(八):自定义Button
- 游戏引擎Andengine总结(一):AndEngine环境配置及extension,AndEngineExamples导入
- 游戏引擎Andengine总结(三):AndEngine与Android的粘合点
- 游戏引擎Andengine总结(一):AndEngine环境配置及extension,AndEngineExamples导入
- AndEngine游戏引擎剖析【一】
- Android游戏引擎--AndEngine入门
- AndEngine游戏引擎之【生命周期】
- AndEngine游戏引擎之【生命周期】
- 游戏引擎AndEngine总结(七):用AndEngine简单实现根据二维数组生成地图(Mappy原理)
- Android游戏引擎-AndEngine(整理)
- Android 游戏引擎AndEngine(加载场景)
- 关于浮点运算 .
- VirtualBox实现内外网络互访问的配置
- 硬币正反的比例
- 路由器的原理和功能
- 单调队列-hdu-4193-Non-negative Partial Sums
- andengine游戏引擎总结基础篇
- HDOJ,杭电 2097, sky数字。。中秋夜的代码又是水的一塌糊涂。
- 二叉树的相关操作总结
- RSA算法原理(一)
- 计算机软件发展史之个人观
- paip.c++ qt 图片处理 检测损坏的图片
- RSA算法原理(二)
- paip.c++ qt 目录遍历以及文件操作
- UVa 558 Wormholes 判断负权环 对比bellman-ford和spfa效率