Hi,
I want to create football game and i just want to think how to do it in a simple way.Thats why i just want to make the ball move with the player.For example let say i created an object from player class....player player1=new player()...Then i want to get its coordinates like player1.getX();
player1.getY(); and in the ball class i want to set location when i see the footballer.Thats why i used can see from Animal class of greenfoot.However even if i do like this;
if(cansee(player.class))
{int x=player1.getX();
int y=player1.getY();
setLocation(x,y);
}
Its give error message after player see ball..(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.)So i see i dont understand something because im new at Java but can you help me about how can i fix this problem?
PLAYER CLASS
import greenfoot.*;
public class player extends Actor
{double WALKING_SPEED=5.0;
public void act()
{
move();
}
public void move()
{ if (Greenfoot.isKeyDown("left"))
{setLocation(getX()-5,getY());
}
if (Greenfoot.isKeyDown("right"))
{
setLocation(getX()+5,getY());
}
if (Greenfoot.isKeyDown("up"))
{
setLocation(getX(),getY()-5);
}
if (Greenfoot.isKeyDown("down"))
{
setLocation(getX(),getY()+5);
}
}}
BALL CLASS
import greenfoot.*;
public class ball extends Actor
{
public void act()
{
go();
}
public void go(){
if(canSee(player.class)){
player s=new player();
int a=s.getX();
setLocation(getX()+5,getY());
}
}
public boolean canSee(Class clss)
{
Actor actor = getOneObjectAtOffset(0, 0, clss);
return actor != null;
}
}

