我知道还有其他线程有类似的标题,但我已经完成了那些线程中建议的所有内容。
我正在尝试制作一款名为Fossil Fuels的恐龙赛车游戏,现在我正在制作通用游戏,从顶部向下漂浮的障碍需要避免。
我确实知道Python,并且最近才开始学习Java,所以这个问题可能听起来很愚蠢,但是这里有。
import javax.swing.JFrame;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import java.lang.System;
import java.util.ArrayList;
import java.lang.Math;
public class MainLoop
{
public static void main(String [] args)
{
boolean mloop = true;
int obsMoveIncrease = 1;
ArrayList obsList = new ArrayList();
final String[] validObsTypes = {"tree", "rock", "bush"};
long moveFrequency = 750;
long beginningTime = System.currentTimeMillis();
long obsInterval = 3000; //how often a new object is created in milliseconds
long timeOfLastMove = System.currentTimeMillis();
JFrame frame = new JFrame();
ImageIcon background = new ImageIcon("background.jpg"); //background is 700 by 1500 px
JLabel bgLabel = new JLabel(background);
frame.add(bgLabel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
while (mloop)
{
int freshObsTypeNum = (int) Math.random() * 3;
String freshObsType = validObsTypes[freshObsTypeNum]; //chooses a pseudo-random valid obstacle type
if (System.currentTimeMillis() >= beginningTime + obsInterval)
{ //checks if sufficient time has passed since last obstacle was created
Obstacle obs = new Obstacle(freshObsType, 0);
obsList.add(0, obs);
beginningTime = System.currentTimeMillis(); //resets "clock"
}
if (System.currentTimeMillis() >= timeOfLastMove + moveFrequency) //checks if it is time to move obstacles
{
timeOfLastMove = System.currentTimeMillis();
for (int i = 0; i < obsList.size(); i++)
{
obsList.get(i).moveObs(obsMoveIncrease); //moves obstacles ERROR OCCURS ON moveObs
if (obsList.get(i).getObsY() > 1500)
{
obsList.remove(i);
}
}
}
}
}
}
这是我的障碍课程:
import java.util.Arrays;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
public class Obstacle
{
private String obstacleType;
private int obstacleXPos;
public final int[] obsXlist = {0, 175, 350, 525}; //list of starting positions (4 slots along the x axis)
int obstacleYPos = 0;
public Obstacle(String type, int startPos)
{
obstacleType = type;
obstacleXPos = obsXlist[startPos];
}
public int getObsX()
{return obstacleXPos;}
public int getObsY()
{return obstacleYPos;}
public void moveObs(int increase)
{obstacleYPos += increase;}
ImageIcon obstacleImg;
public JLabel getObsImg() //gets image (on a label) for keyword given to constructor
{
if (obstacleType.equals("tree"))
obstacleImg = new ImageIcon("treeObs.jpg");
else if (obstacleType.equals("rock"))
obstacleImg = new ImageIcon("rockObs.jpg");
else if (obstacleType.equals("bush"))
obstacleImg = new ImageIcon("bushObs.jpg");
else
System.out.println(obstacleType + " is not a valid obstacle type");
JLabel obsLabel = new JLabel(obstacleImg);
return obsLabel;
}
}
它告诉我,当我从main方法中调用moveObs(int)时,找不到符号。我不知道为什么我无法访问障碍类中的公共方法
答案 0 :(得分:3)
obsList没有类型。 你试图在'对象'上调用moveObs(),而不是在'障碍'上调用。
要改变这一点,你需要让obsList成为一个类似的列表:
ArrayList<Obstacle> obsList = new ArrayList<>();