App Lab中需要Java碰撞检测

问题描述 投票:0回答:1

我正在code.org应用程序实验室制作游戏。游戏基于玩家2掉落的流星,玩家1必须躲避。但是,我不知道如何进行碰撞检测。有人可以帮忙吗?我需要一个基本的碰撞检测系统来降低流星击中玩家1的生命。随意使用Player 1的x和y值,以及添加自己的变量。我会向帮助我的人大声疾呼。这是我的代码:

var x = 104;
var y = 172;
var p1HP = 100;
var meteorCount = 25;
var mousex;
var mousey;
var meteorNum = 0;
//Code below triggers a screen change and music when a button is pressed.
onEvent("startButton", "click", function() {
  setScreen("playscreen");
  playSound("Brobot-Battle-(8-BIT)---Super-Paper-Mario-(192--kbps).mp3", false);
});
//Code below is basic movement for player.
onEvent("playscreen", "keydown", function(event) {
  if (event.key=="w") {
    getP1Pos();
    setPosition("image2", x, y-8, 100, 100);
  } else if (event.key=="a") {
    getP1Pos();
    setPosition("image2", x-8, y, 100, 100);
  } else if (event.key=="d") {
    getP1Pos();
    setPosition("image2", x+8, y, 100, 100);
  } else if (event.key=="s") {
    getP1Pos();
    setPosition("image2", x, y+8, 100, 100);
  }
});
function getP1Pos() {
  x = getXPosition("image2");
  y = getYPosition("image2");
}
//code below resets the game when a retry button is pressed
onEvent("retry"||"retry2", "click", function( ) {
  setScreen("startscreen");
  p1HP = 100;
  meteorCount = 20;
  setText("p1HP", "P1 HP: "+p1HP);
  setText("meteorCounter", "P2 METEORS: "+meteorCount);
});
//code below records mouse position
onEvent("playscreen", "mousemove", function(mouse) {
  mousex = mouse.x;
  mousey = mouse.y;
});
//code below makes the meteors go to the mouse and fall when the screen is clicked
onEvent("playscreen", "click", function() {
  stopTimedLoop();
  if (meteorNum==5) {
    meteorNum = 1;
  } else {
    meteorNum = meteorNum+1;
  }
  meteorCount = meteorCount-1;
  if (meteorCount==-1) {
    setScreen("p1victory");
    stopSound("Brobot-Battle-(8-BIT)---Super-Paper-Mario-(192--kbps).mp3");
  } else {
    setPosition("meteor"+meteorNum, mousex-20, mousey-20, 100, 100);
    timedLoop(60, function() {
      setPosition("meteor"+meteorNum, getXPosition("meteor"+meteorNum), getYPosition("meteor"+meteorNum) + 40, 100, 100);
    });
    setText("meteorCounter", "P2 METEORS: "+meteorCount);
  }
});
//Insert collision detection algorithm here
javascript collision-detection
1个回答
0
投票

你好!如果您访问code.org并共享您的项目,那么它将更容易使用,并且我可以尝试为您提供更快的帮助,但是您却没有,因此难度更大。我可能有一些可行的方法:
sprite.collide(target);
如果它不起作用,那么请为您的项目添加链接。

请给我点赞,因为我需要它

© www.soinside.com 2019 - 2024. All rights reserved.