The title says it all; if I have numerous same actors in the same world will getY/X() work correctly inorder to detect ground? Thanks in advance.
import greenfoot.*;
import java.awt.Color;
public class Cube extends Conector
{
public boolean jump;
private double gravity=0.2;
private double exactX;
private double exactY;
private boolean ready=true;
public boolean fall = false;
public double ygravity=0.2;;
public double xgravity;
public void addedToWorld(World world)
{
GreenfootImage image=new GreenfootImage(25,25);
image.setColor(new Color(205,201,201,175));
image.fill();
setImage(image);
}
public void setLocation(int x, int y) {
exactX = x;
exactY = y;
super.setLocation(x, y);
}
public void setLocation(double x, double y) {
exactX = x;
exactY = y;
super.setLocation((int) x, (int) y);
}
public double getexactX() {
return exactX;
}
public double getexactY() {
return exactY;
}
public void act()
{
platnumber(); vector();
}
public void platnumber()
{
if(getOneIntersectingObject(Plat1.class)!=null) {
setLocation(getexactX(),platy1-20);
plat=1;
ready=true;
ydefiner=0;
fall=false;
ygravity=-0.2;
xgravity=0;
}
if(getOneIntersectingObject(Plat2.class)!=null) {
setLocation(platy2+20,getexactY());
plat=2;
ready=true;
xdefiner=0;
fall=false;
ygravity=0;
xgravity=0.2;
}
if(getOneIntersectingObject(Plat3.class)!=null) {
setLocation(getexactX(),platy3+20);
plat=3;
ready=true;
ydefiner=0;
fall=false;
ygravity=0.2;
xgravity=0;
}
if(getOneIntersectingObject(Plat4.class)!=null) {
setLocation(platy4-20,getexactY());
plat=4;
ready=true;
xdefiner=0;
fall=false;
ygravity=0;
xgravity=0.2;
}
if(getOneObjectAtOffset(0,40,Plat5.class)!=null) { //from here I need help
setLocation(getexactY(),platy5+40);
plat=3;
ready=true;
ydefiner=0;
fall=false;
ygravity=-0.2;
xgravity=0;
}
if(getOneObjectAtOffset(-25,25,Plat5.class)!=null) {
setLocation(platy5-25,getexactY());
plat=2;
ready=true;
xdefiner=0;
fall=false;
ygravity=0;
xgravity=0.2;
}
if(getOneObjectAtOffset(25,25,Plat5.class)!=null) {
setLocation(getexactY(),platy5+25);
plat=1;
ready=true;
ydefiner=0;
fall=false;
ygravity=0.2;
xgravity=0;
}
if(getOneObjectAtOffset(25,25,Plat5.class)!=null) {
setLocation(platy5+25,getexactX());
plat=4;
ready=true;
ydefiner=0;
fall=false;
ygravity=0;
xgravity=-0.2;
} //to here
}
public void vector()
{
if(plat==1&&ready==true&&Greenfoot.isKeyDown("w")){
ydefiner=-6;
xdefiner=0;
ready=false;
fall=true;
}
if(plat==2&&ready==true&&Greenfoot.isKeyDown("w")){
ydefiner=0;
xdefiner=6;
ready=false;
fall=true;
}
if(plat==3&&ready==true&&Greenfoot.isKeyDown("w")){
ydefiner=6;
xdefiner=0;
ready=false;
fall=true;
}
if(plat==4&&ready==true&&Greenfoot.isKeyDown("w")){
ydefiner=0;
xdefiner=-6;
ready=false;
fall=true;
}
if(fall==true&&plat==1)
{
ydefiner -= ygravity;
setLocation(getexactX()+xdefiner, getexactY()+ydefiner);
}
if(fall==true&&plat==2)
{
xdefiner -= xgravity;
setLocation(getexactX()+xdefiner, getexactY()+ydefiner);
}
if(fall==true&&plat==3)
{
ydefiner -= ygravity;
setLocation(getexactX()+xdefiner, getexactY()+ydefiner);
}
if(fall==true&&plat==4)
{
xdefiner += xgravity;
setLocation(getexactX()+xdefiner, getexactY()+ydefiner);
}
if(Greenfoot.isKeyDown("a")&&plat==1){
setLocation(getexactX()-2,getexactY());
}
if(Greenfoot.isKeyDown("a")&&plat==2){
setLocation(getexactX(),getexactY()-2);
}
if(Greenfoot.isKeyDown("a")&&plat==3){
setLocation(getexactX()+2,getexactY());
}
if(Greenfoot.isKeyDown("a")&&plat==4){
setLocation(getexactX(),getexactY()+2);
}
if(Greenfoot.isKeyDown("d")&&plat==1){
setLocation(getexactX()+2,getexactY());
}
if(Greenfoot.isKeyDown("d")&&plat==2){
setLocation(getexactX(),getexactY()+2);
}
if(Greenfoot.isKeyDown("d")&&plat==3){
setLocation(getexactX()-2,getexactY());
}
if(Greenfoot.isKeyDown("d")&&plat==4){
setLocation(getexactX(),getexactY()-2);
}
}}int p5w = plat5.getImage().getWidth(); // plat5 width
int p5h = plat5.getImage().getHeight(); // plat5 height
int cw = getImage().getWidth(); // cube width
int ch = getImage().getHeight(); // cube height
int cx = getX(); // cube x coordinate
int cy = getY(); // cube y coordinate
int p5x = plat5.getX(); // plat5 x coordinate
int p5y = plat5.getY(); // plat5 y coordinate
if (Math.abs(cx - p5x) < (cw + p5w) / 2)
{
if (cx - p5x > 0) setLocation(p5x + (p5w + cw) / 2, cy);
else setLocation(p5x - (p5w + cw) / 2, cy);
}
// do the same as lines 9 through 13 with y and h valuesjava.lang.NullPointerException at Cube.platnumber(Cube.java:81) at Cube.act(Cube.java:39) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205)
// world class 'Field'
import greenfoot.*;
public class Field extends World
{
public Field()
{
super(600, 600, 1);
addObject(new Wall(10, 560), 25, 300);
addObject(new Wall(10, 560), 575, 300);
addObject(new Wall(560, 10), 300, 25);
addObject(new Wall(560, 10), 300, 575);
addObject(new Wall(100, 100), 300, 300);
addObject(new Cube(), 300, 545);
}
}
// actor class 'Wall'
import greenfoot.*;
import java.awt.Color;
public class Wall extends Actor
{
public Wall(int wide, int high)
{
GreenfootImage image = new GreenfootImage(wide, high);
image.setColor(Color.darkGray);
image.fill();
setImage(image);
}
}
// actor class 'Cube'
import greenfoot.*;
import java.awt.Color;
public class Cube extends Actor
{
private int gravity;
public Cube()
{
GreenfootImage image = new GreenfootImage(49, 49);
image.setColor(Color.gray);
image.fill();
setImage(image);
}
public void act()
{
// gravity and jump
gravity += 2; // add gravity
turn(90); // face 'down'
move(gravity); // fall faster (or rise slower)
boolean hitWall = false;
while (getOneIntersectingObject(Wall.class) != null)
{ // move off any intersecting wall
hitWall = true;
move(-(int)Math.signum(gravity));
}
if (hitWall)
{ // add buffer space and stop fall (or rise)
move(-(int)Math.signum(gravity));
gravity = 0;
// jump on command
if (Greenfoot.isKeyDown("w") || Greenfoot.isKeyDown("up")) gravity -= 30;
}
turn(-90); // re-face 'forward'
// left and right
int d = 0; // to hold direction input by user
if (Greenfoot.isKeyDown("a") || Greenfoot.isKeyDown("left")) d--;
if (Greenfoot.isKeyDown("d") || Greenfoot.isKeyDown("right")) d++;
if (d != 0)
{
move(4*d); // move in given direction
if (getOneIntersectingObject(Wall.class) != null)
{ // intersecting wall after side movement
turn(-90*d); // new gravity direction
move(d); // add buffer from old ground wall
gravity = 0;
}
}
}
}public void addedToWorld(World world)
{
int x=getexactX();
int y=getexactY();
World world=(Field)getWorld();
world.addObject(new Side1(), x-5,y);
world.addObject(new Side2(), x,y+5);
world.addObject(new Side3(), x+5,y);
world.addObject(new Side4(), x,y-5);
}