S
SocietyGraz
Neues Mitglied
- 0
Hallo. Ich habe folgendes Problem:
Ich möchte einen Spieler (Bitmap) auf einem Canvas bewegen. Mein Problem ist, dass das alte Bild immer gelöscht werden sollte, sodass nur noch die aktuelle Bitmap angezeigt wird. Derzeit wird bei jeder bewegung ein neues Bild hinzugefügt und alle alten behalten. Wie kann ich diese bevor ich die neue Bitmap auf das Canvas zeichne, löschen?
GameScreen class:
private void updateRunning(List<TouchEvent> touchEvents, float deltaTime) {
Graphics g = game.getGraphics();
int len = touchEvents.size();
for (int i = 0; i < len; i++) {
TouchEvent event = touchEvents.get(i);
if (event.type == TouchEvent.TOUCH_DOWN) {
if(event.x < 100 && event.y < 100)
{
player.goUp();
g.drawPlayerUp(player.currentImageUp, player.x, player.y);
}
if(event.x > 700 && event.y < 200)
{
player.goRight();
g.drawPlayerRight(player.currentImageRight, player.x, player.y);
}
if(event.x > (GameImpl.displayWidth - 100) && event.y > (GameImpl.displayHeight - 100))
{
player.goDown();
g.drawPlayerDown(player.currentImageDown, player.x, player.y);
}
if(event.x < 100 && event.y > (GameImpl.displayHeight - 100))
{
player.goLeft();
g.drawPlayerLeft(player.currentImageLeft, player.x, player.y);
}
}
}
}
Player class:
public class Player {
public int x;
public int y;
public int currentImageUp = 2;
public int currentImageRight = 2;
public int currentImageLeft = 2;
public int currentImageDown = 2;
public static final int MOVE_SPEED = (int) (GameImpl.displayWidth / 60);
public Player(int x, int y) {
this.x = x;
this.y = y;
}
public void goUp() {
if (currentImageUp < 2)
currentImageUp++;
else
currentImageUp = 0;
this.y -= MOVE_SPEED;
}
public void goDown() {
if (currentImageDown < 2)
currentImageDown++;
else
currentImageDown = 0;
this.y = this.y + MOVE_SPEED;
}
public void goLeft() {
if(currentImageLeft < 2)
currentImageLeft++;
else
currentImageLeft = 0;
this.x -= MOVE_SPEED;
}
public void goRight() {
if (currentImageRight < 2)
currentImageRight++;
else
currentImageRight = 0;
this.x += MOVE_SPEED;
}
}
GraphicImpl class with one of the moving methods:
@Override
public void drawPlayerRight(int image, int x, int y) {
switch(image)
{
case 0:
canvas.drawBitmap(((PixmapImpl) Assets.player_new_right).bitmap, x, y, null);
break;
case 1:
canvas.drawBitmap(((PixmapImpl) Assets.player_new_right_left).bitmap, x, y, null);
break;
case 2:
canvas.drawBitmap(((PixmapImpl) Assets.player_new_right_right).bitmap, x, y, null);
break;
}
}
Ich möchte einen Spieler (Bitmap) auf einem Canvas bewegen. Mein Problem ist, dass das alte Bild immer gelöscht werden sollte, sodass nur noch die aktuelle Bitmap angezeigt wird. Derzeit wird bei jeder bewegung ein neues Bild hinzugefügt und alle alten behalten. Wie kann ich diese bevor ich die neue Bitmap auf das Canvas zeichne, löschen?
GameScreen class:
private void updateRunning(List<TouchEvent> touchEvents, float deltaTime) {
Graphics g = game.getGraphics();
int len = touchEvents.size();
for (int i = 0; i < len; i++) {
TouchEvent event = touchEvents.get(i);
if (event.type == TouchEvent.TOUCH_DOWN) {
if(event.x < 100 && event.y < 100)
{
player.goUp();
g.drawPlayerUp(player.currentImageUp, player.x, player.y);
}
if(event.x > 700 && event.y < 200)
{
player.goRight();
g.drawPlayerRight(player.currentImageRight, player.x, player.y);
}
if(event.x > (GameImpl.displayWidth - 100) && event.y > (GameImpl.displayHeight - 100))
{
player.goDown();
g.drawPlayerDown(player.currentImageDown, player.x, player.y);
}
if(event.x < 100 && event.y > (GameImpl.displayHeight - 100))
{
player.goLeft();
g.drawPlayerLeft(player.currentImageLeft, player.x, player.y);
}
}
}
}
Player class:
public class Player {
public int x;
public int y;
public int currentImageUp = 2;
public int currentImageRight = 2;
public int currentImageLeft = 2;
public int currentImageDown = 2;
public static final int MOVE_SPEED = (int) (GameImpl.displayWidth / 60);
public Player(int x, int y) {
this.x = x;
this.y = y;
}
public void goUp() {
if (currentImageUp < 2)
currentImageUp++;
else
currentImageUp = 0;
this.y -= MOVE_SPEED;
}
public void goDown() {
if (currentImageDown < 2)
currentImageDown++;
else
currentImageDown = 0;
this.y = this.y + MOVE_SPEED;
}
public void goLeft() {
if(currentImageLeft < 2)
currentImageLeft++;
else
currentImageLeft = 0;
this.x -= MOVE_SPEED;
}
public void goRight() {
if (currentImageRight < 2)
currentImageRight++;
else
currentImageRight = 0;
this.x += MOVE_SPEED;
}
}
GraphicImpl class with one of the moving methods:
@Override
public void drawPlayerRight(int image, int x, int y) {
switch(image)
{
case 0:
canvas.drawBitmap(((PixmapImpl) Assets.player_new_right).bitmap, x, y, null);
break;
case 1:
canvas.drawBitmap(((PixmapImpl) Assets.player_new_right_left).bitmap, x, y, null);
break;
case 2:
canvas.drawBitmap(((PixmapImpl) Assets.player_new_right_right).bitmap, x, y, null);
break;
}
}