我知道有一些有類似標題的其他線程,但我已經做了在那些線程提出的一切。不能使用另一個類的方法?
我試圖做一個恐龍的賽車遊戲稱爲化石燃料,現在我創造了障礙從需要避免頂灑落在一般的遊戲。
我知道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;
}
}
它告訴我,當我打電話從主方法中moveObs(INT)符號無法找到。我不知道爲什麼我會無法訪問的故障等級
嘗試做這樣的事情'的ArrayList obsList'因此這個列表知道什麼樣的東西在裏面,所以它知道什麼方法存在爲那些對象。 –
csmckelvey
只要改變你的ArrayList定義使用泛型像 ArrayList的 obsList =新的ArrayList (); –
我剛試過,它解決了這個問題,但它爲什麼起作用?難道不應該已經知道哪種類型的項在列表 –