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

2017/7/29

HELP!

SystemZero SystemZero

2017/7/29

#
I have this
public void looseHeal()
    {
    heal = heal-20;
    if (heal<=0) getWorld().removeObject(this);
   }
but here
Actor Agricultor1 = getOneObjectAtOffset(0,0,Agricultor1.class);
            if(Agricultor1!=null){
           MyWorld MyW=(MyWorld)getWorld();        
           looseHeal().Agricultor1;
    } 
is an error in the 4 line "not a statement" can somebody help me?
Super_Hippo Super_Hippo

2017/7/29

#
You want to call the method on the actor, so it has to be the other way around.
Agricultor1.looseHeal();
This will give you another error though because it can't find the method in the Actor class. You need to save the Agricultor1 variable as an Agricultor1, not as an Actor. And the variable name should start with a lowercase letter, so it is not the exact same as the class name.
Agricultor1 agri1 = (Agricultor1) getOneObjectAtOffset(0,0,Agricultor1.class);
if (agri1 != null)
{
    agri1.looseHeal();
}
SystemZero SystemZero

2017/7/30

#
        Agricultor1 agri1 = (Agricultor1) getOneObjectAtOffset(0,0,Agricultor.class);
        if (agri1 != null)
        {
            agri1.perdervida();
        }
I have this problem, "java.lang.ClassCastException: Agricultor cannot be cast to Agricultor1 at Tribu2.act(Tribu2.java:38) at guer.act(guer.java:19)"
Super_Hippo Super_Hippo

2017/7/30

#
You wrote Agricultor.class instead of Agricultor1.class. If the actual class name is Agricultor, then remove all instances of '1'.
SystemZero SystemZero

2017/7/30

#
Super_Hippo wrote...
You wrote Agricultor.class instead of Agricultor1.class. If the actual class name is Agricultor, then remove all instances of '1'.
It's because there are another class Agricultor, i need when a Agricultor and Angricultor1 intercepts, the method perdervida() start.
Super_Hippo Super_Hippo

2017/7/30

#
Agricultor agri = (Agricultor) getOneObjectAtOffset(0, 0, Agricultor.class);
Agricultor1 agri1 = (Agricultor1) getOneObjectAtOffset(0, 0, Agricultor1.class);
if (agri != null && agri1 != null)
{
    agri.perdervida();
    agri1.perdervida();
}
By the way, I usually use 'getOneIntersectingObject' instead of 'getOneObjectAtOffset', but you can find out yourself which one fits better to you scenario.
SystemZero SystemZero

2017/7/30

#
Super_Hippo wrote...
Agricultor agri = (Agricultor) getOneObjectAtOffset(0, 0, Agricultor.class);
Agricultor1 agri1 = (Agricultor1) getOneObjectAtOffset(0, 0, Agricultor1.class);
if (agri != null && agri1 != null)
{
    agri.perdervida();
    agri1.perdervida();
}
By the way, I usually use 'getOneIntersectingObject' instead of 'getOneObjectAtOffset', but you can find out yourself which one fits better to you scenario.
with that code, they loose heal when intercepts the same class, i need when two different class intercepts one of them loose heal.
Super_Hippo Super_Hippo

2017/7/30

#
Ah okay, I know what you mean:
//in Agricultor1 class
Agricultor agri = (Agricultor) getOneObjectAtOffset(0, 0, Agricultor.class);
if (agri != null)
{
    if (Greenfoot.getRandomNumber(2)==0) perdervida();
    else agri.perdervida();
}
SystemZero SystemZero

2017/7/30

#
//in Agricultor1 class

 Chaman cha = (Chaman) getOneObjectAtOffset(0, 0, Chaman.class);
if (cha != null)
{
    if (Greenfoot.getRandomNumber(2)==0) perdervida();
    else cha.perdervida();
}
I'm doing the same for the Chaman class, but there ir a error: "java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed. "
Super_Hippo Super_Hippo

2017/7/30

#
Make sure that this code is in the act method and you do not remove the object from the world before executing this code (or prevent this code from executing if it was removed).
You need to login to post a reply.