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

2012/11/9

getObjectsInRange()

Rodeurhalt Rodeurhalt

2012/11/9

#
Hi ! I need some help for getting coordonate X and Y from an element of a list made by getObjectsInRange() My actor here (Flamme) , needs to have the X and Y from another actor (Bateau) to move towards this actor and destroy it, but I can't find the right code to have this coordinates. Could you help me ? Here is my code :
import greenfoot.*;

/**
 * Projectile infligeant des dégats aux ennemis.
 * 
 * @author -
 * @version 1.0.0
 */
public class Flamme extends Dragon
{
    /**
     * Le projectile se dirige vers sa cible.
     */
    public void act() 
    {
        move(5);
    }   

    private void cible()
    {
        java.util.List lescibles = getObjectsInRange(250, Bateau.class);
        Object cible = lescibles.get(0);
        turnTowards(cible.getX(), cible.getY());
    }

    
}
An error come after the cible.getX() : cannot find symbol- method getX() I understood that it's because cible is just an element of the list and doesn't have coordinates but I don't know how to get them. (P.S : I'm swiss and speak french so sorry for my english )
Rodeurhalt Rodeurhalt

2012/11/9

#
Hum in the code I didn't put the methods cible() in the act(), but now it's corrected ;)
danpost danpost

2012/11/10

#
The error does not arise because cible is just an element of the list. It arises because 'getX()' and 'getY()' are methods in the 'Actor' class (not the Object class). Change line 22 to:
Actor cible = (Actor) lescibles.get(0);
If there is a chance that 'cible' is not in the world, you must make sure that the list is not empty first.
Rodeurhalt Rodeurhalt

2012/11/10

#
Thank you ! It works well now, but could you tell me here :
Actor cible = (Actor) lescibles.get(0); 
why must we write the second Actor in brackets ?
SPower SPower

2012/11/10

#
Because you cast an object of the class Object, into an object of the class Actor. Casting means converting, but it only works if it's really a kind of that class if you understand what I mean :)
danpost danpost

2012/11/10

#
The list 'lescibles', when created, was populated with elements of the Object class. The variable 'cible' is declared to hold an 'Actor' class object. In order to set it with an element from the list, the element must be 'cast' to that of which the variable holds (or the compiler will complain about it). The statement would have worked just as well if both occurances of 'Actor' were changed to 'Bateau'; since objects of that class inherit Actor methods.
Rodeurhalt Rodeurhalt

2012/11/10

#
Oh okay ! Thank you very much for your help ! :) I have another question about "life", if you have time could you answer it please ? Here is my question : I want to remove an object "Bateau" of the world ,if his variable "life" ,who is created with a value of 3 , equals 0. The variable life will loose 1 at eatch intersection with my actor Flamme, that I just finished to create with your help. I tried to code what I explain here :
    public boolean intersect(Class clss)
    {
        Actor actor = getOneIntersectingObject(clss);
        return actor != null;
    }


    public void vie()
    {
        if(intersect(Flamme.class))
        {
            vie = vie-1;
        }
        if( vie == 0)
        {
            getWorld().removeObject(this);
        }
    }
But when the life of my object "Bateau" would be at 0, it didn't disappear. :/ I don't know why, so could you maybe explain me the reason if you know it ?
Rodeurhalt Rodeurhalt

2012/11/10

#
Oh sorry, i didn't translate it but vie = life And by the way you don't have all the code, but the variable 'vie' has been created and equals to 3 first don't worry ! :P
danpost danpost

2012/11/10

#
There is not enough information (code) here to determine why the object does not disappear. Might need to see the act method in the Bateau class as well. As a side note, the 'vie()' method (if everything else was coded correctly) should remove the object the first time it intersects the Flamme object. The reasoning is that 3 act cycles (which is normally around 0.05 seconds) is such a short amount of time that, unless the Flamme object is removed from the world immediately upon capture of its state of intersecting the object, the 3 lives will be removed in a heartbeat; but still, the Bateau object should have been removed at that point.
Rodeurhalt Rodeurhalt

2012/11/10

#
Hmm ok i'll try to fix that if I can thank you a lot
You need to login to post a reply.