用法Andengine PhysicsWorld

时间:2013-04-13 15:40:12

标签: android andengine

我正在尝试编写一个简单的andengine游戏,但PhysicsWorld会出错。我在下面的代码中突出显示了双星(**)的错误。我在哪里犯错?

我查找了示例(github,stackoverflow,code.google.com等),但我找不到任何错误。我是从今天早上开始尝试的。

package com.k.hockeygame;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.FillResolutionPolicy;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.shape.Shape;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.entity.util.FPSLogger;
import org.anddev.andengine.extension.physics.box2d.PhysicsConnector;
import org.anddev.andengine.extension.physics.box2d.PhysicsFactory;
import org.anddev.andengine.extension.physics.box2d.PhysicsWorld;
import org.anddev.andengine.opengl.texture.Texture;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.ui.activity.BaseGameActivity;
import android.hardware.SensorManager;
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 HockeyGame extends BaseGameActivity {
private static final int CAMERA_WIDTH = 800;
private static final int CAMERA_HEIGHT = 480;
private Camera camera;
private Engine engine;
**private PhysicsWorld physicsWorld = new PhysicsWorld(new Vector2(0, SensorManager.GRAVITY_DEATH_STAR_I), false);
**private FixtureDef fixDef = PhysicsFactory.createFixtureDef(0.5f, 0.5f, 0.5f);
Scene scene;
private BitmapTextureAtlas texField, texPlayer1, texPlayer2;
private TextureRegion texRegField, texRegPlayer1, texRegPlayer2;
private Sprite spriteField, spritePlayer1, spritePlayer2;
**private Body bodyPlayer1, bodyPlayer2;

@Override
public Engine onLoadEngine() {
    camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
    final EngineOptions engineOptions = new EngineOptions(true,ScreenOrientation.LANDSCAPE, new FillResolutionPolicy(), camera);
    engineOptions.getTouchOptions().setRunOnUpdateThread(true);
    engine = new Engine(engineOptions);

    return engine;
}

@Override
public void onLoadResources() {
    texField = new BitmapTextureAtlas(1024, 512, TextureOptions.BILINEAR_PREMULTIPLYALPHA);

    texPlayer1 = new BitmapTextureAtlas(128, 128, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
    texPlayer2 = new BitmapTextureAtlas(128, 128, TextureOptions.BILINEAR_PREMULTIPLYALPHA);

    BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");

    texRegField = BitmapTextureAtlasTextureRegionFactory.createFromAsset(texField, this, "background.png", 0, 0);
    texRegPlayer1 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(texPlayer1, this, "a.png", 0, 0);
    texRegPlayer2 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(texPlayer2, this, "b.png", 0, 0);

    Texture [] textures = {texField, texPlayer1, texPlayer2};
    mEngine.getTextureManager().loadTextures(textures);
}
private float fingerCoorX = 0;
private float fingerCoorY = 0;
private boolean isMoveBlue = false;
@Override
public Scene onLoadScene() {
    this.engine.registerUpdateHandler(new FPSLogger());
    this.scene = new Scene();

    spriteField = new Sprite(0, 0, texRegField);

    spritePlayer1 = new Sprite(600, CAMERA_HEIGHT / 2 - 128, texRegPlayer1);

    spritePlayer2 = new Sprite(50, CAMERA_HEIGHT / 2, texRegPlayer2);

    **bodyPlayer1 = PhysicsFactory.createCircleBody(this.physicsWorld, (Shape)spritePlayer1, BodyType.DynamicBody, fixDef);
    **bodyPlayer2 = PhysicsFactory.createCircleBody(this.physicsWorld, (Shape)spritePlayer2, BodyType.DynamicBody, fixDef);

    **this.physicsWorld.registerPhysicsConnector(new PhysicsConnector(spritePlayer1, bodyPlayer1));
    **this.physicsWorld.registerPhysicsConnector(new PhysicsConnector(spritePlayer2, bodyPlayer2));

    **bodyPlayer1.setLinearVelocity(new Vector2(10, 0));
    **bodyPlayer2.setLinearVelocity(new Vector2(10, 0));

    this.scene.attachChild(spriteField);
    this.scene.attachChild(spritePlayer1);
    this.scene.attachChild(spritePlayer2);

    this.scene.registerTouchArea(spritePlayer1);
    this.scene.registerTouchArea(spritePlayer2);

    **this.scene.registerUpdateHandler(physicsWorld);

    return this.scene;
}

@Override
public void onLoadComplete() {

}
}

这是logcat。

04-13 19:10:39.865: D/AndEngine(17573):     at org.anddev.andengine.engine.Engine$UpdateThread.run(Engine.java:685)
04-13 19:10:40.279: D/AndroidRuntime(17573): Shutting down VM
04-13 19:10:40.279: W/dalvikvm(17573): threadid=1: thread exiting with uncaught exception (group=0x40af7930)
04-13 19:10:40.294: E/AndroidRuntime(17573): FATAL EXCEPTION: main
04-13 19:10:40.294: E/AndroidRuntime(17573): java.lang.NoSuchMethodError: org.anddev.andengine.entity.shape.Shape.getVelocityX
04-13 19:10:40.294: E/AndroidRuntime(17573):    at org.anddev.andengine.extension.physics.box2d.PhysicsFactory.createCircleBody(PhysicsFactory.java:118)
04-13 19:10:40.294: E/AndroidRuntime(17573):    at org.anddev.andengine.extension.physics.box2d.PhysicsFactory.createCircleBody(PhysicsFactory.java:105)
04-13 19:10:40.294: E/AndroidRuntime(17573):    at com.k.hockeygame.HockeyGame.onLoadScene(HockeyGame.java:80)
04-13 19:10:40.294: E/AndroidRuntime(17573):    at org.anddev.andengine.ui.activity.BaseGameActivity.doResume(BaseGameActivity.java:161)
04-13 19:10:40.294: E/AndroidRuntime(17573):    at org.anddev.andengine.ui.activity.BaseGameActivity.onWindowFocusChanged(BaseGameActivity.java:85)
04-13 19:10:40.294: E/AndroidRuntime(17573):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onWindowFocusChanged(PhoneWindow.java:2451)
04-13 19:10:40.294: E/AndroidRuntime(17573):    at android.view.View.dispatchWindowFocusChanged(View.java:7440)
04-13 19:10:40.294: E/AndroidRuntime(17573):    at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:930)
04-13 19:10:40.294: E/AndroidRuntime(17573):    at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:2927)
04-13 19:10:40.294: E/AndroidRuntime(17573):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-13 19:10:40.294: E/AndroidRuntime(17573):    at android.os.Looper.loop(Looper.java:137)
04-13 19:10:40.294: E/AndroidRuntime(17573):    at android.app.ActivityThread.main(ActivityThread.java:5226)
04-13 19:10:40.294: E/AndroidRuntime(17573):    at java.lang.reflect.Method.invokeNative(Native Method)
04-13 19:10:40.294: E/AndroidRuntime(17573):    at java.lang.reflect.Method.invoke(Method.java:511)
04-13 19:10:40.294: E/AndroidRuntime(17573):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
4-13 19:10:40.294: E/AndroidRuntime(17573):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
04-13 19:10:40.294: E/AndroidRuntime(17573):    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

'java.lang.NoSuchMethodError'使它看起来像版本不匹配 - 检查库是否在正确的位置,并且您没有将它引用到另一个位置。