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

2014/6/7

Can not longer place my units.

1
2
3
CalzoneCannon CalzoneCannon

2014/6/8

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

/**
 * Write a description of class Lawn here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Lawn extends World
{
    private int PeaTimer;
    private int zombTimer;
    private Location [][] grid;
    /**
     * Constructor for objects of class Lawn.
     * 
     */
    public Lawn()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1050, 600, 1); 
        grid = new Location[5][9];
        for(int r = 0; r < grid.length; r++)
        {
            for(int c = 0; c < grid[r].length; c++)
            {
                grid[r][c] = new Location(215 + (c*80), 130 + (r*97));
            }
        }
        Menu menu = new Menu();
        addObject(menu, getWidth()/2, 38);
        menuLoad();
        lawnmower();
        zombTimer=300;
        PeaTimer=0;
    }

    public void act()
    {
        if(!(StartScreen.checkChange()))
        {
            Greenfoot.setWorld(new StartScreen());
        }
        zombSpawn();       
        if(Greenfoot.mousePressed(this))
        {
            build(Greenfoot.getMouseInfo().getX(), Greenfoot.getMouseInfo().getY());
        }
        PeaTimer++;
    }

    public void build(int x, int y)
    {
        String key = getKey();
        for(int r=0; r < grid.length; r++)
        {
            for(int c = 0; c <grid[r].length; c++)
            {
                if( x + 40 >= grid[r][c].getX() && x - 40 <= grid[r][c].getX())
                {
                    if(y + 48 >= grid[r][c].getY() && y - 48 <= grid[r][c].getY())
                    {
                        if(checkEmpty(grid[r][c].getX(), grid[r][c].getY()) && key != null)
                        {
                            spawn(grid[r][c].getX(), grid[r][c].getY(), key);
                        }
                    }
                }
            }
        }

    }

    public String getKey()
    {
        if(Greenfoot.isKeyDown("P"))
        {
            return "P";
        }
        else if(Greenfoot.isKeyDown("S"))
        {
            return "S";
        }
        else if(Greenfoot.isKeyDown("W"))
        {
            return "W";
        }
        else if(Greenfoot.isKeyDown("M"))
        {
            return "M";
        }
        else if(Greenfoot.isKeyDown("T"))
        {
            return "T";
        }
        else
        {
            return null;
        }
    }

    public void spawn(int x, int y, String str)
    {
        if(str.equals("P")) //&& SunScoreboard.getSun() >= PeaShooter.COST)
        {
            addObject(new PeaShooter(), x, y);
            SunScoreboard.changeSun((PeaShooter.COST)*-1);
        }
        else if(str.equals("S")) //&& SunScoreboard.getSun() >= Sunflower.COST)
        {
            addObject(new Sunflower(), x, y);
            SunScoreboard.changeSun((Sunflower.COST)*-1);
        }
        else if(str.equals("W"))
        {            
            addObject(new Wallnut(), x, y);
            SunScoreboard.changeSun((Wallnut.COST)*-1);
        }
        
        else if(str.equals("M"))
        {
            addObject(new PotatoMine(), x, y);
            SunScoreboard.changeSun((PotatoMine.COST)*-1);
        }
        else if(str.equals("T"))
        {
            addObject(new Threepeater(), x, y);
            SunScoreboard.changeSun((Threepeater.COST)*-1);
        }
        else;
        //addObject(new PeaShooter(), x, y);
    }

    public boolean checkEmpty(int x, int y)
    {
        if(getObjectsAt(x, y, Plant.class).size() > 0)
        {
            return false;
        }
        else
        {
            return true;
        }
    }

    public void zombSpawn()
    {
        int zomb = (int)(Math.random()*4);
        if(zombTimer == 0)
        {
            if(zomb == 0)
            {
                addObject(new RegZombie(), getWidth() , 130 + ((int)(Math.random()*5)*97));
            }
            else if(zomb == 1)
            {
                addObject(new BucketZombie(), getWidth(), 130 + ((int)(Math.random()*5)*97));
            }
            else if(zomb == 2)
            {
                addObject(new NewsZombie(), getWidth(), 130 + ((int)(Math.random()*5)*97));
            }
            else if(zomb == 3)
            {
                addObject(new ScreenZombie(), getWidth(), 130 + ((int)(Math.random()*5)*97));
            }
            zombTimer=300;
        }
        else
        {
            zombTimer--;
        }
    }

    public void menuLoad()
    {
        BuySunFlower buySun = new BuySunFlower();
        addObject(buySun, 313, 38);
        BuyPeaShooter buyPea = new BuyPeaShooter();
        addObject(buyPea, 407, 38);
        BuyWallnut buyWall = new BuyWallnut();
        addObject(buyWall, 360, 38);
    }

    public static void gameOver()
    {
        Greenfoot.setWorld(new GameOver());
    }
    
    public void lawnmower()
    {
        for(int i = 0; i < grid.length; i++)
        {
            addObject(new Lawnmower(), 125, grid[i][0].getY());
        }
    }
}
danpost danpost

