这里有个egg类和母鸡的comegg方法,为什么母鸡移动之后,按j键下蛋,鸡蛋图片不随母鸡移动,不应该啊,java小白
//鸡蛋类
public class egg extends Role{
Image bufferedImage;
Hen hen=new Hen();
public egg(){
this.h=Tools.EGG_HEIGHT;
this.w=Tools.EGG_WEIGHT;
this.speed=Tools.EGG_SPEED;
}
@Override
void display(Graphics g) {
bufferedImage=Tools.imageMap.get("jd.jpg");//获取鸡蛋图片
g.drawImage(bufferedImage,(hen.positionX+hen.w)/2,hen.positionY+30,w,h,null);//设置图片大小和位置
System.out.println(hen.positionY);//测试输出
hen.positionY+=this.speed;//位置移动
}
}
public class Hen extends Role{
static Image im;
List<egg> li=new ArrayList<>();
public Hen(){
this.h=Tools.HEN_HEIGHT;
this.w=Tools.HEN_WEIGHT;
this.positionX=(Tools.PANEL_WEIGHT-w)/2;
this.positionY=0;
this.speed=Tools.HEN_SPEED;
}
void display(Graphics g){
im=Tools.imageMap.get("mj.jpg").getScaledInstance(w,h,Image.SCALE_DEFAULT);
g.drawImage(im,positionX,positionY,null);
}
void moveRight(){
this.positionX+=this.speed;
}
void moveLeft(){
this.positionX-=this.speed;
System.out.println("left"+positionX);
}
void comeEgg(){//下蛋方法
egg eg=new egg();
li.add(eg);
}
}