Andengine+Box2D を使用した小さなプロジェクトに取り組んでいます。プレイヤー キャラクターと地面があります。地面は StaticBody で、プレイヤーは DynamicBody です。人が地面を歩いているような効果が欲しいです。
問題は、プレーヤーを動かすと地面に沈むことです.どうすれば修正できますか?写真で説明する必要があります.
これが私のコードです:
import org.andengine.engine.camera.Camera;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.FillResolutionPolicy;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.util.FPSLogger;
import org.andengine.extension.physics.box2d.PhysicsConnector;
import org.andengine.extension.physics.box2d.PhysicsFactory;
import org.andengine.extension.physics.box2d.PhysicsWorld;
import org.andengine.input.touch.TouchEvent;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.TextureRegion;
import org.andengine.ui.activity.SimpleBaseGameActivity;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.MassData;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.app.Activity;
import android.view.Gravity;
import android.view.Menu;
public class BlokKirmaActivity extends SimpleBaseGameActivity {
private EngineOptions engineOptions;
private Camera camera;
private Scene oyunSahne;
private static final int CAMERA_HEIGHT = 480;
private static final int CAMERA_WIDTH = 800;
private PhysicsWorld physicsWorld= new PhysicsWorld(new Vector2(0,SensorManager.GRAVITY_EARTH),false);
private FixtureDef fixDef = PhysicsFactory.createFixtureDef(1.0f, 0.0f, 1.0f);
private BitmapTextureAtlas texYer,texKamil,texSol,texSag,texZipla;
private TextureRegion texRegYer,texRegKamil,texRegSol,texRegSag,texRegZipla;
private Sprite spriteYer,spriteKamil,spriteSol,spriteSag,spriteZipla;
private Body bodyYer,bodyKamil;
private boolean solBasilimi=false,sagBasilimi=false,ziplaBasilimi=false;
public EngineOptions onCreateEngineOptions() {
camera = new Camera(0,0,CAMERA_WIDTH,CAMERA_HEIGHT);
engineOptions = new EngineOptions(true,ScreenOrientation.LANDSCAPE_FIXED, new FillResolutionPolicy(),camera);
engineOptions.getTouchOptions().setNeedsMultiTouch(true);
engineOptions.getAudioOptions().setNeedsSound(true);
return this.engineOptions;
}
@Override
protected void onCreateResources() {
texYer = new BitmapTextureAtlas(this.getTextureManager(),1024,256,TextureOptions.BILINEAR_PREMULTIPLYALPHA);
texKamil = new BitmapTextureAtlas(this.getTextureManager(),64,128,TextureOptions.BILINEAR_PREMULTIPLYALPHA);
texSol = new BitmapTextureAtlas(this.getTextureManager(),128,128,TextureOptions.BILINEAR_PREMULTIPLYALPHA);
texSag = new BitmapTextureAtlas(this.getTextureManager(),128,128,TextureOptions.BILINEAR_PREMULTIPLYALPHA);
texZipla = new BitmapTextureAtlas(this.getTextureManager(),128,128,TextureOptions.BILINEAR_PREMULTIPLYALPHA);
texYer.load();
texKamil.load();
texSol.load();
texSag.load();
texZipla.load();
texRegYer = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.texYer, this, "gfx/oyunyer.png",0,0);
texRegKamil = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.texKamil, this, "gfx/kamil.png",0,0);
texRegSol = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.texSol, this, "gfx/sol.png",0,0);
texRegSag = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.texSag, this, "gfx/sag.png",0,0);
texRegZipla = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.texZipla, this, "gfx/zipla.png",0,0);
}
@Override
protected Scene onCreateScene() {
this.mEngine.registerUpdateHandler(new FPSLogger());
this.oyunSahne = new Scene();
this.oyunSahne.registerUpdateHandler(physicsWorld);
spriteYer = new Sprite(0,355,texRegYer,getVertexBufferObjectManager());
spriteKamil = new Sprite(220,50,texRegKamil,getVertexBufferObjectManager()){
protected void onManagedUpdate(float pSecondsElapsed){
if(solBasilimi==true){
bodyKamil.setLinearVelocity(-10, 0);
}
if(sagBasilimi==true){
bodyKamil.setLinearVelocity(10, 0);
}
}
};
spriteSol = new Sprite(100,50,texRegSol,getVertexBufferObjectManager()){
public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
if(pSceneTouchEvent.isActionDown()){
solBasilimi=true;
}
if(pSceneTouchEvent.isActionUp()){
solBasilimi=false;
}
return true;
}
};
spriteSag = new Sprite(300,50,texRegSag,getVertexBufferObjectManager()){
public boolean onAreaTouched(TouchEvent pSceneTouchEvent,float pTouchAreaLocalX, float pTouchAreaLocalY) {
if(pSceneTouchEvent.isActionDown()){
sagBasilimi=true;
}
if(pSceneTouchEvent.isActionUp()){
sagBasilimi=false;
}
return true;
}
};
spriteZipla = new Sprite(600,50,texRegZipla,getVertexBufferObjectManager()){
public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
if(pSceneTouchEvent.isActionDown()){
bodyKamil.setLinearVelocity(0,-20);
}
if(pSceneTouchEvent.isActionUp()){
bodyKamil.setLinearVelocity(0,0);
}
return true;
}
};
bodyYer = PhysicsFactory.createBoxBody(physicsWorld, spriteYer, BodyType.StaticBody, fixDef);
this.physicsWorld.registerPhysicsConnector(new PhysicsConnector(spriteYer, bodyYer,true,false));
bodyKamil = PhysicsFactory.createBoxBody(physicsWorld, spriteKamil, BodyType.DynamicBody, fixDef);
this.physicsWorld.registerPhysicsConnector(new PhysicsConnector(spriteKamil, bodyKamil,true,false));
MassData massData1 = bodyKamil.getMassData();
massData1.mass= 75;
bodyKamil.setMassData(massData1);
this.oyunSahne.attachChild(spriteYer);
this.oyunSahne.attachChild(spriteKamil);
this.oyunSahne.attachChild(spriteSol);
this.oyunSahne.attachChild(spriteSag);
this.oyunSahne.attachChild(spriteZipla);
this.oyunSahne.registerTouchArea(spriteSol);
this.oyunSahne.registerTouchArea(spriteSag);
this.oyunSahne.registerTouchArea(spriteZipla);
return this.oyunSahne;
}
}