为什么不能在andengine项目中使用createPhysics方法呢?

时间:2014-04-17 16:23:54

标签: android methods box2d andengine

为什么我不能在GameScene.java类中使用方法createPhysics?

package com.irvan.scene;

import java.io.IOException;

import org.andengine.engine.camera.hud.HUD;
import org.andengine.entity.IEntity;
import org.andengine.entity.modifier.LoopEntityModifier;
import org.andengine.entity.modifier.ScaleModifier;
import org.andengine.entity.scene.IOnSceneTouchListener;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.text.Text;
import org.andengine.entity.text.TextOptions;
import org.andengine.extension.physics.box2d.FixedStepPhysicsWorld;
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.util.SAXUtils;
import org.andengine.util.adt.align.HorizontalAlign;
import org.andengine.util.adt.color.Color;
import org.andengine.util.level.EntityLoader;
import org.andengine.util.level.constants.LevelConstants;
import org.andengine.util.level.simple.SimpleLevelEntityLoaderData;
import org.andengine.util.level.simple.SimpleLevelLoader;
import org.xml.sax.Attributes;

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.BodyDef.BodyType;
import com.irvan.base.BaseScene;
import com.irvan.manager.SceneManager;
import com.irvan.manager.SceneManager.SceneType;

public class GameScene extends BaseScene implements IOnSceneTouchListener{

    private int score = 0;

    private HUD gameHUD;
    private Text scoreText;
    private PhysicsWorld physicsWorld;

    @Override
    public void createScene() {
        createBackground();
        createHUD();
        createPhysics();
    }

    @Override
    public void onBackKeyPressed() {
        SceneManager.getInstance().loadMenuScene(engine);
    }

    @Override
    public SceneType getSceneType() {
        return SceneType.SCENE_GAME;
    }

    @Override
    public void disposeScene() {
        camera.setHUD(null);
        camera.setCenter(400, 240);
    }

    @Override
    public boolean onSceneTouchEvent(Scene arg0, TouchEvent arg1) {
        return false;
    }


    private void createHUD(){
        gameHUD = new HUD();
        camera.setHUD(gameHUD);

        scoreText = new Text(getX(), (getY()+50) * 8, resourcesManager.font, "Score 
            : 0123456789", new TextOptions(HorizontalAlign.LEFT), vbom);
        scoreText.setAnchorCenter(0, 0);
        scoreText.setText("Score : 0");
        gameHUD.attachChild(scoreText);

        camera.setHUD(gameHUD);
    }

    private void createBackground(){
        setBackground(new Background(Color.BLUE));
    }

    private void addToScore(int i){
        score += i;
        scoreText.setText("Score : " + score);
    }

    private void createPhysics(){
        physicsWorld = new FixedStepPhysicsWorld(60, new Vector2(0, -17), false); 
        registerUpdateHandler(physicsWorld);
    }
}

Class FixedStepPhysicsWorld.java

package org.andengine.extension.physics.box2d;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.World;


public class FixedStepPhysicsWorld extends PhysicsWorld {
    // ===========================================================
    // Constants
    // ===========================================================

    public static final int STEPSPERSECOND_DEFAULT = 60;

    // ===========================================================
    // Fields
    // ===========================================================

    private final float mTimeStep;
    private final int mMaximumStepsPerUpdate;
    private float mSecondsElapsedAccumulator;

    // ===========================================================
    // Constructors
    // ===========================================================

    public FixedStepPhysicsWorld(final int pStepsPerSecond, final Vector2 pGravity, final boolean pAllowSleep) {
        this(pStepsPerSecond, Integer.MAX_VALUE, pGravity, pAllowSleep);
    }

    public FixedStepPhysicsWorld(final int pStepsPerSecond, final int pMaximumStepsPerUpdate, final Vector2 pGravity, final boolean pAllowSleep) {
        super(pGravity, pAllowSleep);
        this.mTimeStep = 1.0f / pStepsPerSecond;
        this.mMaximumStepsPerUpdate = pMaximumStepsPerUpdate;
    }

    public FixedStepPhysicsWorld(final int pStepsPerSecond, final Vector2 pGravity, final boolean pAllowSleep, final int pVelocityIterations, final int pPositionIterations) {
        this(pStepsPerSecond, Integer.MAX_VALUE, pGravity, pAllowSleep, pVelocityIterations, pPositionIterations);
    }

