【问题标题】:Move background vertically in andengine在andengine中垂直移动背景
【发布时间】:2014-05-19 06:37:10
【问题描述】:

我想垂直移动背景。我试过了,但它不起作用。通过应用下面的代码图像背景保持静止。我通过在这里改变它的方向来尝试它(bgEntity = new VerticalParallaxEntity(0.0f,背景,方向))但是没有影响。

autoParallaxBackground = new VerticalParallaxBackground(0, 0, 0);
background = new Sprite(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT,
        this.mParallaxLayerBack, this.vbom);
bgEntity = new VerticalParallaxEntity(0.0f, background, 1);

autoParallaxBackground.attachVerticalParallaxEntity(bgEntity);
autoParallaxBackground.attachVerticalParallaxEntity(bgEntity);
mainScene.setBackground(autoParallaxBackground);

我用过这个类:

public class VerticalParallaxBackground extends ParallaxBackground {
    public static int SCROLL_DOWN = -1;
    public static int SCROLL_UP = 1;
    // ===========================================================
    // Constants
    // ===========================================================

    // ===========================================================
    // Fields
    // ===========================================================

    private final ArrayList<VerticalParallaxEntity> mParallaxEntities = new ArrayList<VerticalParallaxEntity>();
    private int mParallaxEntityCount;

    protected float mParallaxValue;

    // ===========================================================
    // Constructors
    // ===========================================================

    public VerticalParallaxBackground(float red, float green, float blue) {
        super(red, green, blue);
        // TODO Auto-generated constructor stub
    }

    // ===========================================================
    // Getter & Setter
    // ===========================================================

    public void setParallaxValue(final float pParallaxValue) {
        this.mParallaxValue = pParallaxValue;
    }

    // ===========================================================
    // Methods for/from SuperClass/Interfaces
    // ===========================================================

    @Override
    public void onDraw(final GLState pGLState, final Camera pCamera) {
        super.onDraw(pGLState, pCamera);

        final float parallaxValue = this.mParallaxValue;
        final ArrayList<VerticalParallaxEntity> parallaxEntities = this.mParallaxEntities;
        // Log.d("VAPB", "VAPB onDraw pre entity");
        for (int i = 0; i < this.mParallaxEntityCount; i++) {
            parallaxEntities.get(i).onDraw(pGLState, pCamera, parallaxValue);
        }
    }

    // ===========================================================
    // Methods
    // ===========================================================

    public void attachVerticalParallaxEntity(
            final VerticalParallaxEntity pParallaxEntity) {
        this.mParallaxEntities.add(pParallaxEntity);
        this.mParallaxEntityCount++;
    }

    public boolean detachVerticalParallaxEntity(
            final VerticalParallaxEntity pParallaxEntity) {
        this.mParallaxEntityCount--;
        final boolean success = this.mParallaxEntities.remove(pParallaxEntity);
        if (success == false) {
            this.mParallaxEntityCount++;
        }
        return success;
    }

    // ===========================================================
    // Inner and Anonymous Classes
    // ===========================================================

    public static class VerticalParallaxEntity {
        // ===========================================================
        // Constants
        // ===========================================================

        // ===========================================================
        // Fields
        // ===========================================================

        final float mParallaxFactor;
        final IAreaShape mShape;
        private int direction;

        // ===========================================================
        // Constructors
        // ===========================================================

        public VerticalParallaxEntity(final float pParallaxFactor,
                final IAreaShape pShape) {
            this.mParallaxFactor = pParallaxFactor;
            this.mShape = pShape;
            this.direction = VerticalParallaxBackground.SCROLL_DOWN;
        }

        public VerticalParallaxEntity(final float pParallaxFactor,
                final IAreaShape pShape, int direction) {
            this.mParallaxFactor = pParallaxFactor;
            this.mShape = pShape;
            this.direction = direction;
        }

        // ===========================================================
        // Getter & Setter
        // ===========================================================

        // ===========================================================
        // Methods for/from SuperClass/Interfaces
        // ===========================================================

        // ===========================================================
        // Methods
        // ===========================================================

        public void onDraw(final GLState pGL, final Camera pCamera,
                final float pParallaxValue) {

            pGL.pushModelViewGLMatrix();
            final float cameraHeight = pCamera.getHeight();
            final float shapeHeightScaled = this.mShape.getHeightScaled();
            float baseOffset = (pParallaxValue * this.mParallaxFactor)
                    % shapeHeightScaled;
            while (baseOffset > 0) {
                baseOffset -= shapeHeightScaled;
            }
            pGL.translateModelViewGLMatrixf(0, (direction * baseOffset), 0);
            float currentMaxY = baseOffset;
            do {
                this.mShape.onDraw(pGL, pCamera);
                pGL.translateModelViewGLMatrixf(0,
                        (direction * shapeHeightScaled), 0);
                currentMaxY += shapeHeightScaled;
            } while (currentMaxY < (cameraHeight + shapeHeightScaled));
            // Added shapeHeightScaled to cameraHeight so the drawing flows in
            // instead of popping in.

            pGL.popModelViewGLMatrix();

        }
        // ===========================================================
        // Inner and Anonymous Classes
        // ===========================================================
    }
}

有什么建议吗? 提前致谢!

【问题讨论】:

    标签: android andengine


    【解决方案1】:

    我无法对上一个答案添加评论,所以我将在此处添加。

    您需要使用 AutoVerticalParallaxBackground。这不是游戏引擎默认提供的,但 AndAppsUK 已经为我们创建了类(AutoVerticalParallaxBackground 和 VerticalParallaxBackground) - http://www.andengine.org/forums/post306324.html#p31347

    将类添加到代码后,只需更改此行

    autoParallaxBackground = new VerticalParallaxBackground(0, 0, 0);
    

    to(注意变化速度和不同等级的额外参数)

    autoParallaxBackground = new AutoVerticalParallaxBackground(0, 0, 0, 15);
    

    另外,将 VerticalParallaxEntity 上的速度值更改为 0.2f

    bgEntity = new VerticalParallaxEntity(0.2f, background, 1);
    

    然后一点点实验应该可以得到你想要的结果。

    希望这会有所帮助。

    【讨论】:

      【解决方案2】:

      看起来你做的一切都是正确的。只需在此处增加视差系数:

      bgEntity = new VerticalParallaxEntity(0.0f, background, 1);
      

      0.0f - 没有因素所以它不会移动

      你也可以试试

      new VerticalParallaxBackground(0, 0, 0, 5);
      

      最后一个参数是每秒变化

      【讨论】:

      • 通过使用上述建议没有发生任何事情。背景没有移动。而且我没有任何具有这些参数的构造函数..(new VerticalParallaxBackground(0, 0, 0, 5);)
      猜你喜欢
      • 2017-08-27
      • 1970-01-01
      • 1970-01-01
      • 2017-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-09
      • 1970-01-01
      相关资源
      最近更新 更多