我正在开发一款简单的2D无限跑步游戏。我试图让玩家向任何方向移动而无需专门触摸玩家。我希望游戏找到用户触摸的位置,然后允许用户从那里控制玩家。当玩家移动时,但仅当用户直接触摸玩家时,这使得用户在玩游戏并试图躲避障碍时难以看到实际的玩家。
以下是我目前的代码:
public boolean recieveTouch(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
int action = event.getAction();
int x = (int) event.getX(); // or getRawX();
int y = (int) event.getY();
int h2 = bitmap2.getHeight();
int w2 = bitmap2.getWidth();
float xRatio2 = (Constants.SCREEN_WIDTH / 2)-( w2 / 2);
float yRatio2 = (Constants.SCREEN_HEIGHT / 2)-( h2 / 2) + (Constants.SCREEN_WIDTH/4 + (Constants.SCREEN_WIDTH/6)- Constants.SCREEN_HEIGHT/10);
int h55 = bitmap55.getHeight();
int w55 = bitmap55.getWidth();
int h66 = bitmap66.getHeight();
int w66 = bitmap66.getWidth();
float xRatio55 = ((Constants.SCREEN_WIDTH / 4)-( w55 / 4));
float yRatio55 = (((Constants.SCREEN_HEIGHT / 2) + (h55 / 2)) + (Constants.SCREEN_HEIGHT/6));
float xRatio66 = ((Constants.SCREEN_WIDTH / 2)+( w66 / 4) + (Constants.SCREEN_HEIGHT/24));
float yRatio66 = (((Constants.SCREEN_HEIGHT / 2) + (h66 / 2)) + (Constants.SCREEN_HEIGHT/6));
//help
int h5 = bitmap4.getHeight();
int w5 = bitmap4.getWidth();
float xRatio5 = ((Constants.SCREEN_WIDTH / 4) - (w5 / 1));
float yRatio5 = (((Constants.SCREEN_HEIGHT / 8) - (h5 / 1)));
long pressTime = System.currentTimeMillis();
if (!gameOver && player.getRectangle().contains((int) event.getX(), (int) event.getY()))
movingPlayer = true;
if (gameOver && System.currentTimeMillis() - gameOverTime >= 0) {
if (x >= xRatio2 && x < (xRatio2 + bitmap2.getWidth())
&& y >= yRatio2 && y < (yRatio2 + bitmap2.getHeight())) {
if (GamePanel.Ad1 == 1) {
MainActivity.click.start();
MainActivity.mAd = MobileAds.getRewardedVideoAdInstance(mActivityRef.get());
loadAd();
MainActivity.mAd.show();
//Appodeal.show(mActivityRef.get(), Appodeal.REWARDED_VIDEO);
}
}
if (x >= xRatio55 && x < (xRatio55 + bitmap55.getWidth())
&& y >= yRatio55 && y < (yRatio55 + bitmap55.getHeight())) {
MainActivity.click.start();
reset();
gameOver = false;
orientationData.newGame();
}
if (x >= xRatio66 && x < (xRatio66 + bitmap66.getWidth())
&& y >= yRatio66 && y < (yRatio66 + bitmap66.getHeight())) {
MainActivity.click.start();
SceneManager.ACTIVE_SCENE = 4;
MainActivity.mInterstitialAd.loadAd((new AdRequest.Builder().build()));
MainActivity.mInterstitialAd.show();
//Appodeal.show(mActivityRef.get(), Appodeal.INTERSTITIAL);
if (GamePanel.YelowBird == 4) {
GamePanel.YelowBird = 2;
GamePanel.RedBird = 1;
}
reset();
gameOver = false;
}
if (x >= xRatio5 && x < (xRatio5 + bitmap4.getWidth())
&& y >= yRatio5 && y < (yRatio5 + bitmap4.getHeight())) {
if (GamePanel.Help == 0) {
GamePanel.Help = 1;
if (GamePanel.Help == 1) {
MainActivity.click.start();
orientationData.register();
}
} else if (GamePanel.Help == 1) {
GamePanel.Help = 0;
if (GamePanel.Help == 0) {
MainActivity.click.start();
orientationData.pause();
}
}
} return true;
}
break;
case MotionEvent.ACTION_MOVE:
if (GamePanel.NinjaBird == 4) {
if (!gameOver)
playerPoint.set((int) event.getX(), (int) event.getY());
} else {
if (!gameOver && movingPlayer)
playerPoint.set((int) event.getX(), (int) event.getY());
}
break;
case MotionEvent.ACTION_UP:
movingPlayer = false;
break;
}
return false;
}
答案 0 :(得分:0)
如果其他人想要做类似的事情: 我更改了以下行 从这个:
if(!gameOver&amp;&amp; movingPlayer) playerPoint.set((int)event.getX(),(int)event.getY()); 对此:
if(!gameOver&amp;&amp; movingPlayer) playerPoint.set((int)event.getX(),(int)event.getY()+ 200);
所以你可以通过触摸200下方的像素来移动播放器。