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

2011/6/17

Testing and Changing Variables from a Collision

Lildarkone Lildarkone

2011/6/17

#
I am working on a simulation that involves breeding ants. I would like to find out a way to, when two ants collide, check if the other is male or female, and then, if M & F, create a new ant.
davmac davmac

2011/6/18

#
How do you track the sex of the ants? Do you use a variable, or do you have different subclasses (MaleAnt vs FemaleAnt)?
Lildarkone Lildarkone

2011/6/18

#
I would like to track it by setting a variable.
Lildarkone Lildarkone

2011/6/18

#
An emaple would be... if(getOneIntersectingObject(Ant.class)!=null) { }
Lildarkone Lildarkone

2011/6/18

#
How do I specifically read a variable from the ant I have collided with?
DonaldDuck DonaldDuck

2011/6/18

#
Believe that you need a reference to the specific ant, like so. All you need is a int called gender. Ant ant = (Ant) getOneIntersectingObject(Ant.class); if(ant != null) { if(ant.gender != this.gender) { getWorld().addObject(new Ant(), x, y); } }
Lildarkone Lildarkone

2011/6/18

#
Thanks, it seems to be working!
Lildarkone Lildarkone

2011/6/18

#
If you could give me any advice on how to improve the scenario, it would be much appreciated! http://www.greenfootgallery.org/scenarios/3081
You need to login to post a reply.