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

2021/2/3

New to Greenfoot... How to make object not protrude world edge.

Pijmzhchus57 Pijmzhchus57

2021/2/3

#
Hey. I would like to make my actor not protrude world edge. It kinda goes halfway and then it blocks the move input. I kinda feel this is easy, but I can“t find anything online. Thanks!
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)


public class Kosik extends Actor
{
   
    final String MoveLeft="Left";
    
    final String MoveRight="Right";
    
    public int movement=10;
    public void act() 
    
    {
                if(Greenfoot.isKeyDown(MoveLeft)){
            move(-movement);
        }
                if(Greenfoot.isKeyDown(MoveRight)){
            move(movement);
        }
    }    
}
danpost danpost

2021/2/3

#
Pijmzhchus57 wrote...
I would like to make my actor not protrude world edge. It kinda goes halfway and then it blocks the move input.
The actor's image is centered at its location. Use actor's location and the width of its image to adjust location of actor when too close to an edge (after a move).
Pijmzhchus57 Pijmzhchus57

2021/2/3

#
danpost wrote...
Pijmzhchus57 wrote...
I would like to make my actor not protrude world edge. It kinda goes halfway and then it blocks the move input.
Use actor's location and the width of its image to adjust location of actor when too close to an edge (after a move).
Could you please give me the code? Im really not good at programming...
Pijmzhchus57 Pijmzhchus57

2021/2/3

#
danpost wrote...
Pijmzhchus57 wrote...
I would like to make my actor not protrude world edge. It kinda goes halfway and then it blocks the move input.
The actor's image is centered at its location. Use actor's location and the width of its image to adjust location of actor when too close to an edge (after a move).
OK. I somehow figured it out! Thanks, your reply helped me get to the bottom of it ;)
public class Kosik extends Actor
{
   
    final String MoveLeft="Left";
    
    final String MoveRight="Right";
    
    public int movement=10;
    public void act() 
    
    {
       
            
                if(Greenfoot.isKeyDown(MoveLeft)){
            move(-movement);
            if (getX()<70){
                setLocation(70, 577);
            }
        }
   
                if(Greenfoot.isKeyDown(MoveRight)){
            move(movement);
            if(getX()>730){
            setLocation(730, 577);
        }
    
}
}
}
You need to login to post a reply.