静态和非静态问题

时间:2014-04-21 09:38:07

标签: java static

我对从静态上下文引用的非静态变量有一些问题,我不知道,如何更改我的方法来解决它。

第一个就是这个,它应该在射击时射手的位置产生子弹:

World.addObject(new bulletH(), (int)posX , (int)posY);

当他们离开世界时,移除子弹的触发器也不起作用:

if(posX<0 || posX> getWorld().getWidth() || posY <0 || posY > getWorld().getHeight()){
        World.removeObject(this);

我真的很感激,如果你能帮我解决这些问题并让我明白,我怎么能绕过这个错误。

//修改

班级英雄

    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(){

    }

}

1 个答案:

答案 0 :(得分:1)

此处存在严重错误。您不能从静态上下文引用非静态变量,除非您实例化您的类 根据定义,静态变量/方法也称为类变量/方法,这意味着您无需拥有该类的实例即可访问。
非静态变量/方法也称为实例变量/方法,这意味着您必须具有该类的实例,并且您可能只能通过对象访问这些实例。

也就是说,如果您必须有权访问所述变量,要么将它们设置为静态,要么将代码移动到非静态范围 如果您创建对象的实例,则可以访问这些变量,但我认为这不符合您的目的。