Skip to content

Commit

Permalink
拖动的时候判断手指位置
Browse files Browse the repository at this point in the history
  • Loading branch information
@zyk1002_room committed Dec 4, 2023
1 parent 08b74af commit 5382fa8
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,20 @@ public void fight(GameView1 gameView) {
return;
}

float x = getX() + getWidth() / 2;
float y = getY() - 5;
int y = getY() - 5;
if (single) {
//单发模式下发射单发黄色子弹
Bitmap yellowBulletBitmap = gameView.getYellowBulletBitmap();
Bullet yellowBullet = new Bullet(yellowBulletBitmap);
int x = getX() + getWidth() / 2 - yellowBulletBitmap.getWidth() / 2;
yellowBullet.moveTo(x, y);
gameView.addSprite(yellowBullet);
} else {
int x = getX() + getWidth() / 2;
//双发模式下发射两发蓝色子弹
float offset = getWidth() / 4;
float leftX = x - offset;
float rightX = x + offset;
int offset = getWidth() / 4;
int leftX = x - offset;
int rightX = x + offset;
Bitmap blueBulletBitmap = gameView.getBlueBulletBitmap();

Bullet leftBlueBullet = new Bullet(blueBulletBitmap);
Expand Down Expand Up @@ -172,8 +173,8 @@ private void explode(GameView1 gameView) {
if (!collide) {
collide = true;
setVisibility(false);
float centerX = getX() + getWidth() / 2;
float centerY = getY() + getHeight() / 2;
int centerX = getX() + getWidth() / 2;
int centerY = getY() + getHeight() / 2;
Explosion explosion = new Explosion(gameView.getExplosionBitmap());
explosion.centerTo(centerX, centerY);
gameView.addSprite(explosion);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ protected void afterDraw(Canvas canvas, Paint paint, GameView1 gameView) {
//创建爆炸效果后会销毁敌机
public void explode(GameView1 gameView) {
//创建爆炸效果
float centerX = getX() + getWidth() / 2;
float centerY = getY() + getHeight() / 2;
int centerX = getX() + getWidth() / 2;
int centerY = getY() + getHeight() / 2;
Bitmap bitmap = gameView.getExplosionBitmap();
Explosion explosion = new Explosion(bitmap);
explosion.centerTo(centerX, centerY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public Explosion(Bitmap bitmap) {
}

@Override
public float getWidth() {
public int getWidth() {
Bitmap bitmap = getBitmap();
if (bitmap != null) {
return bitmap.getWidth() / segment;
Expand Down
40 changes: 23 additions & 17 deletions app/src/main/java/com/zyk/demo/code/demo1/view/GameView1.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ public class GameView1 extends View {
private long lastSingleClickTime = -1;//上次发生单击的时刻
private long touchDownTime = -1;//触点按下的时刻
private long touchUpTime = -1;//触点弹起的时刻
private float touchX = -1;//触点的x坐标
private float touchY = -1;//触点的y坐标
private int touchX = -1;//触点的x坐标
private int touchY = -1;//触点的y坐标

public GameView1(Context context) {
super(context);
Expand Down Expand Up @@ -158,8 +158,8 @@ private void drawGameStarted(Canvas canvas) {

//第一次绘制时,将战斗机移到Canvas最下方,在水平方向的中心
if (frame == 0) {
float centerX = canvas.getWidth() / 2;
float centerY = canvas.getHeight() - combatAircraft.getHeight() / 2;
int centerX = canvas.getWidth() / 2;
int centerY = canvas.getHeight() - combatAircraft.getHeight() / 2;
combatAircraft.centerTo(centerX, centerY);
}

Expand Down Expand Up @@ -411,10 +411,10 @@ private void createRandomSprites(int canvasWidth) {
}

if (sprite != null) {
float spriteWidth = sprite.getWidth();
float spriteHeight = sprite.getHeight();
float x = (float) ((canvasWidth - spriteWidth) * Math.random());
float y = -spriteHeight;
int spriteWidth = sprite.getWidth();
int spriteHeight = sprite.getHeight();
int x = (int) ((canvasWidth - spriteWidth) * Math.random());
int y = -spriteHeight;
sprite.setX(x);
sprite.setY(y);
if (sprite instanceof AutoSprite) {
Expand All @@ -427,6 +427,8 @@ private void createRandomSprites(int canvasWidth) {

/*-------------------------------touch------------------------------------*/

private final int touchWith = 30;// 手指的半径

@Override
public boolean onTouchEvent(MotionEvent event) {
//通过调用resolveTouchType方法,得到我们想要的事件类型
Expand All @@ -436,7 +438,11 @@ public boolean onTouchEvent(MotionEvent event) {
if (status == STATUS_GAME_STARTED) {
if (touchType == TOUCH_MOVE) {
if (combatAircraft != null) {
combatAircraft.centerTo(touchX, touchY);
Rect touchRect = new Rect(touchX - touchWith, touchY - touchWith, touchX + touchWith, touchY + touchWith);
if (Rect.intersects(touchRect, combatAircraft.getCurrentRect())) {
//移动玩家战机
combatAircraft.centerTo(touchX, touchY);
}
}
} else if (touchType == TOUCH_DOUBLE_CLICK) {
if (status == STATUS_GAME_STARTED) {
Expand All @@ -462,8 +468,8 @@ public boolean onTouchEvent(MotionEvent event) {
private int resolveTouchType(MotionEvent event) {
int touchType = -1;
int action = event.getAction();
touchX = event.getX();
touchY = event.getY();
touchX = (int) event.getX();
touchY = (int) event.getY();
if (action == MotionEvent.ACTION_MOVE) {
long deltaTime = System.currentTimeMillis() - touchDownTime;
if (deltaTime > singleClickDurationTime) {
Expand Down Expand Up @@ -525,7 +531,7 @@ private boolean isSingleClick() {
return singleClick;
}

private void onSingleClick(float x, float y) {
private void onSingleClick(int x, int y) {
if (status == STATUS_GAME_STARTED) {
if (isClickPause(x, y)) {
//单击了暂停按钮
Expand All @@ -545,19 +551,19 @@ private void onSingleClick(float x, float y) {
}

//是否单击了左上角的暂停按钮
private boolean isClickPause(float x, float y) {
private boolean isClickPause(int x, int y) {
RectF pauseRecF = getPauseBitmapDstRecF();
return pauseRecF.contains(x, y);
}

//是否单击了暂停状态下的“继续”那妞
private boolean isClickContinueButton(float x, float y) {
return continueRect.contains((int) x, (int) y);
private boolean isClickContinueButton(int x, int y) {
return continueRect.contains(x, y);
}

//是否单击了GAME OVER状态下的“重新开始”按钮
private boolean isClickRestartButton(float x, float y) {
return continueRect.contains((int) x, (int) y);
private boolean isClickRestartButton(int x, int y) {
return continueRect.contains(x, y);
}

private RectF getPauseBitmapDstRecF() {
Expand Down
28 changes: 16 additions & 12 deletions app/src/main/java/com/zyk/demo/code/demo1/view/Sprite.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
*/
public class Sprite {
private boolean visible = true;
private float x = 0;
private float y = 0;
private int x = 0;
private int y = 0;
private float collideOffset = 0;
private Bitmap bitmap = null;
private boolean destroyed = false;
Expand All @@ -35,30 +35,30 @@ public boolean getVisibility() {
return visible;
}

public void setX(float x) {
public void setX(int x) {
this.x = x;
}

public float getX() {
public int getX() {
return x;
}

public void setY(float y) {
public void setY(int y) {
this.y = y;
}

public float getY() {
public int getY() {
return y;
}

public float getWidth() {
public int getWidth() {
if (bitmap != null) {
return bitmap.getWidth();
}
return 0;
}

public float getHeight() {
public int getHeight() {
if (bitmap != null) {
return bitmap.getHeight();
}
Expand All @@ -70,14 +70,14 @@ public void move(float offsetX, float offsetY) {
y += offsetY;
}

public void moveTo(float x, float y) {
public void moveTo(int x, int y) {
this.x = x;
this.y = y;
}

public void centerTo(float centerX, float centerY) {
float w = getWidth();
float h = getHeight();
public void centerTo(int centerX, int centerY) {
int w = getWidth();
int h = getHeight();
x = centerX - w / 2;
y = centerY - h / 2;
}
Expand Down Expand Up @@ -156,4 +156,8 @@ public boolean isDestroyed() {
public int getFrame() {
return frame;
}

public Rect getCurrentRect() {
return new Rect(x, y, x + getWidth(), y + getHeight());
}
}

0 comments on commit 5382fa8

Please sign in to comment.