4

出于某种原因,我的所有精灵都开始显示为黑框,而之前它们不是。这是我的代码。我只用前 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

4 回答 4

13

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

this.mBitmapTextureAtlas.load();
于 2012-04-09T22:52:07.067 回答
2

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

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

我遇到了这个确切的问题,发现问题是这样的。

希望能帮助到你!

于 2013-04-07T20:40:56.403 回答
0

这是否与此问题有关:问题 53:BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset 当文件名称中有下划线时找不到位图文件

于 2012-04-09T19:07:01.020 回答
0

查看

  1. 您的地图集已加载
  2. 如果您在场景更改期间加载和卸载了您的图集,请确保您再次加载了您的图集。
  3. 图像的大小。图像尺寸不应超过 2048 * 2048。尺寸大于此尺寸会在不同设备(高分辨率设备)中产生问题。
于 2015-08-20T07:41:28.487 回答