Andengine Sprites显示为黑盒子

时间:2012-04-09 18:35:44

标签: android andengine sprite

出于某种原因,我的所有精灵都开始出现在黑盒子之前。这是我的代码。我只使用前2个精灵运行良好,但当我添加其他精灵时,他们在我的Android设备上变成了黑盒子。任何帮助将不胜感激。

public class GameHabeebActivity extends SimpleBaseGameActivity implements IAccelerationListener, IOnSceneTouchListener, IOnAreaTouchListener {
    // ===========================================================
    // Constants
    // ===========================================================



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

    private BitmapTextureAtlas mBitmapTextureAtlas;

    private TextureRegion mBoxFaceTextureRegion;
    private TextureRegion mCircleFaceTextureRegion;
    private TextureRegion mBaseballTextureRegion;
    private TextureRegion mTomatoeTextureRegion;
    private TextureRegion mBatTextureRegion;
    private TextureRegion mCanTextureRegion;
    private TextureRegion mBottleTextureRegion;

    private int mFaceCount = 0;

    private PhysicsWorld mPhysicsWorld;

    private float mGravityX;
    private float mGravityY;

    private int CAMERA_WIDTH;
    private int CAMERA_HEIGHT;

    private Scene mScene;

    private int itemscale=2;
    private int whichface=0;
    // ===========================================================
    // Constructors
    // ===========================================================

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

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

    @Override
    public EngineOptions onCreateEngineOptions() {
        Toast.makeText(this, "! HIT THE OBJECTS TO KEEP THEM IN THE AIR !", Toast.LENGTH_LONG).show();

        final Display display = getWindowManager().getDefaultDisplay();

        CAMERA_WIDTH = display.getWidth();
        CAMERA_HEIGHT = display.getHeight();

        final Camera camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);

