重力和加速度计不能正常工作Box2D andenigne

时间:2014-05-19 18:30:18

标签: android box2d andengine game-development

我是安卓游戏中的新手,在4天的头脑风暴中我陷入了一些愚蠢的境地。我正在制作一个游戏,其中球需要从两个移动的红色条之间的顶部掉落。有一些重力适用于球,球需要在加速度计的帮助下左右移动(通过倾斜手机)enter image description here

如图所示,我想在涂鸦跳跃游戏中加速涂鸦移动时移动球,但重力不允许我的代码这样做,直到球在重力速度下降。此外我能够显示一个Redbar,它是可移动的,但第二个RedBar没有显示。 任何人都可以告诉我,我的代码发生了什么问题?由于缺乏Andengine的知识,我无法理解。提前做了很多事。

此处代码:

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.modifier.MoveXModifier;
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.extension.physics.box2d.util.Vector2Pool;
import org.andengine.input.sensor.acceleration.AccelerationData;
import org.andengine.input.sensor.acceleration.IAccelerationListener;
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 implements IAccelerationListener{

    Scene scene;
    protected static int CAMERA_HEIGHT;
    protected static int CAMERA_WIDTH;
    BitmapTextureAtlas  BallTexture;
    ITextureRegion BallTextureRegion;
    BitmapTextureAtlas RedBarTexture;
    ITextureRegion RedBarRegion;
    Sprite RedBar,RedBar2;
    PhysicsWorld physicsworld;
    Sprite ball;
    Body body;

    Body RedBarBody;
    int gap;
    Body RedBarBody2;
    @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 );
        RedBarTexture=new BitmapTextureAtlas(this.getTextureManager(), 16, 128);
        RedBarRegion=BitmapTextureAtlasTextureRegionFactory.createFromAsset(RedBarTexture, getAssets(), "red.png",0,0);
        BallTexture.load();
        RedBarTexture.load();
        this.enableAccelerationSensor(this);
        pOnCreateResourcesCallback.onCreateResourcesFinished();
    }

    @Override
    public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)throws Exception 
    {
        scene=new Scene();  
        physicsworld=new PhysicsWorld(new Vector2(0,SensorManager.GRAVITY_EARTH), 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
    {
        ball=new Sprite(CAMERA_WIDTH/2,10,BallTextureRegion,this.mEngine.getVertexBufferObjectManager());
        RedBar=new Sprite(CAMERA_WIDTH-14,CAMERA_HEIGHT-77,RedBarRegion,this.mEngine.getVertexBufferObjectManager());
        RedBar2=new Sprite((RedBar.getX()+70),CAMERA_HEIGHT-77,RedBarRegion,this.mEngine.getVertexBufferObjectManager());
        final FixtureDef BallFixture=PhysicsFactory.createFixtureDef(10.0f, 0.2f,0.0f);
        final FixtureDef BarFixture=PhysicsFactory.createFixtureDef(0.0f,0.0f,0.0f);
        body=PhysicsFactory.createCircleBody(this.physicsworld,ball, BodyType.DynamicBody, BallFixture);
        RedBarBody=PhysicsFactory.createBoxBody(physicsworld, RedBar, BodyType.StaticBody,BarFixture);
        RedBarBody2=PhysicsFactory.createBoxBody(physicsworld, RedBar2, BodyType.StaticBody,BarFixture);
        MoveXModifier movex=new MoveXModifier(18.0f, CAMERA_WIDTH-14, 0);
        RedBar.registerEntityModifier(movex);
        RedBar2.registerEntityModifier(movex);
        this.scene.attachChild(ball);
        this.scene.attachChild(RedBar);
        this.scene.attachChild(RedBar2);
        physicsworld.registerPhysicsConnector(new PhysicsConnector(ball, body, true, true));

        pOnPopulateSceneCallback.onPopulateSceneFinished();
    }



    @Override
    public void onAccelerationChanged(AccelerationData pAccelerationData) {
        // TODO Auto-generated method stub
        body.applyForce(new Vector2(pAccelerationData.getX(), 0),body.getWorldCenter());
    }

    @Override
    public void onAccelerationAccuracyChanged(AccelerationData pAccelerationData) {
        // TODO Auto-generated method stub


    }
}

0 个答案:

没有答案