我從靜態上下文引用的非靜態變量有一些問題,我不知道如何更改我的方法來解決它。靜態和非靜態問題
第一個是這個,這是應該的射手已經在發射時刻的位置產卵子彈:
World.addObject(new bulletH(), (int)posX , (int)posY);
而且去除子彈,當他們離開這個世界沒有按觸發「T工作:
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;
}
}
}
類子彈
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(){
}
}
如果您向我們展示哪些變量是靜態的,哪些是非靜態的,以及確切地說「不起作用」的含義,這可能會有所幫助。 – merlin2011
哪些變量是**靜態**,哪些變量是**非靜態**? – Gundamaiah
posX/posY是非靜態的,其餘的應該是靜態的。 – user3556111