This site requires JavaScript, please enable it in your browser!
Greenfoot back
Zurah
Zurah wrote ...

2021/1/13

removing object from array on world by clicking at it

1
2
3
Zurah Zurah

2021/1/15

#
public void act()
    {
        // check moving
        int playerX = 0, playerY = 0;
        
        if (Greenfoot.isKeyDown("D")) playerX++;
        if (Greenfoot.isKeyDown("A")) playerX--;
        if (Greenfoot.isKeyDown("S") && playerY() < 14 && world[playerX][playerY + 1]) playerY++; // the line of code that i changed
        if (Greenfoot.isKeyDown("W")) playerY--;
        // do no more if trying to move past edge
        int x = player.getX(), y = player.getY();
        if (x+playerX < 0 || x+playerX > WIDE-1) return;
        if (y+playerY < 0 || y+playerY > HIGH-1) return;
        // move player
        player.setLocation(player.getX()+playerX, player.getY()+playerY);
        // scroll, if able
        if (!(playerX == -1 && scrollX == 0) &&
            !(playerY == -1 && scrollY == 0) &&
            !(playerX == 1 && scrollX == BIG_X-WIDE) &&
            !(playerY == 1 && scrollY == BIG_Y-HIGH)) scroll(playerX, playerY);
        // time delay when moving
        Greenfoot.delay(10);
}
if i do it like this, it throws me an error that the method playerY() cannot be found
danpost danpost

2021/1/15

#
Zurah wrote...
<< Code Omitted >> if i do it like this, it throws me an error that the method playerY() cannot be found
Wow. You are using playerX and playerY for something totally different from what they suggest. I would think you might change x and y (see line 11) to them, not dx and dy. dx and dy are the changes in x and y (the player's position) determined by the key states.
danpost danpost

2021/1/15

#
At any rate, you should leave line 8 alone (not adding more conditions there). You can make those checks more simply after line 13:
if (world[x+dx+scrollX][y+dy+scrollY] != 0) return;
Zurah Zurah

2021/1/15

#
thanks, i figured everything out i needed ^^
You need to login to post a reply.
1
2
3