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

2019/5/7

I Cannot Get My Character To Stay Within The World Without Crashing The Program

ScaleFlaw89 ScaleFlaw89

2019/5/7

#
I am creating an Agario based game. I have 70% of it done. I need to get my program to have bots that roam around randomly Here is my code so far: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.util.ArrayList; /** * Write a description of class BotCell here. * * @author (your name) * @version (a version number or a date) */ public class BotCell extends Cell { public String name = "UnnamedBot"; public int mass=100; public Location next; public GridActor neighbor; public BotCell(String bn1) { bn1 = "Bot1" ; } public void act() { setDirection(90); boolean b = canMove(); if (canMove()) move(1); else turn(45); } public void turn() { setDirection(getDirection()+ Location.HALF_RIGHT); } public void move() { Grid<GridActor> gr = getGrid(); if (gr == null) return; Location loc = getLocation(); Location next = loc.getAdjacentLocation(getDirection()); if (gr.isValid(next)) moveTo(next); else turn(45); } public boolean canMove() { System.out.println("test"); Grid<GridActor> gr = getGrid(); if (gr == null) return false; Location loc = getLocation(); if (loc.getRow() <= 0 || loc.getRow() >= 104 || loc.getCol() <= 0 || loc.getCol() >= 199) return false; /*if (!gr.isValid(loc)) return false; else*/ if ((loc.getRow() >= 0 || loc.getRow() <= 104) || (loc.getCol() >= 0 || loc.getCol() <= 199)) { next = loc.getAdjacentLocation(getDirection()); System.out.println(getDirection()); if (!gr.isValid(next)) { System.out.println("this works"); return false; } if (gr.isValid(next)) neighbor = gr.get(next); /*if(gr.isValid(next)) neighbor = gr.get(next);*/ //if (!gr.isValid(next)) } return true; // || (neighbor instanceof UserCell) || (neighbor instanceof VirusCell); // ok to move into empty location or onto flower // not ok to move onto any other actor } public void processActors() { for(GridActor a: getActors()) { if (!getWorld().getObjectsAt(getX(), getY(), MassCell.class).isEmpty()) { getWorld().removeObject(getWorld().getObjectsAt(getX(), getY(), MassCell.class).get(0)); mass=mass+5; } /*if(a instanceof MassCell && this.isTouching (MassCell.class)) { getWorld().removeObject (a); mass=mass+5; }*/ if (!getWorld().getObjectsAt(getX(), getY(), UserBotCell.class).isEmpty()) { getWorld().removeObject(getWorld().getObjectsAt(getX(), getY(), UserBotCell.class).get(0)); mass=mass+10; } if (!getWorld().getObjectsAt(getX(), getY(), UserCell.class).isEmpty()) { getWorld().removeObject(getWorld().getObjectsAt(getX(), getY(), BotCell.class).get(0)); mass=mass+100; } if (!getWorld().getObjectsAt(getX(), getY(), VirusCell.class).isEmpty()) { getWorld().removeObject(getWorld().getObjectsAt(getX(), getY(), VirusCell.class).get(0)); setImage("broken-button-blue.png"); mass=mass-5; } /* if(a instanceof VirusCell && this.isTouching (VirusCell.class)) { //((Actor)this).setImage("broken-button-blue.png"); getWorld().removeObject(a); mass=mass-1; //setImage(brokenImage); //GreenfootImage g = getImage(); //g = brokenImage; //System.out.println("1"); }*/ } //setImage(brokenImage); // broken = true; //mass+=5; //return mass; } public int getMass() { return mass; } public String toString() { return name + " " + mass; } } Some of the comments are previous attempts or simply random crap I wrote. Here is the error: java.lang.IllegalArgumentException: Location (105, 80) is not valid at BoundedGrid.get(BoundedGrid.java:60) at GridActor.setLocation(GridActor.java:218) at greenfoot.Actor.move(Actor.java:304) at BotCell.act(BotCell.java:25) at greenfoot.core.Simulation.actActor(Simulation.java:567) at greenfoot.core.Simulation.runOneLoop(Simulation.java:530) at greenfoot.core.Simulation.runContent(Simulation.java:193) at greenfoot.core.Simulation.run(Simulation.java:183) If anyone needs anything else, let me know. Thanks!
danpost danpost

2019/5/7

#
Your logic in the canMove method is flawed. After what appears to be a valid check on the loc location, you do an erroneous unnecessary one on it. This is just before you acquire the next location, of which you do not do any checks on (where you should).
ScaleFlaw89 ScaleFlaw89

2019/5/7

#
next = loc.getAdjacentLocation(getDirection());
            System.out.println(getDirection());
            if (!gr.isValid(next))
            {
            System.out.println("this works");
            return false;
        }
            if (gr.isValid(next))
            neighbor = gr.get(next);
           
I have removed that check on the loc method. I am not sure how to check the next method. I have tried several methods (including many not shown). What steps do I need to take?
danpost danpost

2019/5/8

#
You check it just like checking loc. Just replace loc with next.
You need to login to post a reply.