0
我有一個Bot類,Gem類和一個主程序。處理:傳入對象
在寶石類:
Gem(float x, float y)
{
xLoc = x;
yLoc = y;
}
在主程序:
void mousePressed()
{
gem1 = new Gem(mouseX, mouseY);
seekGem = true;
}
void draw()
{
if (seekGem)
bot1.seek(gem1.xLoc, gem1.yLoc);
}
然後在博特I類給定:
void seek(float xTarg, float yTarg)
{
if (abs(xTarg - xLoc) < bodyW/4)
xDir = 0;
else if (xTarg > xLoc)
xDir = 1;
else if (xTarg < xLoc)
xDir = -1;
xLoc = xLoc + xDir * speed;
if (abs(yTarg - yLoc) < bodyH/4)
yDir = 0;
else if (yTarg > yLoc)
yDir = 1;
else if (yTarg < yLoc)
yDir = -1;
yLoc = yLoc + yDir * speed;
}
基本上機器人移動到寶石出現在屏幕上時的寶石。
我被告知將gem1傳入機器人的搜尋方法,而不是有bot1.seek(gem1.xLoc, gem1.yLoc)
,但我不知道該怎麼做。
你會寫什麼是你的全局變量的代碼?將一個對象傳遞給一個函數意味着:你應該像這樣改變你的seek函數定義:seek(Gem myobject)並且用下面的代碼調用它:seek(gem1)(但是我認爲gem1對象是你不需要的全局變量之一傳遞給一個函數:/) –