2012-04-09 56 views
4

由于某些原因,我以前的所有精灵都开始出现黑盒子。这是我的代码。我把它运行得很好,只有第一个2个精灵,但是当我添加其他精灵时,他们变成了我的android设备上的黑色方块。任何帮助将不胜感激。Andengine精灵出现黑盒子

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 
    // =========================================================== 
} 
+0

您是否找到解决方案?对我来说,它适用于nexus 4,但在星系标签2上显示黑色框。...... – AndroidMechanic 2015-04-02 18:30:39

回答

13

我认为你需要在年底的onCreateResources()方法添加此

this.mBitmapTextureAtlas.load(); 
2

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

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

我有这个确切的问题,发现问题是这样的。

希望它有帮助!

+0

您的答案肯定有效。但是我需要一些使Texture Atlas变得更大更好的技巧。帮助我进入andEngine新手。 – Crawler 2014-10-10 06:08:41

0

检查

  1. 你阿特拉斯加载
  2. 如果您已经加载,并且在场景改变期间卸载你的图集,请确保您已再次载入您的地图集。
  3. 图像的大小。图像尺寸不应超过2048 * 2048.尺寸大于此值会在不同设备(高分辨率设备)中产生问题。