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

2013/9/25

Position Detecting Help

Y3llowMustang Y3llowMustang

2013/9/25

#
Hey so I've been attempting to make a heat seeking missile but I ran into a bit of trouble. If anyone knows how to fix this I would be grateful! :)
public void fly()
    {
        if (bodyFound == true)
        {
            if (getWorld().getObjects(Body.class).getX() > getX())
            {
                setLocation(getX() + 5, getY());
            }
        }
    }
"Can't find symbol- method getX()"
erdelf erdelf

2013/9/25

#
change Line 5 to
if(((Actor)getWorld().getObjects(Body.class).get(0)).getX()>getX())
danpost danpost

2013/9/25

#
The main thing that I see that is amiss is this: getObjects returns a List object and the List class does not have a getX method. Even if you got the first item from the List object with get(0), you would have an Object object and the Object class does not contain a getX method either. You need an Actor object or an object that is a subclass of the Actor class and it would need to be typecast as such; '((Actor)getWorld().getObjects(Body.class).get(0)).getX()'. I am presuming that 'bodyFound == true' means that at least one object of the Body class will be in the List returned using getObjects. If not, you will be an IndexOutOfBoundsException error. The other thing I noticed is you are only moving your missile to the right (unless you have more checks for the other directions). You might consider using the 'turnTowards' and 'move' methods for the missile movement.
Y3llowMustang Y3llowMustang

2013/9/26

#
erdelf wrote...
change Line 5 to
if(((Actor)getWorld().getObjects(Body.class).get(0)).getX()>getX())
Thanks, erdelf! Helped a lot finally I can move forward with progress made to my game <3.
Entity1037 Entity1037

2013/9/29

#
Try this:
        List bodies = getObjectsInRange(500, Body.class);
        if (!bodies.isEmpty()){
            Actor body = (Actor) bodies.get(0);
            int r = getRotation();
            turnTowards(body.getX(),body.getY());
            int pr = getRotation();
            setRotation(r);
            int a = r;
            for (int i=0; i<180; i++){
                a++;
                if (a==361){a=0;}
                if (a==pr){r+=5; break;}
            }
            a=r;
            for (int i=0; i<180; i++){
                a--;
                if (a==-1){a=360;}
                if (a==pr){r-=5; break;}
            }
            if (r>360){r-=360;}
            if (r<0){r+=360;}
            setRotation(r);
        }
        move(5);
That should be quite sufficient.
You need to login to post a reply.