新建class文件用于创建障碍物(本文命名为Obstacle)
定义变量和对象
public class Obstacle {
//用于表示坐标
private int x;
private int y;
//用于记录障碍物类型
private int type;
//用于显示图像
private BufferedImage show = null;
//定义当前的场景对象
private BackGround bg = null;
}
创造构建函数
赋值
public Obstacle(int x,int y,int type,BackGround bg) {
this.x = x;
this.y = y;
this.type = type;
this.bg = bg;
}
初始化show
show = StaticValue.obstacle.get(type);
得到该类型的障碍物图像
生成变量的getter方法
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getType() {
return type;
}
public BufferedImage getShow() {
return show;
}
在Java中,getter和setter是两种常规方法,用于检索和更新变量的值。 通过使用getter和setter,可以控制如何以适当的方式访问和更新其重要变量。
快捷键生成getter和setter方法(仅适用Eclipse):
- 右键单击Java文件->选择source->生成Getter和Setter
详细步骤参考博客 http://t.csdn.cn/Ddrha
定义列表存放障碍物
//用于存放我们的所有障碍物
private List<Obstacle> obstacleList = new ArrayList<>();
需在BackGround文件中添加
注:本文仅作为学习笔记,参考的视频链接