    public FixedStepPhysicsWorld(final int pStepsPerSecond, final int pMaximumStepsPerUpdate, final Vector2 pGravity, final boolean pAllowSleep, final int pVelocityIterations, final int pPositionIterations) {
        super(pGravity, pAllowSleep, pVelocityIterations, pPositionIterations);
        this.mTimeStep = 1.0f / pStepsPerSecond;
        this.mMaximumStepsPerUpdate = pMaximumStepsPerUpdate;
    }

    // ===========================================================
    // Getter & Setter
    // ===========================================================

    // ===========================================================
    // Methods for/from SuperClass/Interfaces
    // ===========================================================

    @Override
    public void onUpdate(final float pSecondsElapsed) {
        this.mRunnableHandler.onUpdate(pSecondsElapsed);
        this.mSecondsElapsedAccumulator += pSecondsElapsed;

        final int velocityIterations = this.mVelocityIterations;
        final int positionIterations = this.mPositionIterations;

        final World world = this.mWorld;
        final float stepLength = this.mTimeStep;

        int stepsAllowed = this.mMaximumStepsPerUpdate;

        while (this.mSecondsElapsedAccumulator >= stepLength && stepsAllowed > 0) {
            world.step(stepLength, velocityIterations, positionIterations);
            this.mSecondsElapsedAccumulator -= stepLength;
            stepsAllowed--;
        }

        this.mPhysicsConnectorManager.onUpdate(pSecondsElapsed);
    }

    // ===========================================================
    // Methods
    // ===========================================================

    // ===========================================================
    // Inner and Anonymous Classes
    // ===========================================================
}

Class PhysicsWorld.java

package org.andengine.extension.physics.box2d;

import java.util.Iterator;
import java.util.List;

import org.andengine.engine.handler.IUpdateHandler;
import org.andengine.engine.handler.runnable.RunnableHandler;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.Contact;
import com.badlogic.gdx.physics.box2d.ContactFilter;
import com.badlogic.gdx.physics.box2d.ContactListener;
import com.badlogic.gdx.physics.box2d.DestructionListener;
import com.badlogic.gdx.physics.box2d.Joint;
import com.badlogic.gdx.physics.box2d.JointDef;
import com.badlogic.gdx.physics.box2d.QueryCallback;
import com.badlogic.gdx.physics.box2d.RayCastCallback;
import com.badlogic.gdx.physics.box2d.World;

public class PhysicsWorld implements IUpdateHandler {
    // ===========================================================
    // Constants
    // ===========================================================

    static {
        System.loadLibrary( "andenginephysicsbox2dextension" );
    }

    public static final int VELOCITY_ITERATIONS_DEFAULT = 8;
    public static final int POSITION_ITERATIONS_DEFAULT = 8;

    // ===========================================================
    // Fields
    // ===========================================================

    protected final PhysicsConnectorManager mPhysicsConnectorManager = new PhysicsConnectorManager();
    protected final RunnableHandler mRunnableHandler = new RunnableHandler();
    protected final World mWorld;

    protected int mVelocityIterations = VELOCITY_ITERATIONS_DEFAULT;
    protected int mPositionIterations = POSITION_ITERATIONS_DEFAULT;

    // ===========================================================
    // Constructors
    // ===========================================================

    public PhysicsWorld(final Vector2 pGravity, final boolean pAllowSleep) {
        this(pGravity, pAllowSleep, VELOCITY_ITERATIONS_DEFAULT, POSITION_ITERATIONS_DEFAULT);
    }

    public PhysicsWorld(final Vector2 pGravity, final boolean pAllowSleep, final int pVelocityIterations, final int pPositionIterations) {
        this.mWorld = new World(pGravity, pAllowSleep);
        this.mVelocityIterations = pVelocityIterations;
        this.mPositionIterations = pPositionIterations;
    }

    // ===========================================================
    // Getter & Setter
    // ===========================================================

    //  public World getWorld() {
    //      return this.mWorld;
    //  }

    public int getPositionIterations() {
        return this.mPositionIterations;
    }

    public void setPositionIterations(final int pPositionIterations) {
        this.mPositionIterations = pPositionIterations;
    }

    public int getVelocityIterations() {
        return this.mVelocityIterations;
    }

    public void setVelocityIterations(final int pVelocityIterations) {
        this.mVelocityIterations = pVelocityIterations;
    }

