java - Problems with static and non static -


i have problems non-static variables referenced static context , don't know, how change methods solve it.

the first 1 one, supposed spawn bullet @ position shooter has @ moment of firing:

world.addobject(new bulleth(), (int)posx , (int)posy); 

also trigger removing bullets, when leave world doesn't work:

if(posx<0 || posx> getworld().getwidth() || posy <0 || posy > getworld().getheight()){         world.removeobject(this); 

i appreciate it, if me these problems , make me understand, how can circumvent error.

//edit

class hero

    import greenfoot.*;  import java.util.list; import java.awt.rectangle;  public class hero extends alive {     private double mspeed = 100.0;     private long framebegin = 0;     private double posx;     private double posy;     private boolean isjumping = false;     private double maxjumptime = 0.35;     private double jumptime = 0;     private double jspeed = 250.0;     private double fspeed = jspeed;     private boolean facer;     private int hoch = 30;     private int breit = 26;     private boolean oncd = false;     private long cd = 0;      public hero() {     }      public void addedtoworld(world world) {         posx = getx();         posy = gety();         facer = true;     }      public static void schuss(){         /*world.addobject(new bulleth(), (int)posx , (int)posy);         if(facer=true){           bullet.firedr=true;         }         else{           bullet.firedr=false;           }          */     }      public rectangle getbounds(){         return new  rectangle((int)posx, (int)posy, breit,hoch);           }      public void act(){         double deltatime;             if(framebegin == 0) {             deltatime = 0;         }             else{             deltatime = ((double) (system.currenttimemillis()-framebegin))/1000;         }         framebegin = system.currenttimemillis();          if(greenfoot.iskeydown("a")){                posx -= mspeed * deltatime;                facer = false;             }          if(greenfoot.iskeydown("d")){                 posx += mspeed * deltatime;                 facer = true;             }          if(greenfoot.iskeydown("w") && !getintersectingobjects(floor.class).isempty()) {             isjumping = true;             jumptime = 0;         }          if((!greenfoot.iskeydown("w") || (jumptime > maxjumptime)) && isjumping) {             isjumping = false;         }          if(isjumping) {             posy -= jspeed * deltatime;             jumptime += deltatime;         }            if(getintersectingobjects(floor.class).isempty() && !isjumping) {             posy += fspeed *deltatime;         }          if(greenfoot.iskeydown("space")) {             if(oncd = false){                 schuss();                 oncd = true;             }         }         if(posy > getworld().getheight()){          greenfoot.setworld(new gameover());            }          setlocation((int)posx, (int)posy);         cd += deltatime;          if(cd> 0.3){             cd=0;             oncd = false;         }     }    } 

class bullet

import greenfoot.*;  import java.awt.rectangle;  public class bullet extends actor {     public int mspeed = 250;     public boolean firedr = true;     public long framebegin = 0;     public int yspeed = 0;     public int posx;     public int posy;     private int hoch = getimage().getheight();     private int breit = getimage().getwidth();      public void addedtoworld(world world) {         posx = getx();         posy = gety();     }          public rectangle getbounds(){         return new  rectangle((int)posx, (int)posy, breit,hoch);     }      public void move(){         double deltatime;         if(framebegin == 0) {             deltatime = 0;         }         else{             deltatime = ((double) (system.currenttimemillis()-framebegin))/1000;         }         framebegin = system.currenttimemillis();          if(firedr=true){             posx += mspeed * deltatime;         }          else{             posx -= mspeed * deltatime;         }          posy+= yspeed * deltatime;           setlocation((int)posx, (int)posy);     }         public void act(){      }  } 

there critical error here. cannot reference non static variables static context unless instanciate class.
definition, static variables/methods called class variables/methods, means don't need have instance of class accessible.
non static variables/methods called instance variables/methods, means must have instance of class, , may have access these through object.

said, if must have access said variables, either make them static, or move code non static scope.
may access variables if create instance of object, don't think serve purpose.


Comments

Popular posts from this blog

c++ - How to add Crypto++ library to Qt project -

jQuery Mobile app not scrolling in Firefox -

How to use vim as editor in Matlab GUI -