创建一个PhysicsWorld给了我黑屏

时间:2013-04-19 16:34:55

标签: android box2d andengine

我已经开始学习andegine并且我对box2d扩展有一些问题。 我有这个代码,它给了我黑屏。当我删除使PhysicsWorld运作时。

package com.cupofcoffee.heartjump;
import org.andengine.engine.camera.Camera;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.FillResolutionPolicy;
import org.andengine.entity.primitive.Rectangle;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.util.FPSCounter;
import org.andengine.extension.physics.box2d.PhysicsWorld;
import org.andengine.ui.activity.SimpleBaseGameActivity;
import org.andengine.util.color.Color;

import android.hardware.SensorManager;

import com.badlogic.gdx.math.Vector2;

public class MainActivity extends SimpleBaseGameActivity {

    static final int CAMERA_WIDTH = 800;
    static final int CAMERA_HEIGHT = 480;
    Camera camera;
    Scene gameScene;

    Heart heart;
    PhysicsWorld physicsWorld;

    @Override
    public EngineOptions onCreateEngineOptions() {
        this.camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);

        EngineOptions engineOptions = new EngineOptions(true,
                ScreenOrientation.LANDSCAPE_FIXED, new FillResolutionPolicy(),
                camera);
        return engineOptions;
    }

    @Override
    protected void onCreateResources() {

    }

    @Override
    protected Scene onCreateScene() {
        mEngine.registerUpdateHandler(new FPSCounter());
        gameScene = new Scene();
        gameScene.setBackground(new Background(1, 1, 1));

        physicsWorld = new PhysicsWorld(new Vector2(0, SensorManager.GRAVITY_EARTH), false);
//      heart = new Heart(500, 100, this, getVertexBufferObjectManager(), physicsWorld);

//      gameScene.registerUpdateHandler(physicsWorld);

//      gameScene.attachChild(heart.sprite);

        Rectangle r = new Rectangle(100, 100, 100, 100,
                getVertexBufferObjectManager());
        r.setColor(new Color(50, 50, 50));
        gameScene.attachChild(r);
        return gameScene;
    }

}

这有什么问题?我真的找不到解决方案。

2 个答案:

答案 0 :(得分:0)

我认为这是无效的:
     新颜色(50,50,50)
因为每个参数的有效范围是0-1,而不是0-255

另外,试试:
     gameScene.setBackground(new Background(Color.RED));

答案 1 :(得分:0)

你可以设置颜色背景。

gameScene.setBackground(new ColorBackground(1, 1, 1));

或者如果设置了任何图像

gameScene.setBackground(new SpriteBackground(yourSprite));