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:
Code of Arena-Class:
Here is the error log:
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;
//}
}// 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;
}
}
}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)

