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

2012/12/13

Growing Game Code

wafflepudding wafflepudding

2012/12/13

#
Working on a basic game now, but I'm going to continue expanding it and adding new things as I learn. This is where I'll put any questions I have about it.
canLoseHealth = false;

//I want a delay right here.  Not like Greenfoot.delay() where the entire act stops, I just want a pause between these while everything else continues.

canLoseHealth = true;
I'm wondering if there are any preset methods that can do that, like 'wait(100)' or something. My other question is if there was any way to get an instance to always be rotated towards another instance. (For the purpose of following.) That's about it for now. I really appreciate the help.
danpost danpost

2012/12/14

#
canLoseHealth: what you need for this is to add an instance int field to count out the delay time (in frames -- aka act cycles). I will call the variable (aka field) 'delay'. When health is lost, set the counter to some positive number. In the act method (or a method it calls directly) add the following code
if(delay>0)
{
    delay--;
    canLoseHealth=(delay==0);
}
For a normal speed scenario, choose a number around 60 times the delay time in seconds. Also, you will have to put the condition on losing health that the field be set to 'true'.
danpost danpost

2012/12/14

#
turnTowards: there is an Actor class method called 'turnTowards' that takes screen location coordinates as parameters. You can either keep a reference to the object to turn toward in an instance field in the class that will be doing the following; or, if there is only one object of that type that is to be followed, you can get a reference to it with '(ClassName)getWorld().getObjects(ClassName.class).get(0)'.
wafflepudding wafflepudding

2012/12/14

#
Thanks, I made a delay method and all that. It says there are no errors, but when I play it he doesn't take any damage. I've looked at the code for a while and can't find anything wrong with it. What should I change? Oh, and delay is in act and all the variables and whatnot are created. Sorry for dumping out all this code.
    public void checkForVictim()
    {
        Victim theVictim = null;
        theVictim = (Victim)getOneIntersectingObject(Victim.class);
        if(theVictim != null)
        {
            if(canLoseHealth == true)
            {
                robotHealth --;
                canLoseHealth = false;
                if(delay == 0)
                {
                    canLoseHealth = true;
                    delay = 60;
                }    
            }
        }
    }
    public void checkForDeath()
    {
        if(robotHealth == 0)
        {
          getWorld().removeObject(this);
        }
    }    
    public void delay()
    {
        if (delay>0)
        {
            delay --;
        }    
    }
danpost danpost

2012/12/14

#
You have some code out of place. Lines 11 through 15 should be in the 'delay' method after 'delay--;'. Also, better would be to have the call to 'checkForDeath' at line 10 instead of in the 'act' method. Notice a pattern here. A value changes (delay or robotHealth) and we check its value for limits immediately. If 'checkForVictim' is not the last call that uses the location of the robot (using any of the following: 'getX', 'getY', getWorld() or any of the get...Object... commands), then place the following line after the 'checkForVictim' call in the 'act' method:
if(getWorld()==null)return;
You will get an IllegalStateException error if this happens before inserting the new line. It will say something like 'an attempt was made to use the actor's location while it is not in the world...'.
wafflepudding wafflepudding

2012/12/14

#
Nothing changed, I'm kind of worried I won't get this done in time because it's due Monday. I did all the stuff, putting the code up. I really appreciate the time you've put into this btw.
    public void act()
    {
        move();
        fire();
        delay();
        reloadDelayCount++;
        checkEdge();
        checkForVictim();
        if(getWorld()==null){return;}
    public void checkForVictim()
    {
        Victim theVictim = null;
        theVictim = (Victim)getOneIntersectingObject(Victim.class);
        if(theVictim != null)
        {
            if(canLoseHealth == true)
            {
                robotHealth --;
                checkForDeath();
                canLoseHealth = false;
            }
        }
    }
    public void checkForDeath()
    {
        if(robotHealth == 0)
        {
          getWorld().removeObject(this);
        }
    }    
    public void delay()
    {
        if (delay>0)
        {
            delay --;
                if(delay == 0)
                {
                    canLoseHealth = true;
                    delay = 60;
                }  
                
        }    
    }    
danpost danpost

2012/12/14

#
Since 'checkForVictim' is the last method call in the 'act' method, you do not need line 9 in the code. You are missing the close bracket that ends the 'act' method. And you have much more to this class than you have posted here.
wafflepudding wafflepudding

2012/12/14

#
Put in the bracket, took that line out of the act, still doesn't work. Anything else I should try?
You need to login to post a reply.