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

2021/10/6

I need help whit accessing a Boolean/int From Another Actor Class

Monk_08 Monk_08

2021/10/6

#
i got a konami code for my game as an Easter egg an if you type it correct an int increases by 1 or a boolean gets set to true its not important if its a boolean or int for me
private static final String[] konamiKeys =
        {
            "up", "up", "down", "down",
            "left", "right", "left", "right",
            "b", "a", "enter"
        };

    private int keyIndex = 0;

    public void konami()
    {
        String key = Greenfoot.getKey();
        if (key != null) 
        {
            if (konamiKeys[keyIndex].equals(key)) // is next key in sequence
            {
                if (++keyIndex == konamiKeys.length) // is last key in sequence
                {
                    //rCount = true;
                    //rCount = 1;
                    keyIndex = 0;
                }
            }
            else // not in sequence -- start over
            {
                keyIndex = 0;
            }
        }
    }

    private int pngCount = 0 ;
    //public int rCount = 0;
    //public boolean rCount = false;
    
    
    public void delayCounting()
    {

        pngCount++;

        if(pngCount == 14)
        {
            pngCount = 0;
        }
    }

    void Rainbow()
    {
        if(rCount == true or rCount == 1){
            if(pngCount == 2)
            {
                setImage("ACO.png"); 
            }
            if(pngCount == 4){
                setImage("ACY.png"); 
            }
            if(pngCount == 6){
                setImage("ACG.png");
            }
            if(pngCount == 8){
                setImage("ACLB.png");  
            }
            if(pngCount == 10){
                setImage("ACB.png"); 
            }
            if(pngCount == 12){
                setImage("ACP.png");
            }
            if(pngCount == 14){
                setImage("abenteurer.png");  
            }

            if ((isTouching(Waechter.class)))
            {
                GameEnd myImage = new GameEnd ();
                World w = getWorld();
                w.addObject(myImage, 9, 9);
                myImage.setImage("GG.png");
                Greenfoot.stop();
            }
        }
    }
}
but now i want to use the int/boolean in another actor so when you typed the konami code the weachter is no longer able to (kill = toeten) the abenteurer but rCount is not a declared variable in the actor weachter so how can i access a Boolean/int From Another Actor Class public void Toeten() { if(rCount == false or rCount == 0){ if ((isTouching(Abenteurer.class))) { GameEnd myImage = new GameEnd (); World w = getWorld(); w.addObject(myImage, 9, 9); myImage.setImage("WG.png"); Greenfoot.stop(); } } }
Monk_08 Monk_08

2021/10/6

#
i fund this but i dint know if it is the right thing for me to use and how to implement it https://www.greenfoot.org/topics/5738 <-- where i found the info If it is a single int and it is being held in a unique instance I would do something like
class ZZZ
{
private static int thatThingYouWant;
 
public void setThatThingYouWant(int val)
{
thatThingYouWant = val;
}
 
public int getThatThingYouWant()
{
return thatThingYouWant;
}
}
with it being a static member, you can read this value anywhere by: someint = ZZZ.getThatThingYouWant(); and you can set it by ZZZ.setThatThingYouWant(someint); This should be okay for straight forward linear processing.
danpost danpost

2021/10/6

#
The proper way is:
private int thatThingYouWant;

// to allow value to be accessed from elsewhere
public int getThatThingYouWant()
{
    return thatThingYouWant;
}

// to allow value to be set from elsewhere
public void setThatThingYouWant(int value)
{
    thatThingYouWant = value;
}
with:
if (isTouching(Abenteurer.class))
{
    Abenteurer abenteurer = (Abenteurer)getOneIntersectingObject(Abenteurer.class);
    int value = abenteurer.getThatThingYouWant();
}
Monk_08 Monk_08

2021/10/6

#
Wow that’s stupidly logical will try later thanks for the help danpost
Monk_08 Monk_08

2021/10/8

#
Okay so I now want to use it for a different project where when an enemy touches the game end zone the player lose one life and the enemy gets deleted but im not getting it to work setting the value from the actor enemy to the actor player here the code in actor player :
private int playerLives;

    // to allow value to be accessed from elsewhere
    public int getplayerLives()
    {
        return playerLives;
    }

    // to allow value to be set from elsewhere
    public void setplayerLives(int value)
    {
        playerLives = value;

    }
here the code in the actor enemy: public void dead() { if(isTouching(Game_End.class)){ getWorld().removeObject(this); playerLives--; } } Thanks
danpost danpost

2021/10/8

#
if (isTouching(Game_End.class){
    World world = getWorld(); // to retain reference to world (see next line comment)
    world.removeObject(this); // after this executes getWorld will return a 'null' value
    Player player = (Player)world.getObjects(Player.class).get(0); // get reference to player
    player.setPlayerLives(player.getPlayerLives()-1); // access and set player lives
}
Monk_08 Monk_08

2021/10/10

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