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

2014/8/19

CanTurnLeft() Method

Pamsho Pamsho

2014/8/19

#
hi, first of all.. excuse my english xD well... i want to create a boolean method that returns if my actor can move to the left (also i'll create a canTurnRigth method) if there's another actor (rock) in X cels at left, then the function return false, if there isnt, return true... it sounds very easy even for me, but i dont know what to do... help me please :P
K_wow K_wow

2014/8/19

#
What you want to do is have a method like this:
public void canTurnLeft(int distance)
{
      Actor rock = getOneObjectAtOffset(0, distance, Rock.class);
return rock == null;
}
Where "distance" is how far to the left the rock you are checking for is. Also, this method only works when the rotation of the object is 0. The code will have to be changed for different rotations.
Pamsho Pamsho

2014/8/19

#
thanks for the answer! but its not what im looking for.. i left my act method to make u an idea }
 public void act() 
    {
      
      if(canMove()){
        speed();
        
        }
      else if (canTurnLeft()){
          
          turnLeft();
        
        }
      else if (canTurnRight()){
          
           turnRight();
        
        }
the objective is going up at every act() ... and when he collides with a rock... he must turn left or Right... the problem is that... if there's a Rock in the left down ... the object couldnt move to left... so i want a method who allows me to know if at -n cells on X exists a rock... i have understood that getOneObjectAtOffset detect an object on X and Y but... in both directions (+ and -) i want to detect an object only in the negative X (the left of my actor) I do not know if I explained well
davmac davmac

2014/8/19

#
K_wow wrote...
What you want to do is have a method like this:
This looks to the right rather than the left, unless 'distance' is negative.
K_wow K_wow

2014/8/19

#
davmac wrote...
K_wow wrote...
What you want to do is have a method like this:
This looks to the right rather than the left, unless 'distance' is negative.
Ah, you're right... Well, I suppose it could be used to turn either direction. Also, I accidentally wrote it as a void rather than a boolean. I see what you mean Pamsho, so this should work for your canTurnLeft() method:
public boolean canTurnLeft()  
{  
Actor rock = getOneIntersectingObject(Rock.class);
if (rock.getX() < getX())
{
return true;
}
else
{
return false;
}
}  
danpost danpost

2014/8/19

#
Pamsho wrote...
i have understood that getOneObjectAtOffset detect an object on X and Y but... in both directions (+ and -) i want to detect an object only in the negative X (the left of my actor) I do not know if I explained well
Maybe this is the misunderstanding. When using 'getOneObjectAtOffset', the first two int parameters are the offsets horizontally and vertically from the location at which the actor is at where objects of a class are looked for. That is, if your actor is at an x of 'actorX' and a y of 'actorY' and you look at offset 'offsetX' and 'offsetY', then the location which will be tested for the objects of the given class will be at an x of 'actorX+offsetX' and y of 'actorY+offsetY'. If both offsets are positive numbers, then the point looked at will be to the lower-right of the center of your actor. If both offsets are negative numbers, then the point looked at will be to the upper-left of the actor, etc. Only one point will be looked at, not 'in both directions (+ and -)'. I think you will need to explain better how you want this actor to move, in general. I could be totally wrong, but what I am getting from what you have previously stated is that you want your actor to move upward, if at all possible. If not try left; and if not that way, try right. This movement is all AI movement, without using any user input. It might help if you show the code to one of your turn methods (maybe, the 'turnLeft' or 'turnRight' one). That may provide more info as to how to code what you want.
Pamsho Pamsho

2014/8/19

#
K_wow wrote...
I see what you mean Pamsho, so this should work for your canTurnLeft() method:
Thanks! sometimes works :c im still trying :P
danpost wrote...
It might help if you show the code to one of your turn methods
yes, its an AI movement. here is the complete code of moving:
public void act() 
    {
      
      if(canMove()){
        speed();
        
        }
      else if (canTurnLeft()){
          
          turnLeft();
        
        }
      else if (canTurnRight()){
          
           turnRight();
        
        }
}


public boolean canMove(){
      
        Actor rock= getOneIntersectingObject(Rock.class);
      
         if(nave!=null){ 
           
            return false;
         
            }
          else{
            return true;
        }
        
    
    }

public boolean canTurnLeft(){
        Actor a = getOneIntersectingObject(Rock.class);
      
        
        if (a.getX()< getX()){
        return false;
    }
    else{
        
       return true;
    }
    }

public void turnLeft(){
      
       setLocation(getX()-10,getY());
      
           
    }

  public boolean canTurnRight(){
    
      Actor b = getOneIntersectingObject(Rock.class);
     
      if(b.getX() > getX()){
     
          return false;
        }
      else{
           
        return true;}
    
    } 
    
    
   public void turnRight(){
     
    setLocation(getX()+10,getY());
     
    
    }


the problem now is...  my actor turn left or right buth when detect a Rock on left or right he dont know what to do...
danpost danpost

2014/8/19

#
Ok. So, what you mean by 'turnLeft' and 'turnRight is really 'moveLeft' and 'moveRight'; and there is really no turning involved, just a change in the direction of movement. You are also calling a 'speed' method -- where is it?
Pamsho Pamsho

2014/8/19

#
yes! its a change of direction.. here's the code
 public void speed(){
    
        setImage("sai1.png");
        if(speed<25){
        setLocation(getX(),getY()-10);
        }
        else if (speed < 50) {   
        setLocation(getX(),getY()-12);   
        }  
   
        else if(speed<75){
        setLocation(getX(),getY()-14);  
        }
        else if(speed==100){
        setLocation(getX(),getY()-18);
       }
    
  }
i have the speed value on: public NameOfACtor() with getRandomNumber(100)
danpost danpost

2014/8/19

#
With that, you will have a 25 percent chance of the actual speed being 10, 12, or 14, and a one percent chance of the actual speed being 18 with a 24 percent chance of the speed being zero. I do not think that is what you intended. For a 25 percent chance of the actual speed being either 10, 12, 14, or 18, you could just say:
// with 
public Sailboat(){ // or whatever it is named
    speed = 10+2*Greenfoot.getRandomNumber(4);
    if (speed == 16) speed = 18;
    setImage("sail.png");
// then your 'speed' method could be reduced to this:
public void speed(){
    setLocation(getX(), getY()-speed);
}
For your movement, however, I would suggest coding something like the following as your act method, provided that one of the three directions (up, left, and right) will always be available to move to:
public void act() {
    if (moveUp() || moveLeft() || moveRight()); // I actually think this will work
}
This way, once an available direction is found, you will have already moved and you do not have to call two methods for each direction. Your check/move methods would be
private boolean moveUp()
{
    // if rock is above (center and corner checks) -- no, not moved
    if (getOneObjectAtOffset(0, -getImage().getHeight()/2-1, Rock.class) != null) return false;
    if (getOneObjectAtOffset(getImage().getWidth()/2, -getImage().getHeight()/2-1, Wall.class) != null) return false;
    if (getOneObjectAtOffset(-getImage().getWidth()/2, -getImage().getHeight()/2-1, Wall.class) != null) return false;
    // ok to move
    setLocation(getX(), getY()-speed);
    // check for rock bump
    Actor rock = getOneIntersectingObject(Rock.class);
    // if rock bump -- locate against rock
    if (rock != null)
    {
        setLocation(getX(), rock.getY()+(rock.getImage().getHeight()+getImage().getHeight())/2);
    }
    // yes, moved
    return true;
}
with similar type methods for left and right. I will test the logic and confirm (or not; but, I am quite sure it is good).
danpost danpost

2014/8/20

#
I made some corrections in the code above; so, it works as far as doing what you have so far intended. I am sure that you will still be working on the AI for this actor, however. It will probably come down to you needing to track which direction was moved last and perform the tests for the next move dependent on the last direction moved. Otherwise, you will get some back and forth movement that you will not want.
Pamsho Pamsho

2014/8/20

#
yep im still working, thanks a lot for the answer!! i'll try it
You need to login to post a reply.