Android Media and Animation 9

สร้าง project ใหม่ MyTower วาง background รูปภาพ และเอาเสามาวาง 3 เสา และเอาห่วงมาใส่

Download
andengine.org
download
หน้า GitBub - กด Download Zip
Unzip
เข้าโปรแกรม android
File-Import-General-Exisitng Project workspace
Next-finish

มันจะสร้าง Andengin_Tower
เลือก properties-android-add

สร้างที่ src
New class
Java class
finish

--------------

Picture 2014-03-08 15_34_52

TowerActivity.java
package com.example.andengine_tower;

import java.io.IOException;
import java.io.InputStream;

import org.andengine.engine.camera.Camera;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.sprite.Sprite;
import org.andengine.opengl.texture.ITexture;
import org.andengine.opengl.texture.Texture;
import org.andengine.opengl.texture.bitmap.BitmapTexture;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.texture.region.TextureRegionFactory;
import org.andengine.ui.activity.SimpleBaseGameActivity;
import org.andengine.util.adt.io.in.IInputStreamOpener;

public class TowerActivity extends SimpleBaseGameActivity {
//STEP1
private static int CAMERA_WIDTH=800;
private static int CAMERA_HEIGHT=480;
//step5
private ITextureRegion mBackgroundTextureRegion;

@Override
public EngineOptions onCreateEngineOptions() {
//STEP2
final Camera camera = new Camera(0,0, CAMERA_WIDTH,CAMERA_HEIGHT);
return new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED,
new RatioResolutionPolicy(CAMERA_WIDTH,CAMERA_HEIGHT),camera);
//step3
//copy image file to assets folder
}

@Override
protected void onCreateResources() {
//step4
try {
ITexture backgroundTexture=new BitmapTexture(
this.getTextureManager(),new IInputStreamOpener() {

@Override
public InputStream open() throws IOException {
return getAssets().open("gfx/background.png");
}
});
backgroundTexture.load();
//step6
mBackgroundTextureRegion=TextureRegionFactory.extractFromTexture(backgroundTexture);
}
catch (IOException e) {
e.printStackTrace();
}
}
public InputStream open() throws IOException {
// TODO Auto-generated method stub
return null;
}

@Override
protected Scene onCreateScene() {
//step7
final Scene scene=new Scene();
Sprite backgroundSprite=new Sprite(0,0,
mBackgroundTextureRegion, getVertexBufferObjectManager());
scene.attachChild(backgroundSprite);
return scene;
}
}

---------

Picture 2014-03-08 15_35_08

Picture 2014-03-08 15_39_03

 

เพิ่มเสา 3 ต้นลงในภาพ

TowerActivity.java
package com.example.andengine_tower;

import java.io.IOException;
import java.io.InputStream;

import org.andengine.engine.camera.Camera;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.sprite.Sprite;
import org.andengine.opengl.texture.ITexture;
import org.andengine.opengl.texture.Texture;
import org.andengine.opengl.texture.bitmap.BitmapTexture;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.texture.region.TextureRegionFactory;
import org.andengine.ui.activity.SimpleBaseGameActivity;
import org.andengine.util.adt.io.in.IInputStreamOpener;

public class TowerActivity extends SimpleBaseGameActivity {
//STEP1
private static int CAMERA_WIDTH=800;
private static int CAMERA_HEIGHT=480;
//step5
private ITextureRegion mBackgroundTextureRegion,mTowerITextureRegion;

@Override
public EngineOptions onCreateEngineOptions() {
//STEP2
final Camera camera = new Camera(0,0, CAMERA_WIDTH,CAMERA_HEIGHT);
return new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED,
new RatioResolutionPolicy(CAMERA_WIDTH,CAMERA_HEIGHT),camera);
//step3
//copy image file to assets folder
}

@Override
protected void onCreateResources() {
//step4
try {
ITexture backgroundTexture=new BitmapTexture(
this.getTextureManager(),new IInputStreamOpener() {

@Override
public InputStream open() throws IOException {
return getAssets().open("gfx/background.png");
}
});
//Load tower image
ITexture towerTexture=new BitmapTexture(
this.getTextureManager(),new IInputStreamOpener() {

@Override
public InputStream open() throws IOException {
return getAssets().open("gfx/tower.png");
}
});
backgroundTexture.load();
towerTexture.load();

//step6
mBackgroundTextureRegion=TextureRegionFactory.extractFromTexture(
backgroundTexture);
mTowerITextureRegion=TextureRegionFactory.extractFromTexture(towerTexture);
}
catch (IOException e) {
e.printStackTrace();
}
}

private Sprite mTower1,mTower2,mTower3;
@Override
protected Scene onCreateScene() {
//step7
final Scene scene=new Scene();
Sprite backgroundSprite=new Sprite(0,0,
mBackgroundTextureRegion, getVertexBufferObjectManager());
scene.attachChild(backgroundSprite);
mTower1=new Sprite(192,63, mTowerITextureRegion,
getVertexBufferObjectManager());

mTower2=new Sprite(400,63, mTowerITextureRegion,
getVertexBufferObjectManager());

mTower3=new Sprite(604,63, mTowerITextureRegion,
getVertexBufferObjectManager());

scene.attachChild(mTower1); ใส่เสาต้นที่ 1
scene.attachChild(mTower2); ใส่เสาต้นที่ 1
scene.attachChild(mTower3); ใส่เสาต้นที่ 1
return scene;
}
}

Picture 2014-03-08 15_44_52