0

Andegine error load map when output have :horizontal stripes

i have load map, but have horizontal stripes in maps. i dont know. please help me.i am new developer,i use andegine. Can anyone help me with this problem? Any idea?

the image : enter image description here

the code

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);
    }
}
GuilhE
  • 11,591
  • 16
  • 75
  • 116
QuestionAndroid
  • 881
  • 1
  • 8
  • 25

1 Answers1

1

This is generally caused by pixel blending, which happens in the following situations:

  • If the texture is rendered smaller than native size, the pixels are averaged and values from neighboring pixels affect the edges of the tiles.
  • If the texture is rendered larger than native size, pixel values are interpolated, which also causes values from neighboring pixels to affect the edges of the tiles.
  • If the texture is not rendered exactly aligned to a pixel, interpolation also occurs.

So to avoid it, either make sure none of the above happens or disable texture filtering (putting it on nearest neighbor).

Nearest neighbor filtering of course doesn't look nice when the map gets scaled, so if you want to support this case you can alternatively make sure that each tile is surrounded by pixels that match the color of its edge pixels. This avoids colors from other tiles affecting its borders.

Thorbjørn Lindeijer
  • 2,022
  • 1
  • 17
  • 22