        return new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), camera);
    }

    @Override
    public void onCreateResources() {
        BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");

        this.mBitmapTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), 600, 170, TextureOptions.BILINEAR);
        this.mBoxFaceTextureRegion =  (TextureRegion) BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "face_box_tiled.png", 0, 0); // 64x32
        this.mCircleFaceTextureRegion = (TextureRegion) BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "face_circle_tiled.png", 64, 0); // 64x32
        this.mBaseballTextureRegion= (TextureRegion) BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "Baseball.png", 128, 0);//50x50
        this.mTomatoeTextureRegion= (TextureRegion) BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "tomatoe.png", 178, 0);//50x46
        this.mBatTextureRegion= (TextureRegion) BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "Baseball_bat.png", 228, 0);//261x36
        this.mCanTextureRegion= (TextureRegion) BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "can.png", 489, 0);//50x83
        this.mBottleTextureRegion= (TextureRegion) BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "bottle.png", 539, 0);//50x170
    }

    @Override
    public Scene onCreateScene() {
        this.mEngine.registerUpdateHandler(new FPSLogger());

        this.mPhysicsWorld = new PhysicsWorld(new Vector2(0, SensorManager.GRAVITY_EARTH), false);

        this.mScene = new Scene();
        this.mScene.setBackground(new Background(1, 1, 1));
        this.mScene.setOnSceneTouchListener(this);

        final VertexBufferObjectManager vertexBufferObjectManager = this.getVertexBufferObjectManager();
        final Rectangle roof = new Rectangle(0, 0, CAMERA_WIDTH, 2, vertexBufferObjectManager);
        final Rectangle left = new Rectangle(0, 0, 2, CAMERA_HEIGHT, vertexBufferObjectManager);
        final Rectangle right = new Rectangle(CAMERA_WIDTH - 2, 0, 2, CAMERA_HEIGHT, vertexBufferObjectManager);

        final FixtureDef wallFixtureDef = PhysicsFactory.createFixtureDef(0, 0.5f, 0.5f);

        PhysicsFactory.createBoxBody(this.mPhysicsWorld, roof, BodyType.StaticBody, wallFixtureDef);
        PhysicsFactory.createBoxBody(this.mPhysicsWorld, left, BodyType.StaticBody, wallFixtureDef);
        PhysicsFactory.createBoxBody(this.mPhysicsWorld, right, BodyType.StaticBody, wallFixtureDef);

        this.mScene.attachChild(roof);
        this.mScene.attachChild(left);
        this.mScene.attachChild(right);

        this.mScene.registerUpdateHandler(this.mPhysicsWorld);

        this.mScene.setOnAreaTouchListener(this);

        createitemstimehandler();

        return this.mScene;
    }

    @Override
    public boolean onAreaTouched( final TouchEvent pSceneTouchEvent, final ITouchArea pTouchArea,final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
        if(pSceneTouchEvent.isActionDown()) {
            final Sprite face =  (Sprite) pTouchArea;
            this.jumpFace(face);
            return true;
        }

        return false;
    }

    //add objects by touch
    @Override
    public boolean onSceneTouchEvent(final Scene pScene, final TouchEvent pSceneTouchEvent) {
        /*if(this.mPhysicsWorld != null) {
            if(pSceneTouchEvent.isActionDown()) {
                this.addFace(pSceneTouchEvent.getX(), pSceneTouchEvent.getY());
                return true;
            }
        }*/
        return false;
    }

    @Override
    public void onAccelerationAccuracyChanged(final AccelerationData pAccelerationData) {

    }

    @Override
    public void onAccelerationChanged(final AccelerationData pAccelerationData) {
        this.mGravityX = pAccelerationData.getX();
        this.mGravityY = pAccelerationData.getY();

        final Vector2 gravity = Vector2Pool.obtain(this.mGravityX, this.mGravityY);
        this.mPhysicsWorld.setGravity(gravity);
        Vector2Pool.recycle(gravity);
    }

    @Override
    public void onResumeGame() {
        super.onResumeGame();

        this.enableAccelerationSensor(this);
    }

    @Override
    public void onPauseGame() {
        super.onPauseGame();

        this.disableAccelerationSensor();
    }

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

    private void addFace(final float pX, final float pY) {
        this.mFaceCount++;
        TextureRegion whichtexture = null;

        if(whichface==0)
        {
            whichtexture=mBoxFaceTextureRegion;
        }
        if(whichface==1)
        {
            whichtexture=mCircleFaceTextureRegion;
        }
        if(whichface==2)
        {
            whichtexture=mBaseballTextureRegion;
        }
        if(whichface==3)
        {
            whichtexture= mBatTextureRegion;
        }
        if(whichface==4)
        {
            whichtexture=mTomatoeTextureRegion;
        }
        if(whichface==5)
        {
            whichtexture=mCanTextureRegion;
        }
        if(whichface==6)
        {
            whichtexture=mBottleTextureRegion;
        }
        final Sprite face;
        final Body body;

        final FixtureDef objectFixtureDef = PhysicsFactory.createFixtureDef(1, 0.5f, 0.5f);

        //add object
        face = new Sprite(pX, pY, whichtexture, this.getVertexBufferObjectManager());
        face.setScale(itemscale);
        body = PhysicsFactory.createCircleBody(this.mPhysicsWorld, face, BodyType.DynamicBody, objectFixtureDef);

        this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(face, body, true, true));

        face.setUserData(body);
        this.mScene.registerTouchArea(face);
        this.mScene.attachChild(face);

        whichface++;
        if(whichface>6)
        {
            whichface=0;
        }
    }

    private void jumpFace(final Sprite face) {
        final Body faceBody = (Body)face.getUserData();

        final Vector2 velocity = Vector2Pool.obtain(this.mGravityX * -3, this.mGravityY * -3);
        faceBody.setLinearVelocity(velocity);
        Vector2Pool.recycle(velocity);
    }


    private void createitemstimehandler() {
        TimerHandler itemTimerHandler;
        float mEffectSpawnDelay = 1f;

        itemTimerHandler = new TimerHandler(mEffectSpawnDelay, true,
        new ITimerCallback() {

            @Override
            public void onTimePassed(TimerHandler pTimerHandler) {
                addFace(CAMERA_HEIGHT,CAMERA_WIDTH/2);
                jumpFace ((Sprite)mScene.getLastChild());
            }
        });

        getEngine().registerUpdateHandler(itemTimerHandler);
    }



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

4 个答案:

答案 0 :(得分:13)

我认为您需要在onCreateResources()方法的末尾添加此内容

this.mBitmapTextureAtlas.load();

答案 1 :(得分:2)

问题是你的所有图像都比纹理图集大。这与地图集的整个表面无关。地图集可能有更大的表面,但是根据纹理的大小和形状,它可能无法将它们全部添加到其中,并且无法加载。

我可以告诉你,因为你说它可以正常工作,直到你添加另一个。 尝试使您的纹理图集更大或更好,创建另一个纹理图集并在那里添加其他精灵。

我遇到了这个问题,发现问题就在于此。

希望它有所帮助!

答案 2 :(得分:0)

答案 3 :(得分:0)

检查

  1. 您的Atlas已加载
  2. 如果您在场景变换期间装载和卸载了地图册,请确保再次装入地图册。
  3. 图像的大小。图像尺寸不应超过2048 * 2048.尺寸大于此尺寸会在不同设备(高分辨率设备)中产生问题。