今回はDigitalOnScreenControlでプレイヤーを移動することをします。
概要
・physics worldを作成
・プレイヤーを物理シミュレーション上で動くようにBodyを作成
・DigitalOnScreenControlの操作でプレイヤーを動かす
public Scene onLoadScene() {
this.mEngine.registerUpdateHandler(new FPSLogger());
// Create physics world
this.mPhysicsWorld = new FixedStepPhysicsWorld(30, new Vector2(0, 0), false, 3, 2);
//Create the scene and register the physics world
mScene = new Scene();
mScene.registerUpdateHandler(this.mPhysicsWorld);
//TMX mapの読み込み
try {
final TMXLoader tmxLoader = new TMXLoader(this, this.mEngine.getTextureManager(), // TextureOptions.BILINEAR_PREMULTIPLYALPHA,
TextureOptions.NEAREST);
this.mTMXTiledMap = tmxLoader.loadFromAsset(this, "tmx/ground1.tmx");
} catch (final TMXLoadException tmxle) {
Debug.e(tmxle);
}
// レイヤーをSceneに結びつける
for (int i = 0; i < this.mTMXTiledMap.getTMXLayers().size(); i++){
TMXLayer layer = this.mTMXTiledMap.getTMXLayers().get(i);
mScene.attachChild(layer);
}
// カメラがTMXLayerの境界を超えないようにする
final TMXLayer tmxLayer = this.mTMXTiledMap.getTMXLayers().get(0);
this.mBoundChaseCamera.setBounds(0, tmxLayer.getWidth(), 0, tmxLayer.getHeight());
this.mBoundChaseCamera.setBoundsEnabled(true);
//プレイヤーの表示
final int centerX = (CAMERA_WIDTH - this.mPlayerTextureRegion.getTileWidth()) / 2;
final int centerY = (CAMERA_HEIGHT - this.mPlayerTextureRegion.getTileHeight()) / 2;
final AnimatedSprite player = new AnimatedSprite(centerX, centerY, this.mPlayerTextureRegion);
this.mBoundChaseCamera.setChaseEntity(player);
final FixtureDef playerFixtureDef = PhysicsFactory.createFixtureDef(0, 0, 0.5f);
final Body mPlayerBody = PhysicsFactory.createBoxBody(this.mPhysicsWorld, player,
BodyType.DynamicBody, playerFixtureDef);
this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(player,
mPlayerBody, true, false){
@Override
public void onUpdate(float pSecondsElapsed){
super.onUpdate(pSecondsElapsed);
mBoundChaseCamera.updateChaseEntity();
}
});
mScene.attachChild(player);
//プレイヤーのコントロール
this.mDigitalOnScreenControl = new DigitalOnScreenControl(0,
CAMERA_HEIGHT - this.mOnScreenControlBaseTextureRegion.getHeight(),
this.mBoundChaseCamera,
this.mOnScreenControlBaseTextureRegion,
this.mOnScreenControlKnobTextureRegion, 0.1f,
new IOnScreenControlListener() {
public void onControlChange(final BaseOnScreenControl pBaseOnScreenControl,
final float pValueX, final float pValueY) {
// Set the correct walking animation
if (pValueY == 1){
// Up
if (playerDirection != PlayerDirection.UP){
player.animate(ANIMATE_DURATION, 0, 2, true);
playerDirection = PlayerDirection.UP;
}
}else if (pValueY == -1){
// Down
if (playerDirection != PlayerDirection.DOWN){
player.animate(ANIMATE_DURATION, 9, 11, true);
playerDirection = PlayerDirection.DOWN;
}
}else if (pValueX == -1){
// Left
if (playerDirection != PlayerDirection.LEFT){
player.animate(ANIMATE_DURATION, 3, 5, true);
playerDirection = PlayerDirection.LEFT;
}
}else if (pValueX == 1){
// Right
if (playerDirection != PlayerDirection.RIGHT){
player.animate(ANIMATE_DURATION, 6, 8, true);
playerDirection = PlayerDirection.RIGHT;
}
}else{
if (player.isAnimationRunning()){
player.stopAnimation();
playerDirection = PlayerDirection.NONE;
}
}
// Set the player's velocity
mPlayerBody.setLinearVelocity(pValueX * PLAYER_VELOCITY, pValueY * PLAYER_VELOCITY);
}
});
this.mDigitalOnScreenControl.getControlBase().setBlendFunction(GL10.GL_SRC_ALPHA,
GL10.GL_ONE_MINUS_SRC_ALPHA);
this.mDigitalOnScreenControl.getControlBase().setAlpha(0.5f);
this.mDigitalOnScreenControl.getControlBase().setScaleCenter(0, 128);
this.mDigitalOnScreenControl.getControlBase().setScale(1.25f);
this.mDigitalOnScreenControl.getControlKnob().setScale(1.25f);
this.mDigitalOnScreenControl.getControlKnob().setAlpha(0.5f);
this.mDigitalOnScreenControl.refreshControlKnobPosition();
mScene.setChildScene(this.mDigitalOnScreenControl);
return mScene;
}
解説
前回のコードに追記する形でonLoadScene()メソッド内のコードを載せています。
5行目でphysics worldを作成しています。
これを9行目でSceneに対UpdateHandlerとして登録しています。
physics worldについては以下を参考にしてください。
AndEnginePhysicsBox2DExtension::Creating A Physicsworld
38から49行目でBodyを作成しています。
93行目でBodyに対して速度を設定しています。
以上でプレイヤーはDigitalOnScreenControlの操作で移動することができます。
0 件のコメント:
コメントを投稿