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

2017/10/4

"getWorld in class greenfoot cannot be applied to given types" error?

Beamo Beamo

2017/10/4

#
Hi! I've run into a problem again! When I hover over the red line it appears with this: method getWorld in class greenfoot.Actor cannot be applied to given types: required: no arguments found: rock reason: actual and formal argument lists differ in length Here's the code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class rock here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class rock extends AnimBase
{
    /**
     * Act - do whatever the rock wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    int speed = 2;
    public void act() 
    {
        move();
        checkCollision();
        checkCollisionBoundary();
    }    
    public void move(){
        int y = getY();
        setLocation(getX(),y+speed);
    }
    private void checkForBorder()
    {
        World world = getWorld();
        int y = world.getHeight();
        if(getY()==y){
            getWorld().removeObject(this);
        }
    }
    private void checkCollisionBoundary(){
        int y = getY();
        int maxy = getWorld().getHeight()-1;
        if(getWorld(this)==null){
            if(y==maxy){
            getWorld().removeObject(this);
        }
    }
    }
    private void checkCollision(){
        World world;
        world=getWorld();
        Object obj=world.getObjects(Ship.class).get(0);
        Ship ship=(Ship) obj;
        int shipX=ship.getX();
        int shipY=ship.getY();
        Actor topLeftShip = getOneObjectAtOffset(shipX/-2,shipY/2,Ship.class);
        Actor topRightShip = getOneObjectAtOffset(shipX/2,shipY/2,Ship.class);
        if(topLeftShip != null || topRightShip != null){
            world.removeObject(this);
        }
    }
}
Sorry to keep asking for help! :( But, any assistance would be greatly appreciated! Thank you!
Super_Hippo Super_Hippo

2017/10/4

#
Remove the 'this' in line 37.
Beamo Beamo

2017/10/4

#
Ah thank you! However now it comes up with this error: java.lang.IllegalStateException: 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. at greenfoot.Actor.failIfNotInWorld(Actor.java:711) at greenfoot.Actor.getY(Actor.java:177) at rock.checkCollisionBoundary(rock.java:35) at rock.act(rock.java:20) at greenfoot.core.Simulation.actActor(Simulation.java:604) at greenfoot.core.Simulation.runOneLoop(Simulation.java:562) at greenfoot.core.Simulation.runContent(Simulation.java:221) at greenfoot.core.Simulation.run(Simulation.java:211)
Beamo Beamo

2017/10/4

#
Oh and I should note that "at rock.checkCollisionBoundary(rock.java:35) at rock.act(rock.java:20)" are highlighted red. Sorry!
Super_Hippo Super_Hippo

2017/10/4

#
The problem is that if you remove the rock from the world in the checkCollision method, it won't be in the world when the checkCollisionBoundary method is executed. So you can't get the y-coordinates of the rock anymore for example. You could move line 37 to the beginning of the method.
You need to login to post a reply.