输出时的Andegine错误加载映射具有:水平条纹

时间:2015-05-21 04:25:29

标签: android andengine tmx

输出时的Andegine错误加载图:水平条纹

我有加载地图,但在地图中有水平条纹。我不知道。请帮助我。我是新开发者,我使用的是andegine。任何人都可以帮我解决这个问题吗?有什么想法吗?

图片: enter image description here

代码

public class TileActivity
  extends SimpleBaseGameActivity {

    private TMXTiledMap mTMXTiledMap;
    private BoundCamera mBoundChaseCamera;

    private static final int CAMERA_WIDTH = 480;
    private static final int CAMERA_HEIGHT = 320;
    private Scene mScene;

    private static final long[] ANIMATE_DURATION = new long[] { 200, 200, 200 };
    private static final int PLAYER_VELOCITY = 2;

    private BitmapTextureAtlas mTexturePlayer;
    private Body mPlayerBody;
    private TiledTextureRegion mPlayerTextureRegion;
    private BitmapTextureAtlas mOnScreenControlTexture;
    private TextureRegion mOnScreenControlBaseTextureRegion;
    private TextureRegion mOnScreenControlKnobTextureRegion;
    private DigitalOnScreenControl mDigitalOnScreenControl;
    private PhysicsWorld mPhysicsWorld;
    private TMXLayer layer;

    @Override
    protected void onCreateResources() {
        BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
        // Control texture
        this.mOnScreenControlTexture = new BitmapTextureAtlas(getTextureManager(), 256, 128, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
        this.mOnScreenControlBaseTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mOnScreenControlTexture, this, "onscreen_control_base.png", 0, 0);
        this.mOnScreenControlKnobTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mOnScreenControlTexture, this, "onscreen_control_knob.png", 128, 0);

        // Player sprite texture
        this.mTexturePlayer = new BitmapTextureAtlas(getTextureManager(), 96, 128, TextureOptions.DEFAULT);
        this.mPlayerTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mTexturePlayer, this, "hero.png", 0, 0, 3, 4);

        // Load the textures
        this.mTexturePlayer.load();
        this.mOnScreenControlTexture.load();
    }

    @Override
    protected Scene onCreateScene() {

        this.mEngine.registerUpdateHandler(new FPSLogger());

        // Create physics world
        this.mPhysicsWorld = new FixedStepPhysicsWorld(30, new Vector2(0, 0), false, 8, 1);

        // Create the scene and register the physics world
        mScene = new Scene();
        mScene.registerUpdateHandler(this.mPhysicsWorld);

        // Load the TMX map
        try {
            final TMXLoader tmxLoader = new TMXLoader(this.getAssets(),
              this.mEngine.getTextureManager(),
              TextureOptions.BILINEAR_PREMULTIPLYALPHA,
              getVertexBufferObjectManager(),
              new TMXLoader.ITMXTilePropertiesListener() {
                  @Override
                  public void onTMXTileWithPropertiesCreated(final TMXTiledMap pTMXTiledMap, final TMXLayer pTMXLayer, final TMXTile pTMXTile, final TMXProperties<TMXTileProperty> pTMXTileProperties) {}
              });
            this.mTMXTiledMap = tmxLoader.loadFromAsset("test.tmx");
        } catch (final TMXLoadException tmxle) {
            Debug.e(tmxle);
        }
        // Add the non-object layers to the scene
        for (int i = 0; i < this.mTMXTiledMap.getTMXLayers().size(); i++) {
            layer = this.mTMXTiledMap.getTMXLayers().get(i);
            if (!layer.getTMXLayerProperties().containsTMXProperty("wall", "true")) {
                mScene.attachChild(layer);
            }
        }

        // Read in the unwalkable blocks from the object layer and create boxes for each
        this.createUnwalkableObjects(mTMXTiledMap);
        // Add outer walls
        this.addBounds(layer.getWidth(), layer.getHeight());
        this.mBoundChaseCamera.setBounds(0, layer.getWidth(), 0, layer.getHeight());

        // Calculate the coordinates for the player, so it's centred on the camera.
        final int centerX = (int) (CAMERA_WIDTH - this.mPlayerTextureRegion.getWidth()) / 2;
        final int centerY = (int) (CAMERA_HEIGHT - this.mPlayerTextureRegion.getHeight()) / 2;

        // Create the player sprite and add it to the scene.
        final AnimatedSprite player = new AnimatedSprite(centerX, centerY, this.mPlayerTextureRegion, getVertexBufferObjectManager());
        this.mBoundChaseCamera.setChaseEntity(player);
        final FixtureDef playerFixtureDef = PhysicsFactory.createFixtureDef(0, 0, 0.5f);
        mPlayerBody = PhysicsFactory.createBoxBody(this.mPhysicsWorld, player, BodyDef.BodyType.DynamicBody, playerFixtureDef);
        this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(player, mPlayerBody, true, false) {
            @Override
            public void onUpdate(float pSecondsElapsed) {
                super.onUpdate(pSecondsElapsed);
                mBoundChaseCamera.updateChaseEntity();
            }
        });
        mScene.attachChild(player);

        // Add the control
        this.mDigitalOnScreenControl = new DigitalOnScreenControl(0, CAMERA_HEIGHT - this.mOnScreenControlBaseTextureRegion.getHeight(), this.mBoundChaseCamera, this.mOnScreenControlBaseTextureRegion, this.mOnScreenControlKnobTextureRegion, 0.1f, getVertexBufferObjectManager(),
          new BaseOnScreenControl.IOnScreenControlListener() {
              @Override
              public void onControlChange(final BaseOnScreenControl pBaseOnScreenControl, final float pValueX, final float pValueY) {
                  // Set the correct walking animation
                  if (pValueY == 1) {
                      // Up
                      if (playerDirection != PlayerDirection.UP) {
                          player.animate(ANIMATE_DURATION, 0, 2, true);
                          playerDirection = PlayerDirection.UP;
                      }
                  } else if (pValueY == -1) {
                      // Down
                      if (playerDirection != PlayerDirection.DOWN) {
                          player.animate(ANIMATE_DURATION, 9, 11, true);
                          playerDirection = PlayerDirection.DOWN;
                      }
                  } else if (pValueX == -1) {
                      // Left
                      if (playerDirection != PlayerDirection.LEFT) {
                          player.animate(ANIMATE_DURATION, 3, 5, true);
                          playerDirection = PlayerDirection.LEFT;
                      }
                  } else if (pValueX == 1) {
                      // Right
                      if (playerDirection != PlayerDirection.RIGHT) {
                          player.animate(ANIMATE_DURATION, 6, 8, true);
                          playerDirection = PlayerDirection.RIGHT;
                      }
                  } else {
                      if (player.isAnimationRunning()) {
                          player.stopAnimation();
                          playerDirection = PlayerDirection.NONE;
                      }
                  }
                  // Set the player's velocity
                  mPlayerBody.setLinearVelocity(pValueX * PLAYER_VELOCITY, pValueY * PLAYER_VELOCITY);
              }
          });
        this.mDigitalOnScreenControl.getControlBase().setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
        this.mDigitalOnScreenControl.getControlBase().setAlpha(0.5f);
        this.mDigitalOnScreenControl.getControlBase().setScaleCenter(0, 128);
        this.mDigitalOnScreenControl.getControlBase().setScale(1.25f);
        this.mDigitalOnScreenControl.getControlKnob().setScale(1.25f);
        this.mDigitalOnScreenControl.getControlKnob().setAlpha(0.5f);
        this.mDigitalOnScreenControl.refreshControlKnobPosition();

        mScene.setChildScene(this.mDigitalOnScreenControl);

        return mScene;
    }

    @Override
    public EngineOptions onCreateEngineOptions() {
        this.mBoundChaseCamera = new BoundCamera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
        return new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new FillResolutionPolicy(), this.mBoundChaseCamera);
    }

    private enum PlayerDirection {
        NONE,
        UP,
        DOWN,
        LEFT,
        RIGHT
    }

    private PlayerDirection playerDirection = PlayerDirection.NONE;

    private void createUnwalkableObjects(TMXTiledMap map) {
        // Loop through the object groups
        for (final TMXObjectGroup group : this.mTMXTiledMap.getTMXObjectGroups()) {
            if (group.getTMXObjectGroupProperties().containsTMXProperty("wall", "true")) {
                // This is our "wall" layer. Create the boxes from it
                for (final TMXObject object : group.getTMXObjects()) {
                    final Rectangle rect = new Rectangle(object.getX(), object.getY(), object.getWidth(), object.getHeight(), getVertexBufferObjectManager());
                    final FixtureDef boxFixtureDef = PhysicsFactory.createFixtureDef(0, 0, 1f);
                    PhysicsFactory.createBoxBody(this.mPhysicsWorld, rect, BodyDef.BodyType.StaticBody, boxFixtureDef);
                    rect.setVisible(false);
                    mScene.attachChild(rect);
                }
            }
        }
    }

    private void addBounds(float width, float height) {
        final Rectangle bottom = new Rectangle(0, height - 2, width, 2, getVertexBufferObjectManager());
        bottom.setVisible(false);
        final Rectangle top = new Rectangle(0, 0, width, 2, getVertexBufferObjectManager());
        top.setVisible(false);
        final Rectangle left = new Rectangle(0, 0, 2, height, getVertexBufferObjectManager());
        left.setVisible(false);
        final Rectangle right = new Rectangle(width - 2, 0, 2, height, getVertexBufferObjectManager());
        right.setVisible(false);

        final FixtureDef wallFixtureDef = PhysicsFactory.createFixtureDef(0, 0, 1f);
        PhysicsFactory.createBoxBody(this.mPhysicsWorld, bottom, BodyDef.BodyType.StaticBody, wallFixtureDef);
        PhysicsFactory.createBoxBody(this.mPhysicsWorld, top, BodyDef.BodyType.StaticBody, wallFixtureDef);
        PhysicsFactory.createBoxBody(this.mPhysicsWorld, left, BodyDef.BodyType.StaticBody, wallFixtureDef);
        PhysicsFactory.createBoxBody(this.mPhysicsWorld, right, BodyDef.BodyType.StaticBody, wallFixtureDef);

        this.mScene.attachChild(bottom);
        this.mScene.attachChild(top);
        this.mScene.attachChild(left);
        this.mScene.attachChild(right);
    }
}

1 个答案:

答案 0 :(得分:1)

这通常是由像素混合引起的,在以下情况下会发生:

  • 如果纹理的渲染小于原始尺寸,则会对像素进行平均,而相邻像素的值会影响切片的边缘。
  • 如果纹理渲染大于原始大小,则会插入像素值,这也会导致相邻像素的值影响切片的边缘。
  • 如果纹理未与像素精确对齐,则也会发生插值。

所以要避免它,要么确保没有发生上述情况,要么禁用纹理过滤(将它放在最近的邻居上)。

当地图缩放时,最近邻过滤看起来并不好看,所以如果你想支持这种情况,你可以选择确保每个图块都被与其边缘像素的颜色相匹配的像素包围。这可以避免其他瓷砖的颜色影响其边界。