Android游戏:移动位图会在末端留下模糊的痕迹

时间:2014-07-01 10:12:27

标签: java android bitmap android-animation game-development

我正在尝试为Android游戏创建背景滚动。可以说这是一组以一定速度移动的图像。 为了测试一些基本元素,我拍摄了一个小的矩形图像,并从上到下线性移动。它移动但我看到一条小路,就像火箭在云中飞行(哈哈!)。我在Paint()类中尝试了各种选项来纠正这个问题,但我无法做到。

我将首先发布一个屏幕截图,然后添加相关代码。

所以,这里的垂直线是单个png图像:

现在我正在做的是通过一次增加y来从某个(x,y)移动这个位图。它移动,但在移动时注意图像的末端。一条小道,但不是一条永久的小道。我试过但无法删除此效果。我试过三星Galaxy S5。我没有测试任何其他模型。

代码:

移动的垂直图像是Sprite对象:

package com.src.*.*;


import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Rect;
import android.util.Log;

public class Sprite {

    private Rect sourceRect;    // the rectangle to be drawn from the animation bitmap
    private int frameNr;        // number of frames in animation
    private int currentFrame;   // the current frame
    private long frameTicker;   // the time of the last frame update
    private int framePeriod;    // milliseconds between each frame (1000/fps)

    private int spriteWidth;    // the width of the sprite to calculate the cut out rectangle
    private int spriteHeight;   // the height of the sprite

    private int x;              // the X coordinate of the object (top left of the image)
    private int y;              // the Y coordinate of the object (top left of the image)
    private Bitmap bitmap;
    private float degrees;
    private boolean rotate=false;
    private Matrix matrix;
    private Bitmap reversedBitmap;
    private Paint paint;

    public Sprite(Bitmap bitmap, int x, int y, int fps, int frameCount) {
        this.bitmap = bitmap;
        this.x = x;
        this.y = y;
        currentFrame = 0;
        frameNr = frameCount;
        spriteWidth = bitmap.getWidth() / frameCount;
        spriteHeight = bitmap.getHeight();
        sourceRect = new Rect(0, 0, spriteWidth, spriteHeight);
        framePeriod = 1000 / fps;
        frameTicker = 0l;
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG
                | Paint.FILTER_BITMAP_FLAG);
        paint.setDither(true);
    }

    public void animate(){

    }
    public int getX() {
        return x;
    }
    public void setX(int x) {
        this.x = x;
    }
    public int getY() {
        return y;
    }
    public void setY(int y) {
        this.y = y;
    }

    public void setReverse(Bitmap reverse){
        this.reversedBitmap = reverse;
    }

    public void rorateSprite(float degrees){
        if(degrees == 0){
            rotate = false;
        }else{
            rotate = true;
        }
        this.degrees = degrees;
    }


        public void draw(Canvas canvas) {
            // where to draw the sprite
            Rect destRect = new Rect(getX(), getY(), getX() + spriteWidth, getY() + spriteHeight);

            if(rotate){
                canvas.drawBitmap(reversedBitmap, sourceRect, destRect, paint);
            }else{
                canvas.drawBitmap(bitmap, sourceRect, destRect, paint);
            }
            }

        public void update(long gameTime){
            if (gameTime > frameTicker + framePeriod) {
                frameTicker = gameTime;
                //Log.i("INFO", "In if loop" + currentFrame + " framenr:" + frameNr);
                // increment the frame
                currentFrame++;
                if (currentFrame >= frameNr) {
                    currentFrame = 0;
                }
            }else{
                //Log.i("INFO", "in else" + currentFrame + "");
            }
            // define the rectangle to cut out sprite
            //Log.i("INFO", "sprite width: " + spriteWidth);
            this.sourceRect.left = currentFrame * spriteWidth;
            this.sourceRect.right = this.sourceRect.left + spriteWidth;

        }
}

位图设置如下:     wallAnimation = new Sprite(resizedBitmapWall, 0, 0, 30, 1);     wallAnimation.setX(margin);

精灵的onDraw和Update如下所示。它没有任何异常或错误。所有这一切中唯一的问题是图像在末端具有模糊/尾部效果。我甚至不知道它的正确用语。

onDraw和更新代码:

@Override
protected void onDraw(Canvas canvas) {
    // fills the canvas with black
    canvas.drawColor(Color.WHITE);
    wallAnimation.draw(canvas);

    paint.setColor(Color.BLACK);

    for (int i = 0; i < 6; i++) {
        canvas.drawLine(margin+laneWidth*i, 0, margin+laneWidth*i, laneHeight, paint);

    }
    canvas.drawText("High Score: "+ highScore, 10, 10, paint);


}

public void Update(long gameTime) {
    // TODO Auto-generated method stub
    if(wallState < laneHeight+laneHeight/6){
        wallAnimation.setY(wallState);
        //wallState+=laneHeight/80;
        wallState+=20;
    }else{
        wallState = 0;
    }
}

0 个答案:

没有答案