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

2017/11/11

Make enemy detect position player

ebhiy ebhiy

2017/11/11

#
I will make a platform game. imagine the enemy have zone arround him, if player get in to the zone, enemy moving. If player get out the zone enemy stop moving. please help me code the method. I use this method for Enemy
   public void cekPlayer()
    {
        int x = ((player) getWorld().getObjects(player.class).get(0)).getnilaiX();
        int y = ((player) getWorld().getObjects(player.class).get(0)).getnilaiY();
        if(y < getY() +5 && y > getY() -5 && jumping==false){
           jump();
        }if ( x < getX()+5 && x > getX() -5 && jumping==false){
            jump();
        }

    }
And, this method for player
       public int getnilaiX() {
        return getX();
    }
    
    public int getnilaiY() {
        return getY();
    }
But, not working like my expect :( Please help mee Thanks a lot before
danpost danpost

2017/11/11

#
I would find out if the player was in range using 'getObjectsInRange' before trying to get its location coordinates. I am not sure why you created those methods in the player class. You can call 'player.getX()' actually easier than calling 'player.getnilaiX()'.
You need to login to post a reply.