0

I have a Bot class, Gem class, and a main program.

In the Gem class:

  Gem(float x, float y)    
  {
    xLoc = x;
    yLoc = y;
  }

In the main program:

void mousePressed()
{
  gem1 = new Gem(mouseX, mouseY); 
  seekGem = true;
}

void draw()
{
  if (seekGem)
     bot1.seek(gem1.xLoc, gem1.yLoc);
}

and then in the Bot class I was given:

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;
}

Basically the bot moves to the gem when a gem appears on the screen.

I was told to pass gem1 into the bot's seek method instead of having bot1.seek(gem1.xLoc, gem1.yLoc) but I don't know how to do that.

4

1 に答える 1

0

このような:

void draw()
{
  if (seekGem)
     bot1.seek(gem1);
}

およびボットの seek メソッド:

void seek(Gem gemToSeek)
{
 float xTarg = gemToSeek.xLoc;
 float yTarg = gemToSeek.yLoc;
 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;
}
于 2013-11-10T00:02:36.113 に答える