andengine遊戲引擎總結基礎篇

 


      其他的遊戲引擎知道的不是很對,不過相對於學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;

			@Override
			public 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用來寫的,寫完後一定要提交



 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章