2014/6/8

#
Are you still not able to spawn objects from some of the Plant subclasses? I tried to simulate what you had so far and had no problems with any of the spawnings. Of course, I do not have exactly what you have (or everything you have); but, this just means that pretty much everything dealing with the spawning that you have posted should be working properly. Is it possible that the actors are being removed as soon as they are added to the world?
CalzoneCannon CalzoneCannon

2014/6/9

#
That's possible, but I would not see why. I didn't add a method that would remove them at all
CalzoneCannon CalzoneCannon

2014/6/9

#
I mean, I got them to spawn if I removed everything from their act methods, but they would not do anything even if it was in the parents act and I did super.act().
danpost danpost

2014/6/9

#
You do have a 'die' method in your Plant class. Add the following line to it and see if it prints out anything:
System.out.println("removed: "+this.toString());
CalzoneCannon CalzoneCannon

2014/6/10

#
Ah, good thinking. Yep, it printed that it removed the wallnut when I tried to place it. I'll see what I can do to fix that and touch back if I have any problems.
danpost danpost

2014/6/10

#
I bet if your remove 'private int health = 30;' from the Wallnut class and add this to the class:
public Wallnut()
{
    health = 30;
}
that it might fix your problem. (I asked that you fix that before)
CalzoneCannon CalzoneCannon

2014/6/10

#
I am now able to get them to spawn, although the biggest problem now is the constructor. Is there a way to create variables in a subclass constructor after you've called super() ? There are certain variables I need for some classes, but I can't seem to create them after I called super. Any ideas?
CalzoneCannon CalzoneCannon

2014/6/10

#
Nevermind, ignore the above statement
danpost danpost

2014/6/10

#
I was going to correct my last post to add into the constructor what you already had in it. But, I guess you got it. Please elaborate on what you are trying to do as far as the needed variables.
CalzoneCannon CalzoneCannon

2014/6/10

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

/**
 * Write a description of class Wallnut here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Wallnut extends Plant
{
    private GifImage gif;
    private boolean changed1;
    private boolean changed2;
    public static final int COST = 50;
    public Wallnut()
    {
        gif = new GifImage("WallNut.gif");
        health = 30;  
        hitCounter = 0;
        changed1=false;
        changed2=false;
    }

    /**
     * Act - do whatever the Wallnut wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        super.act();
        if(super.health <= 20 && super.health > 10)
        {
            medHealth();
        }
        if(super.health <= 10)
        {
            lowHealth();
        }
    }    

    public void medHealth()
    {
        if(changed1 == false)
        {
            gif = new GifImage("Wallnut_cracked1.gif");
            changed1 = true;
        }
    }

    public void lowHealth()
    {
        if(changed2 == false)
        {
            gif = new GifImage("Wallnut_cracked2.gif");
            changed2 = true;
        }
    }

}
I am keeping track of changed variables and have them set to false in the constructor (also, do I make the variables public in the plant class? Because it wont let me set it if its private)
CalzoneCannon CalzoneCannon

2014/6/10

#
Thank you so much! I got my methods working! I guess I answered my own questions. Thank you for sticking with me these last couple of days, I hope I get a high grade on this project.
danpost danpost

2014/6/10

#
You can set the fields as 'protected' (in the Plant class). 'false' is the default value for boolean fields. They do not require being set to 'false' in the constructor. The same goes for all numeric fields; zero is the default value for all numeric primitive types.
CalzoneCannon CalzoneCannon

2014/6/10

#
I have a new issue, if you are willing to help me with it. One of my units, PotatoMine, is supposed to explode when the zombies touch it. The problem now is that when the zombie touches the PotatoMine, the zombie dies but then greenfoot just freezes and glitches out, the world won't load or anything.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class PotatoMine here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class PotatoMine extends Plant
{
    public static final int COST = 25;
    private int armTimer;
    private boolean changed;
    private boolean explode;

    public PotatoMine()
    {
        gif = new GifImage("PotatoMineNotReady.gif");
        health = 6;
        hitCounter = 0;
        armTimer = 300;
        changed = false;
        explode = false;
    }

    /**
     * Act - do whatever the PotatoMine wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        super.act();
        setImage(gif.getCurrentImage());
        if(armTimer == 0)
        {
            arm();
        }
        else
        {
            armTimer--;
        }
        explode();
    }    

    public void arm()
    {
        if(changed == false)
        {
            gif = new GifImage("PotatoMine.gif");
            changed = true;
        }
    }

    public void explode()
    {
        if(this.isTouching(Zombie.class) && changed == true && explode == false)
        {
            gif = new GifImage("PotatoMine_mashed.gif");
            for(int i = 0; i < this.getIntersectingObjects(Zombie.class).size(); i++)
            {
                getWorld().removeObject(((Zombie)(getIntersectingObjects(Zombie.class).get(i))));
            }
            explode=true;
        }
    }
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Zombie here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public abstract class Zombie extends SmoothMover
{
    protected int health;
    protected GifImage gif;

    /**
     * Act - do whatever the Zombie wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if(canEat())
        {
            eat();
        }
        else
        {
            move();
        }
        if(this.checkDeath())
        {
            getWorld().removeObject(this);
        }
    }    

    /**
     * Move forward by the specified exact distance.
     */
    public void move(double distance)
    {
        super.move(distance);
    }

    public void loseHP()
    {
        if(isTouching(Pea.class))
        {
            health--;
            removeTouching(Pea.class);
        }
    }

    public void mowed()
    {
        getWorld().removeObject(this);
    }
    
    public abstract void eat();
    
    public abstract void move();
    
    public boolean canEat()
    {
        if(getWorld().getObjectsAt(this.getX() - 5, this.getY(), Plant.class).size() != 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
    public boolean checkDeath()
    {
        if(this.health <= 0)
        {
            return true;
        }
        return false;
    }


}
import greenfoot.*; import java.util.*; /** * Write a description of class RegZombie here. * * @author (your name) * @version (a version number or a date) */ public class RegZombie extends Zombie { private boolean changed; public RegZombie() { gif = new GifImage("ZombieReal.gif"); health = 17; changed = false; } /** * Act - do whatever the RegZombie wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { super.act(); this.setImage(gif.getCurrentImage()); } public void move() { if(this.getX() <= 5) { getWorld().removeObject(this); Lawn.gameOver(); } else { if(changed == true) { gif = new GifImage("ZombieReal.gif"); changed = false; } this.setLocation(this.getX() - 1, this.getY()); } } public void eat() { if(changed == false) { gif = new GifImage("ZombieAttackReal.gif"); changed = true; } } } I'm not sure what is happening. I don't believe it is the fault of the loop, because it was having this error even before I added the loop.
danpost danpost

2014/6/11

#
You do not need to iterate through the intersecting zombies to remove them. You can remove them all with one statement using 'removeObjects' instead of 'removeObject'. Change lines 59 through 61 of the PotatoMine class to this:
getWorld().removeObjects(getIntersectingObjects(Zombie.class));
You should test more often if the glitching out was happening before you added to loop. Look at some of the things you changed between the last time you had it running properly and now.
There are more replies on the next page.
1
2
3