This site requires JavaScript, please enable it in your browser!
Greenfoot back
Mickey09
Mickey09 wrote ...

2014/11/28

Treasure Hunt

1
2
3
4
danpost danpost

2014/11/30

#
Try something like: '(double)(time/5)/10'
Mickey09 Mickey09

2014/11/30

#
I am assuming that is stating it in milliseconds? when I use time/60, it was relatively close to seconds.
Mickey09 Mickey09

2014/11/30

#
Alright, I have another question hehe! I wont run out, so when you have to go, go! hehe. I want to call the world and get the location of the treasure from within the player class. I have a variable defined as the distance and I want to call that variable to display from within the actor class.
public class Road extends World
{
    // Variables Defined
            // PSH = Player Start Height
            // PSH2 = Player 2 Start Height
            // middle = middle of the world's width
            // origin is the center height of the world
            // TSP = Treasure Starting Point
            // D2T = Distance 2 Treasure
            
    int middle = getWidth() /2 - 10 ;//Subtracted 10 to make finish class coincide with grid background
    int origin = getHeight() /2;
    int ran1 = getHeight();
    int ran2 = getWidth();
    int PSH = getHeight() /4 + origin;// added origin to the equation to the current player spawn point
    int P2SH = getHeight() /6;
    int TSP = Greenfoot.getRandomNumber(ran2);
    int D2T = TSP - middle;
    
    /**
     * Constructor for objects of class Road.
     * 
     */
    public Road()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(2000, 200, 1);
        
        FinishLine finishline = new FinishLine();
        addObject(finishline, middle, origin);
        
        Player player = new Player();
        addObject(player, middle, PSH);
        
        Player2 player2 = new Player2();
        addObject(player2, middle, P2SH);
        
        Treasure treasure = new Treasure();
        addObject(treasure, TSP, PSH);  
        
        Treasure treasure2 = new Treasure();
        addObject(treasure2, TSP, P2SH);
       
    }
    public int treasureDistance()
    {
        return D2T;
    }
}
danpost danpost

2014/11/30

#
Mickey09 wrote...
I am assuming that is stating it in milliseconds? when I use time/60, it was relatively close to seconds.
No. With milliseconds, you would have to divide by 1000 to get seconds. The 'time' field is in act cycles (or scenario frames). One act cycle, at 60 frames per second, would take approximately 17 milliseconds.
Mickey09 Mickey09

2014/11/30

#
Now I have this code, but its not returning the integer distance when i call it...
ublic class Player2 extends Actor
{
    private int limit = 1;
    private int steps = 0;
    private int time = 0;
   
    private int distance;
            
    /**
     *
     */
   public void act() 
   {
        move(); 
   }
   public void move()
   {
      move(1); 
      steps = steps + 1;
         if (steps == limit)
         {
          turn (180); 
          steps = 0;
          limit = limit*2;
         }
      TouchingTreasure();
      time = time + 1;
   }
   public void TouchingTreasure() 
   {   
        Actor treasure = getOneObjectAtOffset(0, 0, Treasure.class); 
        if (treasure !=null)
        {
            System.out.println("Good Job Player 2! You have found the Treasure after: " + time/60 + " Seconds" + " Distance: " + distance + " Pixels");
            getWorld().removeObject (this);
            
        }
   }
    public int getTime()
   {
       return time;
   }
   public void treasureDistance()
   {
       distance = ((Road)getWorld()).treasureDistance();
   }
   public int getDistance()
   {
       return distance;
   }
} 
danpost danpost

2014/11/30

#
I do not see where 'treasureDistance' in the K class (the method starting at line 43) is being called from.
Mickey09 Mickey09

2014/11/30

#
In my world class, i have created a method called treasureDistance() that returns the variable D2T which is the distance for the treasure. So, starting on line 43, i am declaring distance variable the command getWorld.getVariable. Then on line 47, i am calling the variable. Next, on line 34, I am calling the Variable to display.
danpost danpost

2014/11/30

#
Creating a method does not cause the code to execute. It must be called to execute. Line 45 calls the 'treasureDistance' method in your world class; but nothing is calling the 'treasureDistance' method in this class; so, the 'distance' field remains at zero.
Mickey09 Mickey09

2014/11/30

#
So where should i put the code that calls the method? act()>?
danpost danpost

2014/11/30

#
Well, you only need the value once, when printing to the console (line 34). You really do not need the 'distance' field in this class; nor the 'getDistance' method. In fact you can take the '((Road)getWorld).treasureDistance()' code and replace 'distance' in line 34 with it and remove the 'treasureDistance' method from this class. I am not sure if you even need the 'getTime' method in this class, if it not going to be used elsewhere.
Mickey09 Mickey09

2014/11/30

#
It was my distance variable, but then I realized I didnt need it. Thanks for the help!
inkpenKingpin inkpenKingpin

2014/11/30

#
Thanks for the help with this. As someone pretty new to this, the step by step was pretty helpful.
You need to login to post a reply.
1
2
3
4