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

2017/3/11

the "turnTowards()" method does not work

fnole12 fnole12

2017/3/11

#
Hey guys i need your help. This is the Code for a class which is supposed to turn towards an instance of the "ressourcen" class. My debugger shows me that the code works for the most part. The console always shows the right numbers and the if-methode within the while-methode always gets triggered in the right moment and even the System.out.print()-methodes in the if-methode are working correctly. But the turnTowards()-methode after those is just not working. This is the full class:
import greenfoot.*;
public class Arbeiter_smart extends Actor
{
    private int Xloc;
    private int Yloc;
    Ressourcen ressourcen;
    public void act() 
    {
        // Add your action code here.
    } 
    public void findRessources_Fnole()
    {
        int Xloc = getX();
        int Yloc = getY();
        System.out.println(Xloc);
        System.out.println(Yloc);
        
        int iRZ = 0;
        while (iRZ < 18)
        {
         Actor ressourcen = (Actor)getWorld().getObjects(Ressourcen.class).get(iRZ);
         int rX = ressourcen.getX(); 
         int rY = ressourcen.getY();
         System.out.println(rX);
         System.out.println(rY);
         System.out.println((rX - Xloc)*(rX - Xloc) + (Yloc - rY)*(Yloc - rY));
         if(((rX - Xloc)*(rX - Xloc) + (Yloc - rY)*(Yloc - rY)) <= 10000)
         {
            System.out.println(rX);
            System.out.println(rY);
            turnTowards(23,356);
         }
         else
         {
             iRZ++;
         }
        }
    }
    public void turn()
    {turnTowards(23, 235);
    }
}
Super_Hippo Super_Hippo

2017/3/11

#
I am not quite sure what you are calculating in line 27. Is it wanted that you turn to (23|356) and not to (rX|rY)? I guess the findRessourcen_Fnole is executed from somewhere else, right? Btw, I think you should only turn to one Ressourcen object and not to the 18th one after also calculating the first 17. Maybe you could just turn to the nearest:
Actor a=null; int distance=10001; //well, probably something less than 10000
for (Actor ress : getObjectsInRange(10000, Ressourcen.class))
{
    int dist = Math.sqrt(Math.pow(getX()-ress.getX(),2)+Math.pow(getY()-ress.getY(),2));
    if (dist < distance)
    {
        dist = distance;
        a = ress;
    }
}

if (a != null)
{
    turnTowards(a.getX(), a.getY());
}
davmac davmac

2017/3/11

#
I'm thinking that "turnTowards(23,356);" is supposed to be "turnTowards(rX,rY);"? If not, could you explain what you mean by "not working" - what are you expecting it to turn towards, and what happens instead?
fnole12 fnole12

2017/3/11

#
@Super_Hippo Thanks for answering apperently i am fairly new to Greenfoot and Java in general so i have a these questions: 1. What does "ress" stand for, in your code? 2. What is "a" representing?
fnole12 fnole12

2017/3/11

#
@davmac the Actor is supposed to turn towards the ressourcen object to be able to move in its direction later. But the Actor just doesn't turn. and Yes, you're right. it is supposed to be "turnTowards(rX,rY)" it was just a placeholder
Super_Hippo Super_Hippo

2017/3/11

#
It is a for each loop. 'ress' is the current Ressourcen object from the List which was returned by the getObjectsInRange method. So it starts with the first object in the list, then the second and so on. 'a' is the Ressourcen object in the list which is the nearest to the Arbeiter_smart.
fnole12 fnole12

2017/3/11

#
Thanks again Hippo but i'm still getting an Error in line 4 from your code: incompatible Types: Possible lossy conversion from double to int
Super_Hippo Super_Hippo

2017/3/11

#
int dist = (int) Math.sqrt(Math.pow(getX()-ress.getX(),2)+Math.pow(getY()-ress.getY(),2));
fnole12 fnole12

2017/3/11

#
thx
You need to login to post a reply.