terça-feira, 25 de setembro de 2012

dia 5

Caramba que dificuldade pra fazer essa bolinha bater em outra sprite... tentei, tentei, tentei não consegui... funciona meia boca ainda... quando estiver legal eu posto o código aqui :-)

um pouco mais tarde no dia eu consegui :-)

public class SquareSprite extends Sprite {

public SquareSprite(GameView gameView, Bitmap bmp, float x, float y) {
this.bmp = bmp;
this.x = x;
this.y = y;
gm = gameView;
}

public void onDraw(Canvas canvas) {
canvas.drawBitmap(bmp, x, y, null);
}

public boolean isCollition(Sprite sprite) {
float x2 = sprite.x;
float y2 = sprite.y;
float left = Math.max(x2, x);
float right = Math.min(x2 + sprite.bmp.getWidth(), x + bmp.getWidth());
float top = Math.min(y2, y);
float bottom = Math.max(y2 + sprite.bmp.getHeight(), y + bmp.getHeight());
for (float i = left; i < right; i++) {
for (float j = top; j < bottom; j++) {
if (sprite.isFilled(i, j) && this.isFilled(i, j)) {
return true;
}
}
}
return false;
}
}

a função isCollition verifica se a Sprite em questão está colidindo com a SquareSprite atual (x e y são os dados da posição onde a Sprite é desenhada e bmp o bitmap da sprite). Essa outra função eu coloquei na Sprite da bola e é acionada no onSensorEvent (se você fez o tutorial do sensor você sabe do que eu estou falando), o valor do axis representa se você bateu no eixo x, no y ou nos dois. E no finalzinho dela se você conseguiu colocar a bola dentro da Sprite Hole ela volta pro começo (x = y = 0)

public void acelleration(float x, float y, float z, HoleSprite hole) {
float originalx = this.x;
float originaly = this.y;
float xtemp = this.x + y * 5;
float ytemp = this.y + x * 5;
this.x = this.x + y * 5;
this.y = this.y + x * 5;
// limites da tela do celular
if (ytemp < gm.getHeight() - bmp.getHeight() && ytemp > 0)
this.y = ytemp;
else if (ytemp > gm.getHeight() - bmp.getHeight())
this.y = gm.getHeight() - bmp.getHeight();
else if (ytemp < 0)
this.y = 0;
if (xtemp < gm.getWidth() - bmp.getWidth() && xtemp > 0)
this.x = xtemp;
else if (xtemp > gm.getWidth() - bmp.getWidth())
this.x = gm.getWidth() - bmp.getWidth();
else if (xtemp < 0)
this.x = 0;
// verifica se bateu em um square
for (SquareSprite s : squares) {
if (s.isCollition(this)) {
if (s.axis == 1) {
this.x = originalx;
} else if (s.axis == 2) {
this.y = originaly;
} else if (s.axis == 3) {
this.x = originalx;
this.y = originaly;
}
s.axis = 0;
}
}
if(hole.isCollition(this)){
this.x = 0;
this.y = 0;
}

Nenhum comentário:

Postar um comentário