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

2018/4/26

Greenfoot switch worlds

pkd pkd

2018/4/26

#
I am experiencing difficulty switching between worlds when my game, Little Crab. I want the crab to switch from CrabWorld to CopyOfCrabWorld. Both of them are subclasses of World. There are 11 worms, and when 11 worms are eaten, the default world (CrabWorld) switches to a new world (CopyOfCrabWorld). There is an error when I typed this in: public void CopyOfCrabWorld() { if (Crab.getWormsTouched() == 11); { Greenfoot.setWorld (CopyOfCrabWorld); } }
danpost danpost

2018/4/26

#
pkd wrote...
There is an error when I typed this in: << Code Omitted >>
What is the error? -- something about getWormsTouched not being usable in a static context?
pkd pkd

2018/4/26

#
The error is the lack variables that underlines the parenthesis after Crab.getWormsTouched
danpost danpost

2018/4/26

#
pkd wrote...
The error is the lack variables that underlines the parenthesis after Crab.getWormsTouched
This makes no sense. What error message do you get (exactly)?
Super_Hippo Super_Hippo

2018/4/26

#
Is the method getWormsTouched defined as static in the Crab class? Or is Crab a variable there? You should remove the semicolon at the end of the line which starts with 'if' and probably use 'new CopyOfCrabWorld()' instead of 'CopyOfCrabWorld' unless it is a defined variable.
pkd pkd

2018/4/27

#
The wifi went down yesterday, so I was unable to reply
danpost wrote...
pkd wrote...
The error is the lack variables that underlines the parenthesis after Crab.getWormsTouched
This makes no sense. What error message do you get (exactly)?
Super_Hippo wrote...
Is the method getWormsTouched defined as static in the Crab class? Or is Crab a variable there? You should remove the semicolon at the end of the line which starts with 'if' and probably use 'new CopyOfCrabWorld()' instead of 'CopyOfCrabWorld' unless it is a defined variable.
Yes, getWormsTouched is static. in Greenfoot.setWorld (CopyOfCrabWorld); it said "cannot find symbol - variable CopyOfCrabWorld". Do I have to enter "new" to run the program?
danpost danpost

2018/4/27

#
pkd wrote...
Do I have to enter "new" to run the program?
You use "new" to create an object out of a class. So if CopyOfCrabWorld is the name of subclass of World and you wish to create a CopyOfCrabWorld object (or instance), then, yes -- use "new CopyOfCrabWorld()" to create it. The parenthesis at the end are important also (they are for any parameters needed for its creation).
You need to login to post a reply.