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

2012/2/22

Help!!!!!! I don't understand at all?

TheNightStrider TheNightStrider

2012/2/22

#
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Play here. * * @author (your name) * @version (a version number or a date) */ public class Play extends Actor { /** * Act - do whatever the Play wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { if(Greenfoot.mouseClicked(this)){ World World = getWorld(); World.removeObject(this); GameWorld.loadLevel();}}} This keeps giving me: non static method loadLevel cannot be referenced from a static context. First of all, i don't really understand static code and how would i fix it?. Here is my copy of the world:import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class GameWorld here. * * @author (your name) * @version (a version number or a date) */ public class GameWorld extends World { /** * Constructor for objects of class GameWorld. * */ public GameWorld() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(600, 400, 1); setBackground("title.png"); addObject(new Play (),100,300); addObject(new Crosshair(),100,100); } public void loadLevel(){ setBackground("start.png"); } } Help Appreciated
Morran Morran

2012/2/23

#
It's because you are calling "loadLevel()" from the class GameWorld, not from any specific instance of GameWorld. Just use
 ((GameWorld) getWorld()).loadLevel(); 
instead.
TheNightStrider TheNightStrider

2012/2/24

#
but surely World World = getWorld(); World.removeObject(this); GameWorld.loadLevel(); does the same thing? thx for reply!
TheNightStrider TheNightStrider

2012/2/24

#
Sry for double post - can somebody please explain to me the who idea of static and non-static methods as the online explanations have been a bit confusing
danpost danpost

2012/2/24

#
First, let me say that it is not wise to name something with the exact (case-sensitive) name as something else. As 'World' is a super class of your world, 'GameWorld', it is not a good idea to have a reference to 'GameWorld' be named 'World' (it would get very confusing -- not only to you or me, but possibly to the compiler itself). Anyway, if you coded
GameWorld gw = (GameWorld) getWorld();
gw.removeObject(this();
gw.loadLevel();
then, it should work. In your last code post, the use of 'GameWorld' (which is a sub-class of World, but not an instance of a world) will short circuit. The first line in the code I provided will get the actual world that this particular actor is in, and, being cast the an instance of 'GameWorld', will set it to the variable 'gw', which is declared to hold a 'GameWorld' object. I am not sure, but you might get away with just changing your last line to
((GameWorld) World).loadLevel();
which casts 'World' to an instance of 'GameWorld'); but I do not know if the compiler would like that.
danpost danpost

2012/2/24

#
What I understand about static methods is this: They are used to modify or retrieve class information (not individual object information) Class information would include static variables, which hold information that is not specific to any object in that class). When called from outside their class, you use the class name (not a reference to an object that belongs to that class). (This is why you were getting that static/non-static error message; you were using the class name 'GameWorld' to call a method 'loadLevel()' and the compiler was looking for the static method 'loadLevel()' and only found the non-static method).
TheNightStrider TheNightStrider

2012/2/24

#
But i have to make my earlier method non static- load level is a non-static method so i am not really sure how i would call it. Thx for reply
TheNightStrider TheNightStrider

2012/2/24

#
Sry for double post again - i am still not really getting static methods, ints etc. Can i have an example please
TheNightStrider TheNightStrider

2012/2/24

#
Triple post :if (Greenfoot.mouseClicked(this)){ ((GameWorld) getWorld()).removeObject(this); ((GameWorld) getWorld()).loadLevel(); doesn't work????
TheNightStrider TheNightStrider

2012/2/24

#
And another reply - i realized why it wasn't working!
TheNightStrider TheNightStrider

2012/2/24

#
Another question, how would i remove an actor from the world ? how would i refer to it as currently i am justing using (this)
Starsonovasa Starsonovasa

2012/2/24

#
getWorld().removeObject(this);
danpost danpost

2012/2/24

#
type name. For a method or a variable declaration: Access = 'protected', 'private','public', or not specified. Determines what classes may access this method or variable. static = a qualifier. Lets the compiler know the method or variable is not object specific, but is general for the class. final = a qualifier. Lets the compiler know that the value of the variable cannot be changed. type = the return type of the method or the type of value a variable holds. A non-exhaustive list of types incluce 'int', 'double', 'float', 'String', 'List', 'Class', 'boolean', 'World', 'Actor', 'Color', 'Font', 'Point', 'Shape', 'GreenfootImage', 'GreenfootSound', 'MouseInfo', etc.
danpost danpost

2012/2/24

#
Check out the section on 'Class methods' and the following sections in the Classvars tutorial page
You need to login to post a reply.