[英]Box2D Physics with Andengine not working on real devices but works on emulator
我知道這是一個愚蠢的問題,但我無法理解為什么會這樣。我是Android游戲的新手,我試圖使用Andengine SDK創建一個簡單的落球場景。為此,我使用Box2D Extensions Physics創建了落球效果我的球精靈。但是它在模擬器上工作正常,但在真正的devie上卻不工作,Ball並沒有掉下來。這是我的代碼:
package com.example.mygame;
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.scene.background.Background;
import org.andengine.entity.sprite.Sprite;
import org.andengine.extension.physics.box2d.PhysicsConnector;
import org.andengine.extension.physics.box2d.PhysicsFactory;
import org.andengine.extension.physics.box2d.PhysicsWorld;
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.ITextureRegion;
import org.andengine.ui.activity.BaseGameActivity;
import android.hardware.SensorManager;
import android.view.Display;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.FixtureDef;
public class GameActivity extends BaseGameActivity{
Scene scene;
protected static int CAMERA_HEIGHT;
protected static int CAMERA_WIDTH;
BitmapTextureAtlas BallTexture;
ITextureRegion BallTextureRegion;
PhysicsWorld physicsworld;
@SuppressWarnings("deprecation")
@Override
public EngineOptions onCreateEngineOptions() {
final Display display=getWindowManager().getDefaultDisplay();
CAMERA_HEIGHT=display.getHeight();
CAMERA_WIDTH=display.getWidth();
Camera gameCamera=new Camera(0, 0, CAMERA_WIDTH,CAMERA_HEIGHT);
EngineOptions options=new EngineOptions(true,ScreenOrientation.PORTRAIT_FIXED,new RatioResolutionPolicy(CAMERA_WIDTH,CAMERA_HEIGHT),gameCamera);
return options;
}
@Override
public void onCreateResources(OnCreateResourcesCallback pOnCreateResourcesCallback)throws Exception
{
BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
BallTexture=new BitmapTextureAtlas(this.getTextureManager(),256,256,TextureOptions.BILINEAR);
BallTextureRegion=BitmapTextureAtlasTextureRegionFactory.createFromAsset(BallTexture, getAssets(),"ball.png",0,0 );
BallTexture.load();
pOnCreateResourcesCallback.onCreateResourcesFinished();
}
@Override
public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)throws Exception
{
scene=new Scene();
physicsworld=new PhysicsWorld(new Vector2(0,SensorManager.GRAVITY_JUPITER), true);
this.scene.setBackground(new Background(255, 23, 23));
this.scene.registerUpdateHandler(this.physicsworld);
pOnCreateSceneCallback.onCreateSceneFinished(this.scene);
}
@Override
public void onPopulateScene(Scene pScene,OnPopulateSceneCallback pOnPopulateSceneCallback) throws Exception
{
Sprite ball=new Sprite(CAMERA_WIDTH/2,10,BallTextureRegion,this.mEngine.getVertexBufferObjectManager());
final FixtureDef BallFixture=PhysicsFactory.createFixtureDef(10.0f, 1.0f,0.0f);
Body body=PhysicsFactory.createCircleBody(this.physicsworld,ball, BodyType.DynamicBody, BallFixture);
this.scene.attachChild(ball);
physicsworld.registerPhysicsConnector(new PhysicsConnector(ball, body, true, true));
pOnPopulateSceneCallback.onPopulateSceneFinished();
}
}
有人可以幫我嗎?,這樣我就可以以正確的方式學習我的Android游戲概念。
我建議創建Scene
的子類,因為這樣您就可以控制其中的所有內容。 所以在
@Override
public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)throws Exception
{
scene=new PhysicsScene();
}
然后在物理場景中處理所有東西。 這將使手機從您創建的場景類中運行代碼,而不是從主游戲中運行代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.