带有Andengine的Box2D Physics不在真实设备上工作但在仿真器上工作

时间:2014-05-17 19:41:14

标签: android box2d andengine gravity

我知道这是一个愚蠢的问题,但我不知道为什么会发生这种情况。我是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游戏概念。提前做好准备。

1 个答案:

答案 0 :(得分:1)

我建议创建Scene的子类,因为那时你可以控制其中的所有内容。所以在

  @Override
    public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)throws Exception 
    {
        scene=new PhysicsScene();  


    }

然后在物理场景中处理所有的东西。这将允许手机运行您所创建的场景类的代码,而不是主游戏活动。