Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Andengine Sprites appear as black boxes

for some reason all of my sprites started appearing as black boxes when before they were not. here is my code. I had it running fine with just the first 2 sprites but when i added the other sprites they became black boxed on my android device. any help would be greatly appreciated.

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
    // ===========================================================
}
like image 390
Habeeb Ahmed Avatar asked Dec 06 '22 15:12

Habeeb Ahmed


2 Answers

I think you need to add this at end the of your onCreateResources() method

this.mBitmapTextureAtlas.load();
like image 56
jmroyalty Avatar answered Dec 09 '22 04:12

jmroyalty


The problem is that all your images together are bigger than your texture atlas. This has nothing to do with the total surface of the atlas. The atlas might have a bigger surface, but by the size and shape of your textures, it might not be able to add them all inside of it, and it fails to load.

I can tell this because you say that it works fine until you add another. Try to make your Texture Atlas bigger or better yet, create another Texture Atlas and add the other sprite there.

I've had this exact problem and found the problem was that.

Hope it helps!

like image 39
Juanu Avatar answered Dec 09 '22 03:12

Juanu