i think i managed to implement your approach, with addedToWorld now. not sure if this is how you intended
so sorry for all the posts, i just really want this to work
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class NPC here. * * @author (your name) * @version (a version number or a date) */ public class FloatingSkull extends Actor { PC player; private int rotation = Greenfoot.getRandomNumber( 360 ); private int speed = 1 ; private boolean aggro = false ; private int [] roamLimit = new int [ 4 ]; private int roamLimitRange = 50 ; public void act(){ movementAI(); //track the player as soon as they enter a certain AOE, increase speed } protected void addedToWorld(MyWorld World){ roamLimit[ 0 ] = getX()+roamLimitRange; roamLimit[ 1 ] = getX()-roamLimitRange; roamLimit[ 2 ] = getY()+roamLimitRange; roamLimit[ 3 ] = getY()-roamLimitRange; } public FloatingSkull(PC player){ this .player = player; } public void movementAI(){ if (getObjectsInRange( 200 , PC. class ).isEmpty() && aggro == false ){ rotation += 15 -Greenfoot.getRandomNumber( 31 ); // (-15, 15) degree change in direction setRotation(rotation); move(speed* 2 ); setRotation( 0 ); if (getX() < roamLimit[ 0 ] || getX() > roamLimit[ 1 ] || getY() < roamLimit[ 2 ] || getY() > roamLimit[ 3 ]){ move(speed*(- 1 )); } } if (!getObjectsInRange( 150 , PC. class ).isEmpty()){ move(speed* 0 ); } else { turnTowards(player.getX(), player.getY()); move(speed); setRotation( 0 ); } if (!getObjectsInRange( 200 , PC. class ).isEmpty()){ aggro = true ; } } } |