    public PhysicsConnectorManager getPhysicsConnectorManager() {
        return this.mPhysicsConnectorManager;
    }

    public void clearPhysicsConnectors() {
        this.mPhysicsConnectorManager.clear();
    }

    public void registerPhysicsConnector(final PhysicsConnector pPhysicsConnector) {
        this.mPhysicsConnectorManager.add(pPhysicsConnector);
    }

    public void unregisterPhysicsConnector(final PhysicsConnector pPhysicsConnector) {
        this.mPhysicsConnectorManager.remove(pPhysicsConnector);
    }

    // ===========================================================
    // Methods for/from SuperClass/Interfaces
    // ===========================================================

    @Override
    public void onUpdate(final float pSecondsElapsed) {
        this.mRunnableHandler.onUpdate(pSecondsElapsed);
        this.mWorld.step(pSecondsElapsed, this.mVelocityIterations, this.mPositionIterations);
        this.mPhysicsConnectorManager.onUpdate(pSecondsElapsed);
    }

    @Override
    public void reset() {
        // TODO Reset all native physics objects !?!??!
        this.mPhysicsConnectorManager.reset();
        this.mRunnableHandler.reset();
    }

    // ===========================================================
    // Methods
    // ===========================================================

    public void postRunnable(final Runnable pRunnable) {
        this.mRunnableHandler.postRunnable(pRunnable);
    }

    public void clearForces() {
        this.mWorld.clearForces();
    }

    public Body createBody(final BodyDef pDef) {
        return this.mWorld.createBody(pDef);
    }

    public Joint createJoint(final JointDef pDef) {
        return this.mWorld.createJoint(pDef);
    }

    public void destroyBody(final Body pBody) {
        this.mWorld.destroyBody(pBody);
    }

    public void destroyJoint(final Joint pJoint) {
        this.mWorld.destroyJoint(pJoint);
    }

    public void dispose() {
        this.mWorld.dispose();
    }

    public boolean getAutoClearForces() {
        return this.mWorld.getAutoClearForces();
    }

    public Iterator<Body> getBodies() {
        return this.mWorld.getBodies();
    }

    public int getBodyCount() {
        return this.mWorld.getBodyCount();
    }

    public int getContactCount() {
        return this.mWorld.getContactCount();
    }

    public List<Contact> getContactList() {
        return this.mWorld.getContactList();
    }

    public Vector2 getGravity() {
        return this.mWorld.getGravity();
    }

    public Iterator<Joint> getJoints() {
        return this.mWorld.getJoints();
    }

    public int getJointCount() {
        return this.mWorld.getJointCount();
    }

    public int getProxyCount() {
        return this.mWorld.getProxyCount();
    }

    public boolean isLocked() {
        return this.mWorld.isLocked();
    }

    public void QueryAABB(final QueryCallback pCallback, final float pLowerX, final float pLowerY, final float pUpperX, final float pUpperY) {
        this.mWorld.QueryAABB(pCallback, pLowerX, pLowerY, pUpperX, pUpperY);
    }

    public void setAutoClearForces(final boolean pFlag) {
        this.mWorld.setAutoClearForces(pFlag);
    }

    public void setContactFilter(final ContactFilter pFilter) {
        this.mWorld.setContactFilter(pFilter);
    }

    public void setContactListener(final ContactListener pListener) {
        this.mWorld.setContactListener(pListener);
    }

    public void setContinuousPhysics(final boolean pFlag) {
        this.mWorld.setContinuousPhysics(pFlag);
    }

    public void setDestructionListener(final DestructionListener pListener) {
        this.mWorld.setDestructionListener(pListener);
    }

    public void setGravity(final Vector2 pGravity) {
        this.mWorld.setGravity(pGravity);
    }

    public void setWarmStarting(final boolean pFlag) {
        this.mWorld.setWarmStarting(pFlag);
    }

    public void rayCast(final RayCastCallback pRayCastCallback, final Vector2 pPoint1, final Vector2 pPoint2) {
        this.mWorld.rayCast(pRayCastCallback, pPoint1, pPoint2);
    }

    // ===========================================================
    // Methods
    // ===========================================================

    // ===========================================================
    // Inner and Anonymous Classes
    // ===========================================================
}

并且每次编译这个项目,我得到这条消息并在我的设备强制关闭,如果我删除方法createPhysics项目可以运行

error message

0 个答案:

没有答案