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

2015/10/26

Referring to another Actor's variables

EggsD EggsD

2015/10/26

#
So I am trying to refer to a boolean, SHIELD, in another Actor. Here is the Actor I am trying to get the boolean into:
import greenfoot.*; 

public class Enemy extends Scrollers
{import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class Enemy extends Scrollers
{    
    public void kill()
    {
        if(isTouching(Hero.class) == true)
        {
            removeTouching(Hero.class);
            DeathScreen go = new DeathScreen();
            Greenfoot.setWorld(go);
        }
    }
}
And this is the Actor with SHIELD I am trying to get it from.
import greenfoot.*;

public class Hero extends Actor
{
    public boolean SHIELD = false;
}
I've tried all sorts of stuff but as a Greenfoot Novice I'm rather lost. Any help would be greatly appreciated.
Super_Hippo Super_Hippo

2015/10/26

#
I guess you failed with the copy&paste in the first class, right? The first few lines are there twice. It could look like this:
public void kill()
{
    Hero h = (Hero) getOneIntersectingObject(Hero.class);
    if (h != null)
    {
        if (h.shield) //since the variable isn't 'final', it shouldn't be uppercase
        {
            h.removeShield(); //add this method in the 'Hero' class. If you only change the boolean (and not an image showing the shield or something), you can use 'h.shield = false;'
        }
        else
        {
            //getWorld().removeObject(h); //if you change the world, then you don't need to remove the hero first (unless you change back to this world)
            Greenfoot.setWorld(new DeathScreen());
        }
    }
}
EggsD EggsD

2015/10/26

#
Ok so that fixed the error I was getting. Thanks. Only now my issue seems to be that when my hero picks up the powerup to give him the shield, it doesn't seem to change my boolean shield value. Here's the code: For the hero:
public void getShield()
    {
        if (isTouching(Shield.class) == true)
        {
            removeTouching(Shield.class);
            shield = true;
        }
    }
shield is defined earlier as "public boolean shield = false" Here is my enemy kill() code:
public void kill()
    {
        Hero h = (Hero) getOneIntersectingObject(Hero.class);
        if (h != null)
        {
            if ((h.shield) == false)
            {
                Greenfoot.setWorld(new DeathScreen());
            }
        }
    }
Thanks for the help!
EggsD EggsD

2015/10/26

#
Just kidding! Fixed it. Thanks again!
EggsD EggsD

2015/10/26

#
Sorry to ask again but I tried to put the shield variable on a timer so that it expires eventually but it isn't ever changing back.
public int timer;
    
    public void getShield()
    {
        if (isTouching(Shield.class) == true)
        {
            removeTouching(Shield.class);
            timer = 0;
            shieldAnimation();            
        }
    }

    public void shieldAnimation()
    {
        if (timer < 10)
        {
            timer++;
            shield = true;
            setImage("ShieldMan.png");            
        }
        if (timer >= 10)
        {
            timer++;
            shield = false;
            setImage("Man01.png");
        }        
    }
danpost danpost

2015/10/26

#
You are trying to have the timer run inside the 'shieldAnimation' method; but, it is only called when the 'getShield' method detects that the actor is touching a Shield object.
You need to login to post a reply.