I'm trying to use a static initialization of my Player object to make it accessible from other classes without having to initialize again. Whenever I run the code with my Player initialized outside the constructor my character disappears as soon as I spawn in another object during the act sequence.
If I run the code with my player initialized in two separate lines the character doesn't disappear however it loses all functionality and i'm unable to use any of the methods in it.
I'm also using DanPost's scroller class right now which might be what makes it an issue.
World Code:
Player Code
Player player = new Player();
Player player; player = new Player();
public class MyWorld extends World { public static final int WIDE = 1200; public static final int HIGH = 800; public static final int CELL = 1; int block; int arrCharge = 0; boolean advance; Arrow arrow; Scroller scroller; static Player player; Rock rock = new Rock(); /** * Constructor for objects of class MyWorld. * */ public MyWorld() { // Create a new world with WIDExHIGH cells with a cell size of 1x1 pixels. super(WIDE, HIGH, CELL, false); GreenfootImage image = new GreenfootImage("WhiteBackground.png"); // Initiates Unlimited Scroller scroller = new Scroller(this, image); player = new Player(); prepare(); } public void prepare() { //Map map = new Map(WIDE, HIGH); //addObject(map, 0, 0); map(); addObject(player, WIDE/2, HIGH/2); addObject(rock, WIDE+8*block, -HIGH+2*block); } dHIGH/2) + (y*block) + (i*block)); } } } public void act() { scroll(4); checkKeys(); } public void scroll(int amnt) { int rate = amnt; int dsx = 0; int dsy = 0; if(player.wallChecks.contains("right")) dsx--; if(player.wallChecks.contains("left")) dsx++; if(player.wallChecks.contains("down")) dsy--; if(player.wallChecks.contains("up")) dsy++; if(Greenfoot.isKeyDown("d")) { dsx++; player.animate("R"); } if(Greenfoot.isKeyDown("a")) { dsx--; player.animate("L"); } if(Greenfoot.isKeyDown("s")) { dsy++; if(Greenfoot.isKeyDown("d")) player.animate("R"); else if(Greenfoot.isKeyDown("a")) player.animate("L"); else player.animate("D"); } if(Greenfoot.isKeyDown("w")) { dsy--; if(Greenfoot.isKeyDown("d")) player.animate("R"); else if(Greenfoot.isKeyDown("a")) player.animate("L"); else player.animate("U"); } player.wallChecks.clear(); scroller.scroll(dsx*rate, dsy*rate); } public void checkKeys() { if(Greenfoot.mousePressed(null)) { advance = true; } if(advance) { arrCharge++; } if(Greenfoot.mouseClicked(null)) { if(arrCharge <= 30) { arrow = new Arrow(10); arrCharge = 0; advance = false; } else if(arrCharge > 30) { arrow = new Arrow(25); arrCharge = 0; advance = false; } addObject(arrow, player.getX(), player.getY()); } } }
public class Player extends Actor { int health = 25; int frame = 1; int count; GreenfootImage img = new GreenfootImage("Arrow/ArrBack1.png"); String animDir = "Back"; public final Set<String> wallChecks = new HashSet<>(); public Player() { setImage(img); img.scale(getImage().getWidth()*2, getImage().getHeight()*2); } /** * Act - do whatever the Player wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { touchWall(); //checkKeys(); } public void touchWall() { Actor right = getOneObjectAtOffset((getImage().getWidth()/2), 0, Wall.class); Actor left = getOneObjectAtOffset(-(getImage().getWidth()/2), 0, Wall.class); Actor down = getOneObjectAtOffset(0, (getImage().getHeight()/2), Wall.class); Actor up = getOneObjectAtOffset(0, -(getImage().getHeight()/2), Wall.class); Actor right1 = getOneObjectAtOffset((getImage().getWidth()/2), 0, Rock.class); Actor left1 = getOneObjectAtOffset(-(getImage().getWidth()/2), 0, Rock.class); Actor down1 = getOneObjectAtOffset(0, (getImage().getHeight()/2), Rock.class); Actor up1 = getOneObjectAtOffset(0, -(getImage().getHeight()/2), Rock.class); if(right != null || right1 != null) wallChecks.add("right"); if(left != null || left1 != null) wallChecks.add("left"); if(down != null || down1 != null) wallChecks.add("down"); if(up != null || up1 != null) wallChecks.add("up"); } public void damage(int amnt) { health = health = amnt; } public void checkKeys() { } public void animate(String direction) { count++; if(direction.equals("R")) animDir = "Right"; else if(direction.equals("L")) animDir = "Left"; else if(direction.equals("U")) animDir = "Back"; else if(direction.equals("D")) animDir = "Front"; if(count % 6 == 0) { if(frame == 1) { img = new GreenfootImage("Arrow/Arr" + animDir +"1.png"); frame = 2; } else if(frame == 2) { img = new GreenfootImage("Arrow/Arr" + animDir +"2.png"); frame = 3; } else if(frame == 3) { img = new GreenfootImage("Arrow/Arr" + animDir +"3.png"); frame = 1; } setImage(img); img.scale(getImage().getWidth()*2, getImage().getHeight()*2); } } public void setLocation(int x, int y) {} }