So i tried to make grant2 always go to either the x or y coordinate of the next grant one
to determine who the next grant one was I used this post
Tricky situation... Get the nearest object
but there was an error here is my code and the error
java.lang.ClassCastException: class grant2 cannot be cast to class grant1 (grant2 and grant1 are in unnamed module of loader java.net.URLClassLoader @57092432)
at grant.a(grant.java:67)
at grant2.act(grant2.java:17)
at greenfoot.core.Simulation.actActor(Simulation.java:567)
at greenfoot.core.Simulation.runOneLoop(Simulation.java:530)
at greenfoot.core.Simulation.runContent(Simulation.java:193)
at greenfoot.core.Simulation.run(Simulation.java:183)
so yeah hope someone could help me out fast
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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | public double getDistance(Actor actor) { return Math.hypot(actor.getX() - getX(), actor.getY() - getY()); } public Actor getNearestActor() { List<Actor> nearActors = getObjectsInRange( 300 , Actor. class ); Actor nearestActor = null ; double nearestDistance = 300 ; double distance; for ( int i = 0 ; i < nearActors.size(); i++) { distance = getDistance(nearActors.get(i)); if (distance < nearestDistance) { nearestActor = nearActors.get(i); nearestDistance = distance; } } return nearestActor; } public grant() { GreenfootImage image = getImage(); image.scale(image.getWidth() / 2 , image.getHeight() / 2 ); setImage(image); b=System.currentTimeMillis();; } public void a( int range, int b) {Math.round(spin); List<grant> isi = getObjectsInRange(range, grant. class ); x=getX(); y=getY(); int c= 0 ; int spinn = ( int ) spin; getWorld().showText( "b" +spin, 400 , 400 ); if (!isi.isEmpty()) { grant1 grant1 = (grant1) getNearestActor(); spin =(Math.atan2(grant1.y-y,grant1.x-x)/Math.PI* 360 ); if (Math.abs(getX()-grant1.x)!= 0 ||Math.abs(getY()-grant1.y)!= 0 ) { if (Math.abs(getX()-grant1.x)<=Math.abs(getY()-grant1.y)) { if (getX()-grant1.x<= 0 ) {move( 1 ); } if (getX()-grant1.x>= 0 ) {move(- 1 ); } } if (Math.abs(getY()-grant1.y)<=Math.abs(getX()-grant1.x)) { if (getY()-grant1.y<= 0 ) { setLocation(getX(), getY()+ 1 ); } if (getY()-grant1.y>= 0 ) { setLocation(getX(), getY()- 1 ); } } } } |