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

2018/4/29

Making an invisible square in greenfoot

1
2
LobsterL LobsterL

2018/4/29

#
I would like to create an invisible square in which it would act as an out of bounds area that once my frog was in he would be removed. Can i have help in how i would code that??
danpost danpost

2018/4/29

#
Just add a subclass of Actor to create boundaries:
import greenfoot.*;

public class Boundary extends Actor
{
    /** giving the actorr an invisible image */
    public Boundary()
    {
        setImage(new GreenfootImage(5, 400)); // adjust size as needed
    }
    
    /** removing an intersecting frog */
    public void act()
    {
        if (isTouching(Frog.class)) removeTouching(Frro.class));
    }
}
Create the Boundary objects and add them into your world in you world constructor or prepare method
LobsterL LobsterL

2018/4/29

#
Thanks. Was just wondering how i would ignore the remove frog if it is also touching a log>
danpost danpost

2018/4/29

#
LobsterL wrote...
Thanks. Was just wondering how i would ignore the remove frog if it is also touching a log>
Is there a field in the frog class that retains whether the frog is on a log or not?
LobsterL LobsterL

2018/4/29

#
no i haven't created a method for that yet
LobsterL LobsterL

2018/4/29

#
i would also like to know how to change the color of that image so i can see where it is in the world. So that I can position it correctly.
danpost danpost

2018/4/29

#
LobsterL wrote...
i would also like to know how to change the color of that image so i can see where it is in the world. So that I can position it correctly.
getImage().fill();
LobsterL LobsterL

2018/4/29

#
i have created the code for the log and boundary but am getting an illegal statement exception error
        public boolean OnLog()
    {
      if (isTouching(Log1.class) || isTouching(Log2.class) || isTouching(Log3.class))
      {
          return true;
      } else
      {
          return false;
      }
    }
        if (isTouching(Frog.class)&&frog.OnLog() == true) 
        {  
            return;
        }
        else
        {
          removeTouching(Frog.class);
        }
danpost danpost

2018/4/29

#
You have not given enough code and have not provided the exact error message to facilitate anyone helping you.
LobsterL LobsterL

2018/4/29

#
this is the error Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed. and that is both lines of code in which the error stated
danpost danpost

2018/4/29

#
LobsterL wrote...
this is the error Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed. and that is both lines of code in which the error stated
None the less, will need to see the Log class act method and methods called from there.
LobsterL LobsterL

2018/4/30

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

/**
 * Write a description of class Frog here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Frog extends Actor
{
    private String[] move = {"frogup1.png","frogup2.png","frogup3.png","frogup4.png","frogup5.png","frogup6.png"};
    private int frameCounterMove = 0;
    private int reload;
    int lives = 3;
    Lives life = new Lives();
    /**
     * Act - do whatever the Frog wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        move();
        removeWhenTouching();
        OnLog();
    }  
    public void move()
    {
        if (Greenfoot.isKeyDown("up"))
        {
            setLocation(getX(), getY()-5);
            setRotation(360);
            animateOnMove();
        }
        
        else if (Greenfoot.isKeyDown("left"))
        {
            setLocation(getX()-5, getY());
            setRotation(270);
            animateOnMove();
        }
        
        else if (Greenfoot.isKeyDown("right"))
        {
            setLocation(getX()+5, getY());
            setRotation(90);
            animateOnMove();
        }
        
        else if (Greenfoot.isKeyDown("down"))
        {   
            setLocation(getX(), getY()+5);
            setRotation(180);
            animateOnMove();
        }
    }
    public void animateOnMove()
    {
        reload++;
        if(reload==3)
       {
        setImage(move[frameCounterMove]);
        frameCounterMove++;
        if(frameCounterMove >= move.length)
          frameCounterMove=0; 
        reload = 0;
      }
    }
    public int getLives()
    {
       return lives;
    }
    public void loseLife()
    {
         lives = lives - 1;
    }
    public void removeWhenTouching()
    {
                if(isTouching(Car1.class))
        {
            removeTouching(Frog.class);
            if(getLives() == 0)
            {
                
            }else{
                loseLife();
            }
            reset();
        }
               if(isTouching(Car2.class))
        {
            removeTouching(Frog.class);
            if(getLives() == 0)
            {
                
            }else{
                loseLife();
            }
            reset();
        }
               if(isTouching(Car3.class))
        {
            removeTouching(Frog.class);
            if(getLives() == 0)
            {
                
            }else{
                loseLife();
            }
            reset();
        }
    }
    public void reset()
    {
        if(this.getWorld() instanceof MyWorld)
        {
            ((MyWorld) this.getWorld()).reset();
        }
    }
        public boolean OnLog()
    {
      if (isTouching(Log1.class) || isTouching(Log2.class) || isTouching(Log3.class))
      {
          return true;
      } else
      {
          return false;
      }
    }
    }





import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
 * Write a description of class Boundary here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Boundary extends Actor
{
    Frog frog = new Frog();
    /** giving the actorr an invisible image */
    public Boundary()
    {
        setImage(new GreenfootImage(1000, 300)); // size of the boundary
        // getImage().fill(); //used to position the boundary
    }
     
    /** removing an intersecting frog */
    public void act()
    {
        if (isTouching(Frog.class)&&frog.OnLog() == true) 
        {  
            return;
        }
        else
        {
          removeTouching(Frog.class);
        }
    }
}

danpost danpost

2018/4/30

#
In the Frog class, remove line 24.
LobsterL LobsterL

2018/4/30

#
i made that change and it still isn't working
danpost danpost

2018/4/30

#
LobsterL wrote...
i made that change and it still isn't working
I still have not seen what was asked for:
danpost wrote...
will need to see the Log class act method and methods called from there.
There are more replies on the next page.
1
2