使用Java进行Android游戏开发:无法解析参数
问题描述:
我正在开发一款android棋盘游戏,并且遇到来自我的某个参数的错误。这是一个基于mancala的游戏,其视图(TableView.class
)包含当时的排坑和石块,以及主要活动(Game.class
)。我正在尝试定义一个坑及其内容。使用Java进行Android游戏开发:无法解析参数
下面是代码片段:
public class Game extends Activity {
int pitIndex, pitContents;
int _pitContents=4;
int pitsPerRow=5;
//more code here .......
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
table_view = getInitialTableView();
tableView = new TableView(this);
setContentView(tableView);
tableView.requestFocus();
//...more code here
//
// ...
// define a playing pit
public int Pit(int pitIndex, int pitContents) {
this.pitIndex=pitIndex;
this.pitContents=pitContents;
}
// set player1's pits and populate them
public int Player1Pits[] = new int[16*2];
{
for (int i = 0; i < (2 * pitsPerRow); i++) {
if (i < pitsPerRow) {
Player1Pits[i] = new Pit(i,_pitContents);
else {
// do nothing
}
}
}
//.....
}
我从的Eclipse得到的错误是“坑不能被解析为一个类型”当我尝试实例化一个新坑:
Player1Pits[i] = new Pit(i,_pitContents);
有人知道我要去哪里吗?我是否必须将Pit定义为Game
课程之外的课程? 在发布此问题之前,我已经详尽搜索了解决方案。您的意见将不胜感激。提前致谢。
答
摆脱你的方法被称为坑,使一个坑班是这样的:
public class Pit {
public int mPitIndex, mPitContents;
public Pit(int pitIndex, int pitContents) {
this.mPitIndex = pitIndex;
this.mPitContents = pitContents;
}
}
是什么语言,你的方式熟悉?
+0
感谢Ian Newson.That解决了这个问题。我熟悉Matlab和VisualBasic,最近开始与Java。现在我想添加一个字符串到Pit类,但是当我这样做时,我得到的错误“类型不匹配。不能从坑转换为字符串”。我如何重新定义我的数组(公共int Player1Pits [] = new int [16 * 2];),以便它接受除pitIndex和pitContents之外的字符串(pitString)?我尝试过使用Array,但没有奏效。 – Buzz 2012-02-08 15:06:48
请插入你的logcat。 – iSun 2012-02-08 13:29:14
@Ali。感谢您的回应。截至目前,我没有logcat。由于这个错误,程序不会编译。 – Buzz 2012-02-08 13:37:01
你确定你知道你在做什么? – Shaiful 2012-02-08 13:41:04