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

2018/2/5

Creating a Timer

Atebits Atebits

2018/2/5

#
I used this topic as tutorial, but ended up getting a 'java.lang.NullPointerException' when trying to run, stemming from 'at Icebound_Camp01.updateTimerDisplay(Icebound_Camp01.java:93)' 'at Icebound_Camp01.<init>(Icebound_Camp01.java:13)' I am fairly new to Greenfoot and coding in general, so sorry in advance. Full Code: import greenfoot.*; public class Icebound_Camp01 extends World { //initiate privates private Actor timerDisplay = new SimpleActor(); private int timeElapsed; private int timeCounter; public Icebound_Camp01() { //begin world and set speed super(600, 400, 1); Greenfoot.setSpeed(50); //timer dislay updateTimerDisplay(); addObject(timerDisplay, 80, 20); //initiate coord and counter vairables int x; x=0; int y; y=0; int a; a=0; //initiate location variables int Width; Width = (x); int Height; Height = (y); //set perimeter while (a <=13) { a=a+1; addObject(new Wall(),Width,0); Width = (Width+45); } while (a >=13 && a <=23) { a=a+1; addObject(new Wall(),Width,Height); Height = (Height+45); } while (a >=23 && a <=38) { a=a+1; addObject(new Wall(),Width,Height); Width = (Width-45); } while (a >=38 && a <=48) { a=a+1; addObject(new Wall(),Width,Height); Height = (Height-45); } //initate enemy spawn location and coutner variables int EnemySpawnX; int EnemySpawnY; //spawn enemy EnemySpawnX = Greenfoot.getRandomNumber(200)+50; EnemySpawnY = Greenfoot.getRandomNumber(320)+40; addObject(new Enemy(),EnemySpawnX,EnemySpawnY); //initate enemy spawn location and coutner variables int PlayerSpawnX; int PlayerSpawnY; //spawn player PlayerSpawnX = Greenfoot.getRandomNumber(50)+500; PlayerSpawnY = Greenfoot.getRandomNumber(320)+40; addObject(new Player(),PlayerSpawnX,PlayerSpawnY); //timer timeCounter = (timeCounter+1)%55; if (timeCounter == 0) { timeElapsed++; updateTimerDisplay(); } } //timer dispay private void updateTimerDisplay() { timerDisplay.setImage(new GreenfootImage("Time: "+timeElapsed, 24, null, null)); } }
Super_Hippo Super_Hippo

2018/2/5

#
Use this line in the updateTimerDisplay:
timerDisplay.setImage(new GreenfootImage("Time: "+timeElapsed, 24, Color.BLACK, new Color(0,0,0,0)));
(At least until the next version of Greenfoot.)
You need to login to post a reply.