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

2014/3/29

check if two objects are touching each other?

trias95 trias95

2014/3/29

#
Hey there, so im new to greenfoot. im studying programming at my local college and i have an assignment due for sunday in which i need to make a 2d top down zombie game. i have the game working however i need to see if one object is touching another in order to trigger a graphic. The idea is that the zombie will drop an power up and when the survivor touches it, he gains a power. that all works. the problem lies in making the graphic work. I cant have the graphic as a subclass of the player as when i do this, the zombies chase the graphic. Is there some code to check if one actor is touching another and if not, how can i edit this zombie code to only chase the survivor class and not the subclasses?
public class Zombie extends Actor
{
    /**
     * Act - do whatever the Zombie wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
       followHuman();
       move(1);
       
       Actor Survivor;
       Survivor = getOneObjectAtOffset(0,0, Survivor.class);
       if (Survivor != null)
       {
           World world;
           world = getWorld();
           world.removeObject(Survivor);
           
                
                Greenfoot.stop();  
                return; 
        }  
    }
           
        
       
     
      public void followHuman()  
   {  
   double dist = 800;  
    Actor closest = null;  
    if(!getObjectsInRange(800, Survivor.class).isEmpty())  
    {  
        for (Object obj: getObjectsInRange(800, Survivor.class))  
        {  
            Actor guy = (Actor) obj;  
            double guyDist = Math.hypot(guy.getX() - getX(), guy.getY() - getY());  
            if (closest == null || guyDist< dist)  
            {  
                closest = guy;  
                dist = guyDist;  
            }  
        }  
        turnTowards(closest.getX(),closest.getY());  
    }     
   }  
}
Thanks in advance :)
danpost danpost

2014/3/29

#
If ClassA subclasses ClassB, then you should be able to say the 'ClassB is a type of ClassA'. If this graphic is not a type of Survivor, then is should not subclass it. Examples: Church is a type of Building; MountainBike is a type of Bicycle; Corvette is a type of SportsCar; SportsCar is a type of Car; Factory is a type of Building; CarFactory is a type of Factory; Pistol is a type of Gun; Bullet is a type of Ammunition (not a type of Gun). I have a strong suspicion that this graphic is not a type of Survivor.
trias95 trias95

2014/3/29

#
here is a part of my "Survivor" class
 private void touching()
    {
          Actor ak47;
         ak47 = getOneObjectAtOffset(0,0, ak47.class);
        if (ak47 != null)
         {
           World world;
           world = getWorld();
           world.removeObject(ak47);
           
           if (gunType != 1 )
           {
               gunType = 1;
            }
          
    } private void touching()
    {
          Actor ak47;
         ak47 = getOneObjectAtOffset(0,0, ak47.class);
        if (ak47 != null)
         {
           World world;
           world = getWorld();
           world.removeObject(ak47);
           
           if (gunType != 1 )
           {
               gunType = 1;
            }
          
    }
thats the bit that sets "gunType" to 1 when survivor touches the ak47 powerUp. What i want the graphic to do is to enable itself when it too see's that gunType is 1 and to do that, it needs to be a subclass of it so it can see when that gunType changes but when its a subclass, the zombies chase the graphic actor as well as the survivor actor. Id like them to ignore the graphic actor. i understand what you're saying and youre right in that its not a type of survivor however i am subclassing it to allow it to see when the int changes to 1
trias95 trias95

2014/3/29

#
OKAY! something has just clicked for me. i can spawn the graphic in the desired location when i touch the power up with the code!
world.akgraphic akgraphic = new akgraphic();
        addObject(akgraphic, 138, 574);
dont know why i didnt see that before -__- Now just to try to get it to overwrite time to the existing power up. EG if i pick up a power up and it lasts for 10 seconds and during that 10 seconds i pick up another it overwrites the original time. so turning 6 seconds left into 10.
You need to login to post a reply.