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

2013/9/24

Life Scenario Help

1
2
3
danpost danpost

2013/9/24

#
Please refer to my last couple of posts.
JasonZhu JasonZhu

2013/9/24

#
It's so confusing. Is there a different way to differentiate? And the male is giving birth after I did some testing.
danpost danpost

2013/9/24

#
'bug' refers to the male (if 'bug.isMale()' returns true); 'this' (which '!isOld()' is executed on -- same as '!this.isOld()') refers to the female (if 'isFemale()', which is equivalent to 'this.isFemale()', returns true). If you see my post with the 'checkPregnancy' method, you will see I separated the checks. In the act method, the female is checked for fertility before calling the method and the male is checked within the method.
JasonZhu JasonZhu

2013/9/25

#
Sorry for the late reply, but I think I understand now. Thanks for all your help guys!
JasonZhu JasonZhu

2013/9/25

#
    private void gestationPeriod()
    {
        gestationcycles++;
            if( gestationcycles == MONTH ) {
            gestationcycles = 0;
            gestation++;
                if( gestation == BIRTH_TIME ) {
                    getWorld().addObject(new Ladybug(0),getX(),getY());
                    gestation=0;
                    gestated=false;
                }
        }
    }
    
    private void becomingPregnant()      
    {      
        Ladybug bug = (Ladybug)getOneIntersectingObject(Ladybug.class);      
        if(bug != null && !bug.isOld() && bug.isAdult() && !isOld() && isAdult() && isFemale() && bug.isMale() && !isPregnant() && Greenfoot.getRandomNumber(100)<20){      
            gestated = true;  
        }
    }
    public boolean isPregnant()
    {
        return gestated;
    }
   
Ok, so after some testing on my code, everything is working perfectly except for 2 things: 1. Not knowing how to make it so that after the Ladybug isPregnant, it starts the cycle of gestationPeriod. I know Dan gave me an example of how to write this in a more efficient way, however, I want to know the easiest way to go about it while keeping the way my code is, or close to it anyway. 2. After the new Ladybug(0) spawns, it spawns for every Ladybug currently within the world, including the males. I have a feeling that this is realated to the "getX(),getY()", however I can't conclude. Also, with this being said, I have a feeling that my code does not consist of making it so that only the pregnant female Ladybugs could give birth. Once again, I know Dan gave me an example of how to write this in a more efficient way, however, I want to know the easiest way to go about it while keeping the way my code is, or close to it anyway. Thanks for all the help guys!
davmac davmac

2013/9/25

#
1. Not knowing how to make it so that after the Ladybug isPregnant, it starts the cycle of gestationPeriod
I gave you the solution to this earlier. You just need to call gestationPeriod() from act(), but only if isPregnant() returns true.
2. After the new Ladybug(0) spawns, it spawns for every Ladybug currently within the world, including the males.
Your check on line 18 includes isFemale() so you already make sure that only females become pregnant. You just need to avoid calling gestationPeriod() unless isPregnant() returns true, as I stated above.
JasonZhu JasonZhu

2013/9/25

#
        if (isPregnant() = true)
        {
            gestationPeriod();
        }
Sorry for asking the same questions, I'm still new to coding. I tried typing this in the act method, however, it errors out. I'm probably doing something stupid again. And I'm still having the problem of male Ladybugs spawning newborns.
davmac davmac

2013/9/25

#
You should use the code I gave you earlier.
    if (isPregnant()) {  
        gestationPeriod();  
    }  
You could also use this:
    if (isPregnant() == true) {  
        gestationPeriod();  
    }  
You need to use double-equals i.e. '==', which is an equality check, rather than '=', which is assignment.
JasonZhu JasonZhu

2013/9/25

#
Oh. That simple error... I need to start catching these myself. It's funny because I remember trying that before... Anyway... It's working exactly how I want now. I'd like to thank everyone who was kind in heart to give me advice!
You need to login to post a reply.
1
2
3