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

2021/6/14

My character can move in 8 directions and I can't make it work so that my character can't go past a border(object not the world border).

Turbo_Thorsten Turbo_Thorsten

2021/6/14

#
Title
if( isTouching(Border.class) ){
            setRotation(getRotation()+180);
            move(4);
        }
Gbasire Gbasire

2021/6/14

#
do turn(number) instead
Turbo_Thorsten Turbo_Thorsten

2021/6/14

#
Gbasire wrote...
do turn(number) instead
It looks nicer but it doesn'T solve my Problem. The character bugs around spins and goes throgh the border with my code.
Gbasire Gbasire

2021/6/14

#
can you share the rest of the code ?
Turbo_Thorsten Turbo_Thorsten

2021/6/14

#
Sure
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Player here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Player extends Actor
{
    /**
     * Act - do whatever the Player wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    int gunReloadTime;             
    int reloadDelayCount;
    public static double bulletsshot;
    public static int k=30;
    public static int timerA = 100;
    public static boolean l= false;
    public Player ()
    {
    gunReloadTime = k;
    reloadDelayCount = 0;
    }

    public void act() 

    {
        if( isTouching(Border.class) ){
            turn(180);
            move(4);
        }
        checkKeypress();
        reloadDelayCount++;
        if (l == true)
        {
         timerA--; 
         if(timerA == 0)
         {
          k = 30;
          l = false;
          timerA = 100;
         }
        }
    }
    public void checkKeypress()
    {
        if (Greenfoot.isKeyDown("W") && Greenfoot.isKeyDown("D")) 
        {
            setRotation(315);
            move(4);
        }
        
        else if (Greenfoot.isKeyDown("A") && Greenfoot.isKeyDown("W")) 
        {
            setRotation(225);
            move(4);
        }
        
        else if (Greenfoot.isKeyDown("D") && Greenfoot.isKeyDown("S")) 
        {
            setRotation(45);
            move(4);
        }
        else if (Greenfoot.isKeyDown("A") && Greenfoot.isKeyDown("S")) 
        {
            setRotation(135);
            move(4);
        }
        else if (Greenfoot.isKeyDown("W")) 
        {
            setRotation(270);
            move(4);
        }
        else if (Greenfoot.isKeyDown("S")) 
        {
            setRotation(90);
            move(4);
        }
        
        else if (Greenfoot.isKeyDown("D")) 
        {
            setRotation(0);
            move(4);
        }
        else if (Greenfoot.isKeyDown("A")) 
        {
            setRotation(180);
            move(4);
        }
        
        
        if (Greenfoot.isKeyDown("space"))
        {
            shoot();
        }
    }
    
    public void setGunReloadTime(int reloadTime)
    {
        gunReloadTime = reloadTime;
    }
    
    public void shoot()
    {
    if (reloadDelayCount >= gunReloadTime)
        {
            gunReloadTime = k;
        Bullet bullet = new Bullet ();
        getWorld().addObject(bullet, getX(), getY());
        bullet.setRotation(getRotation());
        reloadDelayCount = 0;
        bullet.move(10.0);
        bulletsshot++;
    }
    }
    public static void fasterShooting()
    {
        k = 5;
        l = true;
    }
    
}
danpost danpost

2021/6/14

#
Turbo_Thorsten wrote...
The character bugs around spins and goes throgh the border with my code.
Move checkKeypress to the beginning of act.
Turbo_Thorsten Turbo_Thorsten

2021/6/14

#
danpost wrote...
Turbo_Thorsten wrote...
The character bugs around spins and goes throgh the border with my code.
Move checkKeypress to the beginning of act.
Still bugs
Turbo_Thorsten Turbo_Thorsten

2021/6/14

#
Update: I changed how I move (new code below). How can I make the borders unpassable?
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Player here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Player extends Actor
{
    /**
     * Act - do whatever the Player wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    int gunReloadTime;             
    int reloadDelayCount;
    public static double bulletsshot;
    public static int k=30;
    public static int timerA = 150;
    public static boolean l= false;
    public Player ()
    {
    gunReloadTime = k;
    reloadDelayCount = 0;
    }

    public void act() 

    {
        checkKeypress();
        if( isTouching(Border.class) ){
            turn(180);
            move(4);
        }
        reloadDelayCount++;
        if (l == true)
        {
         timerA--; 
         if(timerA == 0)
         {
          k = 30;
          l = false;
          timerA = 150;
         }
        }
    }
    public void checkKeypress()
    {
        if (Greenfoot.isKeyDown("W") && Greenfoot.isKeyDown("D")) 
        {
            setLocation(getX()+3, getY()-3);
        }
        
        else if (Greenfoot.isKeyDown("A") && Greenfoot.isKeyDown("W")) 
        {
            setLocation(getX()-3, getY()-3);
        }
        
        else if (Greenfoot.isKeyDown("D") && Greenfoot.isKeyDown("S")) 
        {
            setLocation(getX()+3, getY()+3);
        }
        else if (Greenfoot.isKeyDown("A") && Greenfoot.isKeyDown("S")) 
        {
            setLocation(getX()-3, getY()+3);
        }
        else if (Greenfoot.isKeyDown("W")) 
        {
            setLocation(getX(), getY()-4);
        }
        else if (Greenfoot.isKeyDown("S")) 
        {
            setLocation(getX(), getY()+4);
        }
        
        else if (Greenfoot.isKeyDown("D")) 
        {
            setLocation(getX()+4, getY());
        }
        else if (Greenfoot.isKeyDown("A")) 
        {
            setLocation(getX()-4, getY());
        }
        shoot();
    }
    
    public void setGunReloadTime(int reloadTime)
    {
        gunReloadTime = reloadTime;
    }
    
    public void shoot()
    {
    if (reloadDelayCount >= gunReloadTime)
        {
        gunReloadTime = k;
        MouseInfo mouse = Greenfoot.getMouseInfo();
        if (mouse == null) return;
        int x = mouse.getX();
        int y = mouse.getY();
        Bullet bullet = new Bullet();
        getWorld().addObject(bullet, getX(), getY());
        bullet.turnTowards(x, y);
        reloadDelayCount = 0;
        bullet.move(10.0);
        bulletsshot++;
    }
    }
    public static void fasterShooting()
    {
        k = 5;
        l = true;
    }
}
danpost danpost

2021/6/14

#
Two problems with it. One is that the moving distance does not match the "rebound" distance (when a border is encountered). The other is "rebound" code does not know which direction of movement caused the collision. Turning 180 does no good unless the actor faces the direction it moved. I would move the "rebound" code into the checkKeypress method and rewrite the method as follows:
private void checkKeypress()
{
    final int speed = 3;
    int dx = 0, dy = 0;
    if (Greenfoot.isKeyDown("d")) dx++;
    if (Greenfoot.isKeyDown("a")) dx--;
    if (Greenfoot.isKeyDown("s")) dy++;
    if (Greenfoot.isKeyDown("w")) dy--;
    if (dx == 0 && dy == 0) return;
    setLocation(getX()+speed *dx, getY()+speed *dy);
    if (isTouching(Border.class))
    {
        setLocation(getX()-speed *dx, getY()-speed *dy);
    }
}
Turbo_Thorsten Turbo_Thorsten

2021/6/15

#
danpost wrote...
Two problems with it. One is that the moving distance does not match the "rebound" distance (when a border is encountered). The other is "rebound" code does not know which direction of movement caused the collision. Turning 180 does no good unless the actor faces the direction it moved. I would move the "rebound" code into the checkKeypress method and rewrite the method as follows:
private void checkKeypress()
{
    final int speed = 3;
    int dx = 0, dy = 0;
    if (Greenfoot.isKeyDown("d")) dx++;
    if (Greenfoot.isKeyDown("a")) dx--;
    if (Greenfoot.isKeyDown("s")) dy++;
    if (Greenfoot.isKeyDown("w")) dy--;
    if (dx == 0 && dy == 0) return;
    setLocation(getX()+speed *dx, getY()+speed *dy);
    if (isTouching(Border.class))
    {
        setLocation(getX()-speed *dx, getY()-speed *dy);
    }
}
Thank you it works!
You need to login to post a reply.