简体   繁体   English

使用Andengine的Box2D Physics在实际设备上不起作用,但在模拟器上起作用

[英]Box2D Physics with Andengine not working on real devices but works on emulator

I know this is silly question but I can not getting any reson why its happening.I am new to android gaming and I am trying to create a simple ball falling scene using Andengine SDK.For this I used Box2D Extensions Physics to create ball falling effect for my ball sprite.However Its working fine on emulator but its not working on real devie,Ball is not falling.Here is my code: 我知道这是一个愚蠢的问题,但我无法理解为什么会这样。我是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();
    }
}

Can anyone help me?,so that I can learn my Android gaming concept in a right way.Thanx in advance. 有人可以帮我吗?,这样我就可以以正确的方式学习我的Android游戏概念。

I suggest making a subclass of Scene because then you can controll all the stuff in that. 我建议创建Scene的子类,因为这样您就可以控制其中的所有内容。 so in 所以在

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


    }

Then in the physics scene handle all the stuff . 然后在物理场景中处理所有东西。 This will allows the phone to run the code from the scene class that you made rather than from the main game acitivy. 这将使手机从您创建的场景类中运行代码,而不是从主游戏中运行代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM