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/30

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

/**
 * Write a description of class Log1 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Log1 extends Log
{
    /**
     * Act - do whatever the Log1 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
       move(4);
       if (isAtEdge())
       {
           getWorld().removeObject(this);
       }
    }    
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Log2 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Log2 extends Log
{
    /**
     * Act - do whatever the Log2 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
       move(4);
       if (isAtEdge())
       {
           getWorld().removeObject(this);
       }
    }    
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Log3 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Log3 extends Log
{
    /**
     * Act - do whatever the Log3 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
       move(4);
       if (isAtEdge())
       {
           getWorld().removeObject(this);
       }
    }    
}
World
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MyWorld extends World
{
    // int getX() x = Car3X;
    int fastcarTimer = 100;
    int slowcarTimer = 100;
    int logSpawner = 100;
    Lives life1 = new Lives();
    Lives life2 = new Lives();
    Lives life3 = new Lives();
    Frog frog = new Frog();
    Log1 log1 = new Log1();
    Log2 log2 = new Log2();
    Log3 log3 = new Log3(); 
    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1000, 900, 1); 
        addObject(new Finish(), 305, 32); // adding the finish lilypad
        addObject(new Finish(), 515, 32); // adding the finish lilypad
        addObject(new Finish(), 713, 32); // adding the finish lilypad
        addObject(new Finish(), 905, 32); // adding the finish lilypad
        addObject(new Finish(), 95, 32); // adding the finish lilypad
        addObject(new Car1(), 10, 630);
        addObject(new Car1(), 500, 630);
        addObject(new Car1(), 900, 630);
        addObject(new Car1(), 300, 630);
        addObject(new Car3(), 995, 537);
        addObject(new Car3(), 750, 537);
        addObject(new Car3(), 395, 537);
        addObject(new Car3(), 195, 537);
        addObject(new Car3(), 95, 537);
        addObject(new Car2(), 300, 736);
        addObject(new Car2(), 994, 736);
        addObject(new Car2(), 800, 736);
        showText("Lives : " , 40, 880);
        addObject(new Log1(), 183, 366);
        addObject(new Log2(), 537, 366);
        addObject(new Log2(), 846, 366);
        addObject(new Log3(), 107, 282);
        addObject(new Log1(), 408, 282);
        addObject(new Log2(), 778, 282);
        addObject(new Log3(), 214, 204);
        addObject(new Log2(), 475, 204);
        addObject(new Log1(), 811, 204);
        addObject(new Log3(), 106, 136);
        addObject(new Log2(), 395, 136);
        addObject(new Log1(), 774, 136);
        addObject(new Boundary(), 500, 243);
         // Greenfoot.playSound("Commodore64Music(Frogger).mp3");
    }
    public void act()
    {
        // if (Car3X == 0)
        // {
        // }
        randomslowCarSpawner();
        randomfastCarSpawner();
        createLives();
        logSpawner();
        start();
       
    }
    public void slowCarSpawn()
    {
    }
    public void fastCarSpawn()
    {
    }
    public void createLives()
    {
        if(frog.getLives() == 3)
        {
          addObject(life1, 90, 880);
          addObject(life2, 120, 880);  
          addObject(life3, 150, 880);  
        }else if(frog.getLives() == 2)
        {
            addObject(life1, 90, 880);
            addObject(life2, 120, 880);  
        }else if(frog.getLives() == 1)
        {
            addObject(life1, 90, 880);
        }
            // public void removeAtWorldEdge()
        // {
        // if (isAtEdge(Car3()))
        // {
            // removeObject(Car3());
            // spawnTimer();
            
        // }
        // }
        // public void spawnTimer()
       // {
        // spawnTimer++;
        // if (spawnTimer == 0)
        // {
            // addObject(new Car3(), 36, 36);
        // }
        // } 
    }
        public void randomfastCarSpawner()
    {
        fastcarTimer++;
        if(Greenfoot.getRandomNumber(40)<50&&fastcarTimer == 100)
        {
            addObject(new Car1(),10, 630);
        }
        if(Greenfoot.getRandomNumber(30)<40&&fastcarTimer == 100)
        {
            addObject(new Car3(),995, 537);
        }
        if(fastcarTimer == 101)
        {
            fastcarTimer = 0;
        }
    }
    public void randomslowCarSpawner()
    {
        slowcarTimer++;
        if(Greenfoot.getRandomNumber(40)<60&&slowcarTimer == 100)
        {
            addObject(new Car2(),995, 736);
        } 
        if(slowcarTimer == 101)
        {
            slowcarTimer = 0;
        }
    }
    public void logSpawner()
    {
       logSpawner++;
        if(Greenfoot.getRandomNumber(100)<80&&logSpawner == 100)
       {
         addObject(new Log1(), 30, 366);
       }
        if(Greenfoot.getRandomNumber(100)<80&&logSpawner == 100)
       {
         addObject(new Log2(), 30, 282);
       }
        if(Greenfoot.getRandomNumber(100)<80&&logSpawner == 100)
       {
         addObject(new Log3(), 30, 204);
       }
       if(Greenfoot.getRandomNumber(100)<80&&logSpawner == 100)
       {
         addObject(new Log2(), 30, 136);
       }
       if(logSpawner == 101)
       {
         logSpawner = 0;
       }
    }
    public void addedToWorld()
    {
      
    }
    public void reset()
    {
        if(frog.getLives() == 0)
        {
            Greenfoot.stop();
        }
        else if(frog.getLives() == 2){
           removeObject(life1);
        }else{
           removeObject(life2);
        }
        removeObject(frog);
        addObject(frog, 500, 850);
    }
    public void start()
    {
        addObject(frog, 500, 850);
    }
}
danpost danpost

2018/4/30

#
Why would you create a Frog object in your Boundary class? Don't., (1) it is not the frog that is in the world; and (2) you cannot check to see if it is on a log because it is not in a world. You need to check whether the frog already in the world is on a log or not. Use getIntersectingObject instead of isTouching. If not null, then run onLog on it and remove if found not on a log.
LobsterL LobsterL

2018/5/2

#
if have changed the code but am getting the same error
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 (getIntersectingObjects(Frog.class) != null) 
        {  
           if (frog.OnLog() == false)
           {
               reset();
           }
        }
    }
       public void reset()
    {
        if(this.getWorld() instanceof MyWorld)
        {
            ((MyWorld) this.getWorld()).reset();
        }
    }
}

danpost danpost

2018/5/2

#
danpost wrote...
Why would you create a Frog object in your Boundary class? Don't.,
LobsterL LobsterL

2018/5/2

#
how would i then call the frogs on log method??
danpost danpost

2018/5/2

#
Split line 22 into two lines:
Frog frog = (Frog)getOneIntersectingObject(Frog.class);
if (frog != null)
You could then combine the conditions into one line and the act method would then be this:
public void act()
{
    Frog frog = (Frog)getOneIntersectingObject(Frog.class);
    if (frog != null && !frog.onLog()) reset();
}
LobsterL LobsterL

2018/5/2

#
that works perfectly. Thanks for all the help!
Ticker175 Ticker175

2018/5/3

#
nice Mitch
You need to login to post a reply.
1
2