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

2020/5/12

Need help with static method

Shabaz616 Shabaz616

2020/5/12

#
GameOver gameover = new GameOver(); MyWorld.addObject(gameover, MyWorld.getWidth()/2, MyWorld.getHeight()/2); Greenfoot.stop(); .addObject error- When i put this in, it says "non-static method addObject(greenfoot.Actor,int,int) cannot be referenced from a static context .getWidth error - non static method getWidth() cannot be referenced from a static context .getHeight error - non static method getHeight cannot be referenced from a static context
Shabaz616 Shabaz616

2020/5/12

#
Fixed it, for those who need the fix getWorld().addObject(gameover, getWorld().getWidth()/2, getWorld().getHeight()/2);
danpost danpost

2020/5/12

#
MyWorld is a Class name and only shares its name as the type of objects it creates. In general (that is, without specifically programmed restrictions), a class can create multiple objects. Given two worlds:
World mwA = new MyWorld(600, 400, 1);
World mwB = new MyWorld(800, 600, 1);
The use of the Class name (or Type of object) is not sufficient to distinguish what world instance you might, say, want the width of.
You need to login to post a reply.