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

2017/6/24

I nedd help with isTouching()

SystemZero SystemZero

2017/6/24

#
Hello everybody, I'm from Colombia and i'm doing a proyect for my university. The proyect is abour two armies that they fight, when two soldiers of a different armie are in a box, there are a 50% probability that one of they lose 20 hp. I need help because I dont know how make the "If" one soldier isTouching() another soldier, one of they lose 20hp. Thank you very much!
Super_Hippo Super_Hippo

2017/6/24

#
It could look like that. However, you probably have to make sure that only one "touch" is registered or they will fight until one of them dies.
Soldier soldier = (Soldier) getOneIntersectingObject(Soldier.class);
if (soldier != null)
{
    if (Greenfoot.getRandomNumber(2)==0) loseHealth(20); //take 20 damage
    else soldier.loseHealth(20); //deal 20 damage
}
public void loseHealth(int amount)
{
    health -= amount;
    if (health<=0) getWorld().removeObject(this);
}
SystemZero SystemZero

2017/6/24

#
Super_Hippo wrote...
It could look like that. However, you probably have to make sure that only one "touch" is registered or they will fight until one of them dies.
Soldier soldier = (Soldier) getOneIntersectingObject(Soldier.class);
if (soldier != null)
{
    if (Greenfoot.getRandomNumber(2)==0) loseHealth(20); //take 20 damage
    else soldier.loseHealth(20); //deal 20 damage
}
public void loseHealth(int amount)
{
    health -= amount;
    if (health<=0) getWorld().removeObject(this);
}
Hey! Thank you very much! But now I have a problem, when two soldiers of the same army are in a box, they lose Health, do you know how I can correct it?
danpost danpost

2017/6/24

#
If your armies are from different subclasses of Soldier, then you can put the condition:
if (soldier.getClass() != this.getClass())
on losing health.
SystemZero SystemZero

2017/6/24

