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

2014/11/28

Treasure Hunt

1
2
3
4
Mickey09 Mickey09

2014/11/28

#
I am working on creating a scenario with using an algorithm to determine the amount of step for the actor class to use to search for a "treasure".. My question is with the Istouching error. I have used several different versions of the istouching and none of them seem to be working. I am looking for any help with getting the touching code working. Thanks!
 public void TouchingTreasure() 
    {   
        Actor player;
        player = getOneObjectAtOffset(0, 0, Player.class);
        if (player !=null)
        {
            System.out.println("Good Job!");
            Greenfoot.stop();
    
        }
   }
I am also attempting to make an algorithm that automatically adds one each loop, changes direction, and then adds one to the variable and then moves the variable over and over.
 private int algorithm;
    public Player2()
    {
        algorithm = 1;
    }
     public void act() 
    {
       move();
    }
    public void move()
    {
      move(algorithm);
      setRotation(180);
      next();
    }
    public void next()
    {
        algorithm = algorithm + 1;
    }
Super_Hippo Super_Hippo

2014/11/28

#
If the code is in the Player code, you should look for a Treasure and not for a player:
Actor treasure = getOneObjectAtOffset(0, 0, Treasure.class);
if (treasure != null) If it is in the treasure class, you can leave it as it was. You only need to call the method from the act method. In the act method, you don't change the rotation every act cycle. You set it to 180 degree (facing left) every time and move a bit faster to the left side every act. To change direction, you could use 'setRotation(getRotation+x)' or just 'turn(x)'.
danpost danpost

2014/11/28

#
To do what you want to do, you need an 'if' statement in there somewhere for the moving part of the code. The condition would be whether the actor has just now arrived at the left or right edge of the world; and the action would be to turn around and increase speed.
Mickey09 Mickey09

2014/11/30

#
So, i fixed the coding issue with the "Touching" method. My next challenge is getting my algorithm working. Basically what I want to do is, move once, turn around, move again, then turn around and move again. Each time increasing my steps by one.
ublic class Player extends Actor
{
    private int a = 1;
    
    /**
     * Act - do whatever the Player wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
   public void act() 
   {
        add();
        TouchingTreasure();
   }
      public void TouchingTreasure() 
   {   
        Actor treasure = getOneObjectAtOffset(0, 0, Treasure.class); 
        if (treasure !=null)
        {
            System.out.println("Good Job!, You found the treasure!");
            Greenfoot.stop();
    
        }
   }
   public void setDirection()
   {
       if (a==1)
        { DirectionEast();
        }
        else
        { DirectionWest();
        }
    }
   public void add()
   { 
       setDirection(); 
       move(a); 
       a = a+1;

   }
   public void DirectionEast()
   {
       setRotation(0);
   }
   public void DirectionWest()
   {
       setRotation(180);
   }
}
I could write the code like a = 1 b = 2 c = 3 etc. But I want to create a code that theoretically would continue forever. What would it look like because I want my actor to move (a) once, then change direction, add one to (a), then move (a) again.
danpost danpost

2014/11/30

#
I might have misunderstood previously. I was thinking you wanted to jump back and forth, not move back and forth across the screen with increasing limits. This will take at minimum two fields -- one to set the distance to travel (the limit) and the other to count the steps while traveling. You can use the state of the distance being odd or even to determine the direction of movement or you can add a third field to track it. Each act move and increment the step counter, then check to see if is equal to the limit; if so, increment the limit, adjust the direction field if you have one and reset the step counter to zero.
Mickey09 Mickey09

2014/11/30

#
Alright, so I am still a little new to greenfoot programming but I would use like 3 variables right? a = 1 b = -1? Also, how would that code look? I dont neccesarily want you to write for me, but more or less guide me. Um, apologies. Also, i am unsure of how to make the actor only move (a) once, then turn around and move (a) again.
danpost danpost

2014/11/30

#
You can use the 'turn(int)' method of the Actor class for turning around. Then, just move the incremented distance.
Mickey09 Mickey09

2014/11/30

#
Right, I get that, but its repeating itself over and over in a loop, when i put in
public void move()
{
      move(a);
      turn (90);
      a = a + 1

}
My actor freaks out, it just keeps turn 90 and moving (a). I want my actor to move once, turn 90, add one to a, then move a again. Then turn 90, repeat.
danpost danpost

2014/11/30

#
Let us start anew. Remove everything from the class except the move method and the act method (we can add the TouchingTreasure method back in later). Now, post the class as it is.
Mickey09 Mickey09

2014/11/30

#
Sorry, did not mean to make this confusing xD
public class Player extends Actor
{
    private int a = 1;
 
  
    /**
     * Act - do whatever the Player wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
   public void act() 
   {
        move();

   }


   public void move()
   { 
       move(a);
   }

}
danpost danpost

2014/11/30

#
Now, change the move method to what you had just posted previously and you want to turn 180, not 90.
Mickey09 Mickey09

2014/11/30

#
public class Player extends Actor
{
    private int a = 1;
  
    /**
     * Act - do whatever the Player wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
   public void act() 
   {
        move();
        
   }
  
   public void move()
   { 
        
      move(a);  
      turn (180);  
      a = a + 1  ;  
   }
}
danpost danpost

2014/11/30

#
Ok. Now, is this the behavior you wanted? If not, then how is it not and what should it be like?
Mickey09 Mickey09

2014/11/30

#
Now, the actor is glitching back and forth, not, exact moving the way I was hoping. I want the actor to slowly move 1 to the right, then turn around and move 2 to the left. over and over again. Right now, its moving super fast and not traveling the way I intended. Also, I want the actor to glide and not teleport to the spot.
danpost danpost

2014/11/30

#
Then you should have stated you wanted this: move(1); turn(180); move(1); move(1); turn(180); move(1); move(1); move(1); turn(180); etc.
There are more replies on the next page.
1
2
3
4