当玩家击中食物时,使其消失。

huangapple go评论57阅读模式
英文:

When the player hits the food make it disapear

问题

以下是您提供的代码的翻译部分:

private void updateFood(){
    for (int i = 0; i < food.length; ++i) {
        // 是否应该追随或随机移动?
        // 3次里有2次我们会追随...
        if (rnd.nextInt(3) < 2) {
            // 我们追随
            int dx = player.getX() - food[i].getX();
            int dy = player.getY() - food[i].getY();
            if (Math.abs(dx) > Math.abs(dy)) {
                if (dx > 0) {
                    // 玩家在右边 - 相反的方向
                    food[i].moveLeft();
                } else {
                    // 玩家在左边 - 相反的方向
                    food[i].moveRight();
                }
            } else if (dy > 0) {
                // 玩家在下面 - 相反的方向
                food[i].moveUp();
            } else {
                // 玩家在上面 - 相反的方向
                food[i].moveDown();
            }
        } else {
            // 我们随机移动
            int move = rnd.nextInt(4);
            if (move == 0) {
                // 向右移动
                food[i].moveRight();
            } else if (move == 1) {
                // 向左移动
                food[i].moveLeft();
            } else if (move == 2) {
                // 向上移动
                food[i].moveUp();
            } else if (move == 3) {
                // 向下移动
                food[i].moveDown();
            }
        }
        if (food[i].getX() == player.getX() && food[i].getY() == player.getY()) {
            food[i].getX() = random(0, width);
            food[i].getY() = random(0, height);
        }
    }
}

最后的大约10行是我遇到问题的地方。

if (food[i].getX() == player.getX() && food[i].getY() == player.getY()) {
    food[i].getX() = random(0, width);
    food[i].getY() = random(0, height);
}
英文:

I am new to programing, so bare with me lol. Our teacher has given us a game in which we need to add some elements to it. It's a game where we have a player, some enemies and some food made out of rectangles. If the player hits the food i want the food to spawn somewhere else in the window. Heres is the code where im trying to make it happen:

 private void updateFood(){
for(int i = 0; i &lt; food.length; ++i)
{
//Should we follow or move randomly?
//2 out of 3 we will follow..
if(rnd.nextInt(3) &lt; 2)
{
//We follow
int dx = player.getX() - food[i].getX();
int dy = player.getY() - food[i].getY();
if(abs(dx) &gt; abs(dy))
{
if(dx &gt; 0)
{
//Player is to the right - opiste
food[i].moveLeft();
}
else
{
//Player is to the left - opiste
food[i].moveRight();
}
}
else if(dy &gt; 0)
{
//Player is down; - opiste
food[i].moveUp();
}
else
{//Player is up; - opiste
food[i].moveDown();
}
}
else
{
//We move randomly
int move = rnd.nextInt(4);
if(move == 0)
{
//Move right
food[i].moveRight();
}
else if(move == 1)
{
//Move left
food[i].moveLeft();
}
else if(move == 2)
{
//Move up
food[i].moveUp();
}
else if(move == 3)
{
//Move down
food[i].moveDown();
}
}
if(food[i].getX() == player.getX() &amp;&amp; food[i].getY() == player.getY()){
food[i].getX() = random(0,width);
food[i].getY() = random(0,height);
}
}
}

the last 10 lines (or so) is where im having the problem.

    if(food[i].getX() == player.getX() &amp;&amp; food[i].getY() == player.getY()){
food[i].getX() = random(0,width);
food[i].getY() = random(0,height);
</details>
# 答案1
**得分**: 2
由于对于你的 `food[index]` 对象的方法了解有限,我猜测 `.getX()` 是只读的,意味着你不能给它设置一个值。要么找一个 `.setX(number)` 方法,要么使用现有的 `.moveLeft/Right/Up/Down(number)` 方法配合一些数学运算。
<details>
<summary>英文:</summary>
With limited info on what methods your `food[index]` objects have, I&#39;m going to guess `.getX()` is read-only, meaning you can&#39;t set a value to it. Either find a `.setX(number)` method, or use the existing `.moveLeft/Right/Up/Down(number)` with a bit of maths.
</details>

huangapple
  • 本文由 发表于 2020年9月28日 03:59:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/64092760.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定