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

2019/4/5

Need help: Passing variable from world class to actor class

Sploit Sploit

2019/4/5

#
Hey guys I need some help in this project. I wanna pass a variable (boolean) called "gameActive" in the actor class and "bgSelected" in the world class from a my World-SubClass "Arena" to an Actor-Class "PlayerOne". I tried different ways with getter and setter, because the variable shall be private, but nothing is working fine. I always get a null point exception error :( Mby you can help me to find the problem and fix it. Code of PlayerOne-Class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class playerOne here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class PlayerOne extends Players
{
    //Arena arena = (Arena) getWorld();
    
    //private boolean gameActive = false;
    private boolean gameStart = true;
    private int gameActivated = 0;
    
    boolean gameActive = ((Arena) getWorld()).getBGSelected();
    
    /**
     * Act - do whatever the playerOne wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if (gameStart == true) {
            this.getImage().clear();
            gameStart = false;
        }
        if (gameActivated == 0) {
            //bgSelected();
            //booleanSetter();
        }
        if (gameActive == true) {
            this.setImage("fig1.png");
            gameActive = false;
            this.getWorld().showText("Yeah", 100, 100);
        }
    }
    
    //public void booleanSetter() {
    //    if (bgSelected() == true && gameActivated == 0) {
    //        gameActive = true;
    //        gameActivated = 1;
    //    }
    //}    
    
    //public boolean bgSelected() {
    //    Arena arena = new Arena();
    //    return arena.bgSelected;
    //}    
}
Code of Arena-Class:
// WARNING: This file is auto-generated and any changes to it will be overwritten
import lang.stride.*;
import greenfoot.*;

/**
 * Write a description of class Arena here.
 * @author (your name) @version (a version number or a date)
 */
public class Arena extends World
{
    /**
     * Constructor for objects of class Arena.
     */
    public Arena()
    {
        super(1280, 720, 1);
        /* Create a new world with 1280x720 cells with a cell size of 1x1 pixels. Ojekte hinzufügen*/
        this.addObject( new ExtraLive(), 647, 165);
        this.addObject( new PlayerOne(), 60, 650);
        this.addObject( new PlayerTwo(), 1220, 650);
    }
    
    private int pressCooldown = 0;
    private int cooldownRate = 15;
    private int pOneSelection = 4;
    private boolean pOneSelectionCompleted = false;
    private int pTwoSelection = 4;
    private boolean pTwoSelectionCompleted = false;
    private boolean bgSelected = false;
    
    /**
     * Act - do whatever the PlayerSelection wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if (pOneSelectionCompleted == false) {
            aPressed();
            dPressed();
            sPressed();
            selectionFixOne();
            playerOneSelection();
        }
        
        if (pOneSelectionCompleted == true && pTwoSelectionCompleted == false) {
            leftPressed();
            rightPressed();
            downPressed();
            selectionFixTwo();
            playerTwoSelection();
        }
        
        if (pOneSelectionCompleted && pTwoSelectionCompleted && bgSelected == false) {
            bgSelected = true;
            int bgRandom = Greenfoot.getRandomNumber(5);
            switch (bgRandom) {
                case 0:
                this.setBackground("BG1.png");
                break;
                case 1:
                this.setBackground("BG2.png");
                break;
                case 2:
                this.setBackground("BG3.png");
                break;
                case 3:
                this.setBackground("BG4.png");
                break;
                case 4:
                this.setBackground("BG5.png");
                break;
                default:
                this.setBackground("BG1.png");
            }
        }    
    }
    
    //BG-Selected
    public boolean getBGSelected() {
        boolean var = bgSelected;
        return var;
    }    
    
    //Player 1
    public void playerOneSelection() {
        switch (pOneSelection) {
            case 1: 
            this.setBackground("P1C1.png");
            break;
            case 2:
            this.setBackground("P1C2.png");
            break;
            case 3:
            this.setBackground("P1C3.png");
            break;
            case 4:
            this.setBackground("P1C4.png");
            break;
            case 5:
            this.setBackground("P1C5.png");
            break;
            case 6:
            this.setBackground("P1C6.png");
            break;
            case 7:
            this.setBackground("P1C7.png");
            break;
            default:
            this.setBackground("P1C4.png");
        }
    }    
    
    public void aPressed() {
        if (Greenfoot.isKeyDown("A") && pressCooldown < 0) {
            pOneSelection--;
            pressCooldown = cooldownRate;
        }
        else {
            pressCooldown--;
        }    
    }
    
    public void dPressed() {
        if (Greenfoot.isKeyDown("D") && pressCooldown < 0) {
            pOneSelection++;
            pressCooldown = cooldownRate;
        }
        else {
            pressCooldown--;
        }
    }
    
    public void sPressed() {
        if (Greenfoot.isKeyDown("S")) {
            pOneSelectionCompleted = true;
        }    
    }
    
    public void selectionFixOne() {
        if (pOneSelection < 1) {
            pOneSelection = 7;
        }
        else if (pOneSelection > 7) {
            pOneSelection = 1;
        }    
    }    
    
    //Player 2
    public void playerTwoSelection() {
        switch (pTwoSelection) {
            case 1: 
            this.setBackground("P2C1.png");
            break;
            case 2:
            this.setBackground("P2C2.png");
            break;
            case 3:
            this.setBackground("P2C3.png");
            break;
            case 4:
            this.setBackground("P2C4.png");
            break;
            case 5:
            this.setBackground("P2C5.png");
            break;
            case 6:
            this.setBackground("P2C6.png");
            break;
            case 7:
            this.setBackground("P2C7.png");
            break;
            default:
            this.setBackground("P2C4.png");
        }
    }
    
    public void leftPressed() {
        if (Greenfoot.isKeyDown("Left") && pressCooldown < 0) {
            pTwoSelection--;
            pressCooldown = cooldownRate;
        }
        else {
            pressCooldown--;
        }
    }
    
    public void rightPressed() {
        if (Greenfoot.isKeyDown("Right") && pressCooldown < 0) {
            pTwoSelection++;
            pressCooldown = cooldownRate;
        }
        else {
            pressCooldown--;
        }
    }
    
    public void downPressed() {
        if (Greenfoot.isKeyDown("Down")) {
            pTwoSelectionCompleted = true;
        }    
    }
    
    public void selectionFixTwo() {
        if (pTwoSelection < 1) {
            pTwoSelection = 7;
        }
        else if (pTwoSelection > 7) {
            pTwoSelection = 1;
        }    
    }   
}
Here is the error log:
java.lang.NullPointerException
	at PlayerOne.<init>(PlayerOne.java:17)
	at Arena.<init>(Arena.java:19)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
	at greenfoot.core.Simulation.newInstance(Simulation.java:580)
	at greenfoot.platforms.ide.WorldHandlerDelegateIDE.lambda$instantiateNewWorld$0(WorldHandlerDelegateIDE.java:143)
	at greenfoot.core.Simulation.runQueuedTasks(Simulation.java:470)
	at greenfoot.core.Simulation.maybePause(Simulation.java:299)
	at greenfoot.core.Simulation.runContent(Simulation.java:190)
	at greenfoot.core.Simulation.run(Simulation.java:183)
danpost danpost

2019/4/5

#
When called, the getWorld method will return a world that the actor is currently in. If the actor is not in a world when the method is called, it will return a null value. Both the instance field declaration lines (lines 14 to 17 in PlayerOne class) and the constructor (not currently overridden) are executed before the actor can be placed into any world. They are executed when the actor is being created -- well before (in computer time) the actor has a chance to be placed in a world. That being said, the value assignment of gameActive can only be done after the actor is added into the world. You could make use (override) the addedToWorld(World) method for this. I might be better, however, to keep the selection process separate from your main game by using a different world for it (just a thought).
Sploit Sploit

2019/4/5

#
First of all, thank you for the quick response :) Your tip helped me to reinvent the code a little bit with the help of a cooldown in the beginning - now it works, because the boolean is being initialised after the cooldown was count down. Here's the code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class PlayerOne extends Players
{
    private boolean gameStart = true;
    private int gameActivated = 0;
    private int startTime = 10;
    private boolean startSchleife = true;
    private int cooldown = 0;
    private boolean gameActive;
    
    /**
     * Act - do whatever the playerOne wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if (gameStart == true) {
            this.getImage().clear();
            gameStart = false;
        }
        
        if (startTime < 1 && startSchleife == true && cooldown < 1) {
                cooldown = 10;
                getGameActive();
                if (gameActive == true && startSchleife == true) {
                    this.setImage("fig1.png");
                    startSchleife = false;
                }
        }
        else {
            startTime--;
            cooldown--;
        }
    }
    
    public void getGameActive() {
        gameActive = ((Arena) getWorld()).getBGSelected();
    }
}
Thank you very much!
You need to login to post a reply.