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

2017/8/18

Freezing Opponents

SwarmDark SwarmDark

2017/8/18

#
Hello. I have been working on trying to freeze/slow an opponent when they come in contact with one of my projectiles. However, enemies are randomly generated and I dont know how to figure out if the object hits them and how to freeze them. So far I have tried to look through similar cases, and have come up with something like this
 public void moveX(){
            
       if((Hydra)getOneInterectingObject(Hydra.class) != null){
            int Freeze = 100;
            
        }
       if (HoldTimer>0){
        
        HoldTimer = HoldTimer-10;
        
        }
        
       if(HoldTimer ==0){     
       x= x-2;
       X = getX();
        
       setLocation(x,500);
    }
    }
However I am unsure exactly how the intersecting method works with the randomised generation of enemies
Super_Hippo Super_Hippo

2017/8/18

#
It doesn't really matter if the enemies are randomly generated or not.
//Projectile class

//in act
//moving projectile
Enemy enemy = (Enemy) getOneIntersectingObject(Enemy.class); //Enemy should be replaced with Hydra if I get this right
if (enemy != null)
{
    enemy.freeze();
    getWorld().removeObject(this);
}
//Enemy class
private int freezeTimer = 0;

//in act
if (freezeTimer > 0)
{
    freezeTimer--;
    //move with lower speed
}
else
{
    //move with regular speed
}


public void freeze()
{
    freezeTimer = 100;
}
SwarmDark SwarmDark

2017/8/20

#
Ohh-ok thanks for the help. I think when I looked through other peoples code I got confused by how invoking methods and constructors from different actors worked. I should have also probably coded this for the overarching enemy class, seeing as all the actors have the exact same traits. Thanks alot for your help though, I really appreciate it.
You need to login to post a reply.