#
danpost wrote...
If your armies are from different subclasses of Soldier, then you can put the condition:
if (soldier.getClass() != this.getClass())
on losing health.
To differentiate them i do:
public Soldier (int id, int direccion){
If the ID= 1 they have a different sprite than if the ID = 2. But all the Soldiers are in the same subclass.
danpost danpost

2017/6/25

#
SystemZero wrote...
If the ID= 1 they have a different sprite than if the ID = 2. But all the Soldiers are in the same subclass.
Then, use the following condition::
if (soldier.ID != this.ID)
SystemZero SystemZero

2017/7/14

#
danpost wrote...
SystemZero wrote...
If the ID= 1 they have a different sprite than if the ID = 2. But all the Soldiers are in the same subclass.
Then, use the following condition::
if (soldier.ID != this.ID)
Luchador soldado = (Luchador) getOneIntersectingObject(Luchador.class);
        if (soldado != null) 
        {
            if (Luchador.id != this.id){
            if (Greenfoot.getRandomNumber(2)==0) {
                perderVida(20); 
            }
            else soldado.perderVida(20); 
          }
        }
I have this, but there is a problem, can you give me a solution?
Super_Hippo Super_Hippo

2017/7/14

#
It probably should be 'soldado.id' instead of 'Luchador.id' in line 4.
SystemZero SystemZero

2017/7/16

#
Super_Hippo wrote...
It probably should be 'soldado.id' instead of 'Luchador.id' in line 4.
No, still the same error: "cannot find symbol - variable id"
 Luchador soldado = (Luchador) getOneIntersectingObject(Luchador.class);
        if (soldado != null) 
        {
            if (soldado.id != this.id){
            if (Greenfoot.getRandomNumber(2)==0) {
                perderVida(20); 
            }
            else soldado.perderVida(20); 
          }
        }
Super_Hippo Super_Hippo

2017/7/16

#
Is there a 'id' variable in the Luchador class? (I mean a 'private int id' field and not just the int you pass to the constructor.)
SystemZero SystemZero

2017/7/16

#
Super_Hippo wrote...
Is there a 'id' variable in the Luchador class? (I mean a 'private int id' field and not just the int you pass to the constructor.)
public class Luchador extends Actor
{
    /**
     * Act - do whatever the Luchador wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
      public static int NORTE = 270;
    public static int SUR = 90;
    public static int ESTE = 0;
    public static int OESTE = 180;
    public static int NORESTE = 315;
    public static int SURESTE = 45;
    public static int NOROESTE = 225;
    public static int SUROESTE = 135;
    public int i=1;
    public int vida = 100;
     public Luchador (int direccion, int id){
       
        if (direccion == 1){
            setRotation(NORTE);
        }
        if (direccion == 2){
             setRotation(SUR);
            }
            if (direccion == 3){
            setRotation(ESTE);
        }
        if (direccion == 4){
            setRotation(OESTE);
        }
        if (direccion == 5){
             setRotation(NORESTE);
            }
        if (direccion == 6){
            setRotation(SURESTE);
        }
        if (direccion == 7){
            setRotation(NOROESTE);
        }
        if (direccion == 8){
            setRotation(SUROESTE);
        }
        
    }
   
    
    public void act() 
    {
        move(i);
        //CUADRANTE SUR-ESTE
        if ((getRotation()<=90)&& (getRotation()>=0)){
            if (getX()==getWorld().getWidth()-1){
             i=-1; 
            }    
            if (getX()==0){
                i=1;
            }
            if (getY()==0){
                i=1;
            }
            if (getY()==getWorld().getHeight()-1){
                i=-1;
            }
        }
        //CUADRANTE SUR-OESTE
        if ((getRotation()<=180)&& (getRotation()>=90)){
            if (getX()==getWorld().getWidth()-1){
             i=1; 
            }    
            if (getX()==0){
                i=-1;
            }
            if (getY()==0){
                i=1;
            }
            if (getY()==getWorld().getHeight()-1){
                i=-1;
            }
        }
        //CUADRANTE NORTE-ESTE
        if ((getRotation()<=0)&& (getRotation()>=270)){
            if (getX()==getWorld().getWidth()-1){
             i=1; 
            }    
            if (getX()==0){
                i=-1;
            }
            if (getY()==0){
                i=-1;
            }
            if (getY()==getWorld().getHeight()-1){
                i=1;
            }
        }
        //CUADRANTE NORTE-OESTE
        if ((getRotation()<=270)&& (getRotation()>=180)){
            if (getX()==getWorld().getWidth()-1){
             i=1; 
            }    
            if (getX()==0){
                i=-1;
            }
            if (getY()==0){
                i=-1;
            }
            if (getY()==getWorld().getHeight()-1){
                i=1;
            }
        }
        
        //DIAGONALES
        if(getRotation()==315){
            if (getY()==0){
                i=-1;
            }
            else if (getX()==0){
                i=1;
            }
            if (getY()==getWorld().getHeight()-1){
                i=1;
            }
            else if (getX()==getWorld().getWidth()-1){
                i=-1;
            }
        }
        
        Luchador soldado = (Luchador) getOneIntersectingObject(Luchador.class);
        if (soldado != null) 
        {
            if (soldado.id != this.id){
            if (Greenfoot.getRandomNumber(2)==0) {
                perderVida(20); 
            }
            else soldado.perderVida(20); 
          }
        }
          
          
        
        
    }
     
    public void perderVida(int cantidad)
    {
        
        vida = vida - cantidad;
        if (vida<=0){
            getWorld().removeObject(this);
        }
    } 
}
This is my Luchador class. I ask two parameters: id and direccion.
Super_Hippo Super_Hippo

2017/7/16

#
//add this
private int id;

//and in line 18
this.id = id;
Btw, I think you can simplify your act method to:
move(i);
if (getX()==0 || getY()==0 || getX()==getWorld().getWidth()-1 || getY()==getWorld().getHeight()-1) i *= -1;
You need to login to post a reply.