I'd wanted to ask how I can change the position of a bitmap, I am using the following code. I looked at the question below from a guy who seemed to have the same problem, yet I had already tried this and received a different animation location.
android positioning a drawBitmap that uses rect
public class Yodasprite implements Drawable {
private static final int BMP_COLUMNS = 6;
private static final int BMP_ROWS = 1;
private GameContent gameContent;
float fracsect;
private int x = 0;
private int y = 0;
int xSpeed = 5;
private Bitmap bmp;
private int currentFrame = 0;
private int width;
private int height;
long current_time;
long lasttime;
long delta;
public Yodasprite(GameContent gameContent, Bitmap bmp) {
this.gameContent = gameContent;
this.bmp = bmp;
this.width = bmp.getWidth() / BMP_COLUMNS;
this.height = bmp.getHeight() / BMP_ROWS;
this.lasttime = System.currentTimeMillis();
}
@Override
public void update(float fracsec) {
if (x > gameContent.getGameWidth() - width + xSpeed) {
xSpeed = -5;
}
if (x + xSpeed < 0) {
xSpeed = 5;
}
if (delta / fracsec >999) {
x = x + xSpeed;
currentFrame = ++currentFrame % BMP_COLUMNS;
}
}
@Override
public void draw(Canvas canvas) {
current_time = System.currentTimeMillis();
delta = current_time - lasttime;
fracsect = (float) delta / 1000;
update(fracsect);
int srcX = currentFrame * (width -4);
int srcY = height - height;
Rect src = new Rect(srcX, srcY,srcX + width+5,srcY+height);
Rect dst = new Rect(x,10,x+width, y+height);
canvas.drawBitmap(bmp, src, dst, null);
}
}
Rect dest according from what I understand from the documentation as well as the post above determines with x,y the location on the canvas. My sprite spawns at 0,0 and I'd like to move the sprite down via y. Any advice and or help with this ? Moving the sprite down like the code below does not seem to work.
Rect dst = new Rect(x,10,x+width, y+height);
Rect dst = new Rect(x,10,x+width, y+height);
Changed to :
Rect dst = new Rect(x,y,x+width, y+height);
With instantiating the value of y before gave me the solution I was looking for. Apparently hardcoded numbers do not work.