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

2015/3/8

Why won't this code work?

jennipho jennipho

2015/3/8

#
if (yes()) { setBackground("map.jpg"); addObject(new Player(), 180, 680); addObject(new mark1(), 270, 500); addObject(new mark2(), 220, 170); addObject(new mark3(), 655, 310); if(Greenfoot.mouseClicked(mark1()) Greenfoot.setWorld(new NextWorld()); } when I put in the setworld code, it says like java null pointer exception or something..
danpost danpost

2015/3/8

#
It look like you are mixing initialization code with activity code. In other words, the 'if' checking for the click should no be coded in the same block as where the object being clicked on is being placed into the world. Also, the 'mouseClicked' method requires an Object instance; unless 'mark1()' is a method call that returns an Object instance, you will get an error; however, I do not think it will be a nullPointerException. Then, if it is, and it does, then that object must be in the world, else the value returned will always be false. It would be difficult to say what exactly you need to do because not only did you only supply a small portion of the class (in fact, you only supplied a small portion of a method -- much worse), but you only supplied what type of error you got. You should probably post the entire class code and also copy/paste the entire terminal output of the error as well.
jennipho jennipho

2015/3/9

#
this is all in the act method of one of my worlds. the marks and player and classes
danpost danpost

2015/3/9

#
jennipho wrote...
this is all in the act method of one of my worlds. the marks and player and classes
That may be; but, this line:
if(Greenfoot.mouseClicked(mark1())
uses 'mark1()' like it is a method call and 'mouseClicked' requires an Actor instance or 'null' where you have that.
danpost danpost

2015/3/9

#
You probably do not want to check for clicks on actors you add into the world within the same block of code (the 'if (yes())' code-block, in this case). That block is probably only executed when those actors are placed into the world and not continuously; so, trying to continuously check for a click on an actor there is pointless (as it will not be continuous; and if it was, you would have a multitude of actors spawning on top of each other and the one you wanted to be able to click on would be buried and never get clicked on). I would like to see what the 'yes' method does. Please show its code.
You need to login to post a reply.