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

2019/10/11

Im trying to make my actor move in a simple hour glass patern

Faizcon Faizcon

2019/10/11

#
{
    /**
     * Act - do whatever the Gammal wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
    move(10);
    //check upper right corner
      if (getX() >= getWorld() .getWidth()-50 && getY() <= 50 )
    {
        turn(135);
    } 
    //check lower right corner
    if (getX() >= getWorld() .getWidth()-50 && getY() >= getWorld() .getHeight()-50 )
    {
        turn (-135); 
    }
    //check lower left corner
    if (getX() <= 50 && getY() >= getWorld() .getHeight()-50 )
    {
        turn (-135); 
    }
    //check upper left corner
    if (getX() <= 50 && getY() <= 50 )
    {
        turn (135); 
                }                                                                                              
    }         
}
This is my code for trying to make my actor move in a hour glass pattern where the actor moves from top right to bottom left then right and then to the top left and loops forever. I need help, im a beginner and try not to change the base code too much
danpost danpost

2019/10/12

#
What is the size of your world and at what y-coordinate value is the actor being placed into the world?
Faizcon Faizcon

2019/10/17

#
danpost wrote...
What is the size of your world and at what y-coordinate value is the actor being placed into the world?
the world is (400, 400, 1) the actors Y coodinate is 45. It might also be important that i used the Frog default picture as the actor because sometimes the others dont work.
SushiLlama SushiLlama

2019/10/17

#
Faizcon wrote...
{

    public void act() 
    {
    move(10);
Here is your first problem. By moving 10 steps per act() execution you have to be very careful and always only compare getX() to values that are multiples of your speed value. You have done that fine but you could run into trouble later on:
    //check upper right corner
      if (getX() >= getWorld() .getWidth()-50 && getY() <= 50 )
    {
        turn(135);
    } 
Ask yourself when you want this statement to be true. Think about the rotation you want to set your actor to. Your if statement might be true more than one time / resulting in an unwanted loop. I would suggest adding a field to check if the rotation is done and putting it in the statement so that your if statements are true unly once. Maybe use setRotation instead of turn and add getRotation() == "some_Value" to your if statements so that after the rotation is changed, you dont turn forever.
    //check lower right corner
    if (getX() >= getWorld() .getWidth()-50 && getY() >= getWorld() .getHeight()-50 )
    {
        turn (-135); 
    }
    //check lower left corner
    if (getX() <= 50 && getY() >= getWorld() .getHeight()-50 )
    {
        turn (-135); 
    }
    //check upper left corner
    if (getX() <= 50 && getY() <= 50 )
    {
        turn (135); 
                }                                                                                              
    }         
}
This is my code for trying to make my actor move in a hour glass pattern where the actor moves from top right to bottom left then right and then to the top left and loops forever. I need help, im a beginner and try not to change the base code too much
Faizcon Faizcon

2019/10/17

#
public void act() 
    {
    move(10);
    
      if (getX() >= getWorld() .getWidth()-50 && getY() <= 50 )
    {
        turn(135);
    } 
    
    if (getX() >= getWorld() .getWidth()-50 && getY() >= getWorld() .getHeight()-50 )
    {
        turn (-135); 
    }
    
    if (getX() <= 50 && getY() >= getWorld() .getHeight()-50 )
    {
        turn (-135); 
    }

    if (getX() <= 50 && getY() <= 50 )
    {
        turn (45); 
                }    
                
    }         
}
thanks. i changed the code a to this above, now it starts from the top left so it works better as apposed to top right, And it is almost working. It does the pattern once but it fails to loop (which i want it to loop the pattern forever) when it reaches the bottem left corner for the second time it goes the wrong direction
Faizcon Faizcon

2019/10/17

#
also what should i change my move(10) to?(like you said below) because this may be the reason the pattern does not work.
SushiLlama wrote...
danpost danpost

2019/10/17

#
Try using setRotation instead of the turn method.
Faizcon Faizcon

2019/10/17

#
danpost wrote...
try using SetRotation instead of the turn method
it still didnt work, in fact it made it worse as it didnt even go the right direction at the second turn
Faizcon Faizcon

2019/10/17

#
danpost wrote...
Try using setRotation instead of the turn method.
Never mind it actually did work!! i just had to change some of the degrees, thanks a lot
SushiLlama SushiLlama

2019/10/18

#
Faizcon wrote...
also what should i change my move(10) to?(like you said below) because this may be the reason the pattern does not work.
This should be no problem as long as you dont ask for things like if(getX() == 45) because getX() will never be 45 when moving in steps of 10.
SushiLlama wrote...
You need to login to post a reply.