I have trees on the map. How to realize the player's inability to pass through them? I've never actually developed games before, so I don't quite understand how to do it.
So, i think that code would be in this block that I have now:
Here my move function if this would be helpful:
Help please..
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | public boolean canMove() { World myWorld = getWorld(); int x = getX(); int y = getY(); // test for outside border if (x >= myWorld.getWidth() || y >= myWorld.getHeight()) { return false ; } else if (x < 0 || y < 0 ) { return false ; } return true ; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | public void move() { if (!canMove()) { return ; } if (Greenfoot.isKeyDown( "down" )){ setImage( "player.png" ); setLocation(getX(), getY() + speed); } if (Greenfoot.isKeyDown( "up" )){ setImage( "player.png" ); setLocation(getX(), getY() - speed); } if (Greenfoot.isKeyDown( "right" )){ setImage( "player.png" ); setLocation(getX() + speed, getY()); } if (Greenfoot.isKeyDown( "left" )){ setImage( "playerLeft.png" ); setLocation(getX() - speed, getY()); } } |