【发布时间】:2014-05-17 19:41:14
【问题描述】:
我知道这是个愚蠢的问题,但我不明白为什么会发生这种情况。我是 android 游戏新手,我正在尝试使用 Andengine SDK 创建一个简单的落球场景。为此,我使用 Box2D Extensions Physics 来创建我的球精灵的落球效果。但是它在模拟器上工作正常,但在真实设备上不起作用,球没有落下。这是我的代码:
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 游戏概念。提前谢谢。
【问题讨论】:
标签: android box2d andengine gravity