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

2014/3/26

Space game hit detection

zworrub zworrub

2014/3/26

#
Me and my mates are creating a space game and we are stuck on the hit detection we want are bullet(class called lazer) to when it hits the enemy(named enemy1) to both disappear when hit. Please may someone suggest some code
danpost danpost

2014/3/26

#
Basically (in pseudo-code): if can see other, eat other and remove self Does that maybe help? If not, review the Animal class code for help. It comes with the greenfoot download. You should be able to use the menubar to import the class by "Edit>Import class...".
zworrub zworrub

2014/3/26

#
I read somewhere that this is the code but it doesnt seem to work public void hitDetection() { Actor Lazer = getOneIntersectingObject(Enemy1.class); if( Lazer != null) { getWorld().removeObject(Lazer); getWorld().removeObject(this); } }
danpost danpost

2014/3/26

#
Although the name 'Lazer' does not reflect what the Actor object actually is (an Enemy1 class object), the code in itself should work. Maybe 'hitDetection' is not actually being called. Will need to see the 'act' method of this class (I presume it is the Lazer class). Come to think of it, there may be a more general type coding problem here. Please post the entire Lazer class. And, please use the 'code' link below the 'Post a reply' box to insert your code.
zworrub zworrub

2014/3/26

#
  public void act(){
       setLocation(getX()+15, getY());  
       if( getX() <=4 || getX() >= getWorld().getWidth()-2)
        {
                 getWorld().removeObject(this);
                 
                 return;
             
         }
        }
  
       
  public void hitDetection()  {
      
       Actor Lazer = getOneIntersectingObject(Enemy1.class);  
         
       if( Lazer != null)  
       {  
           getWorld().removeObject(Lazer);   
           getWorld().removeObject(this);
       }  
  
}
Sorry I am quite new to all of stuff haha
danpost danpost

2014/3/26

#
I do not see where 'hitDetection' is being called from.
zworrub zworrub

2014/3/26

#
Could you tell me how to do that please because my teacher really isn't helping me with this haha
danpost danpost

2014/3/26

#
Once an object is created, the only way for it to do anything (on its own) is by use of the 'act' method of its class. Everything else must be in some way linked to that act method. Call 'hitDetection' from the 'act' method of the class so that it will execute:
// insert between lines 9 and 10 above
hitDetection();
zworrub zworrub

2014/3/26

#
Thank you
You need to login to post a reply.