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

2022/12/5

How to make my character not disappear when using static variables

Quinnterruption Quinnterruption

2022/12/5

#
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
Player player = new Player();
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
Player player;
player = new Player();
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:
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());
        }
    }
}
Player Code
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) {}
}
danpost danpost

2022/12/5

#
Quinnterruption wrote...
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
Player player = new Player();
my character disappears as soon as I spawn in another object during the act sequence.
Did you pause the scenario and check to see if the Player object, although not seen, may be still in the world but outside the viewing area? I noticed that you did not initialize the scrolling at the end of your world constructor with:
scroll(0);
If I run the code with my player initialized in two separate lines
Player player;
player = new Player();
the character doesn't disappear however it loses all functionality and i'm unable to use any of the methods in it.
It is probably best to initialize the player in the constructor. That way, you have a new Player object when resetting the scenario. However, the loss of functionality seems suspicious. If a Player object exists and is assigned to the static field, then animate, checkKeys and damage methods should still work (whether the player is in the world or not).
I'm also using DanPost's scroller class right now which might be what makes it an issue. << Code Omitted >>
Remove line 70 from the Player class. That line will prevent the actor (player) from moving at all. Also, you should keep the player's movement and collision codes in the Player class. That is not something the world should be responsible for.
Quinnterruption Quinnterruption

2022/12/7

#
Line 70 is there because I don't want my player to actually move, I want the world to move around him and the player to stay centered in the middle. That's also the reason I have the movement and collisions spread out between Player and World. If I initialize Player in one line
Player player = new Player();
It seems to delete the player rather than just remove the image from it. If I initialize Player in the constructor, it ceases to have functionality still. The weird thing is that all this only happens when I spawn in an Arrow class when I click with my mouse. The arrow isn't connected to Player at all which is why I'm so confused about this.
danpost danpost

2022/12/8

#
Quinnterruption wrote...
I have the movement and collisions spread out between Player and World.
Still, you are using my Scroller class. With it, only your world class (the one that is to scroll) code is adjusted. All actors should be coded as normal. That is, if they are to appear to move, then let them move. The scrolling code in your world should let the Scroller object adjust the position the actor back to the center of the world. The Scroller class with adjust ALL actors and the background as directed. Maybe you should take a look at my Scrolling Tutorial scenario, which explains everything.
Quinnterruption Quinnterruption

2022/12/8

#
I dont want the character to move thats why have i that line tthere. I read the scrolling tutorial and messed with the code, if that line isnt there then the character essentially moves according to the world when i just want him locked. in the center of the screen, hence overwriting setlocation I fixed the problem i was having btw by just using a seperate class to get the location of the character
danpost danpost

2022/12/9

#
Quinnterruption wrote...
I dont want the character to move thats why have i that line tthere.
The actor focused on (the one that stays center-screen) is an actor that moves in your world. The code in your world class (that which controls the scrolling) tells the Scroller object to scroll with amounts based upon that actors movements in the world, or by how far it has moved from the center. The scrolling will put the actor back in the center and the actor will not appear to actually move, though moving in the scrolling world.
I read the scrolling tutorial and messed with the code,
The only code that should be messed with would be in your world class. The Scroller class code was written so that NO modification of it is needed to make it work in ANY scenario. All the various scrollings in my demo scenario use the same Scroller class -- the one linked to in its description.
if that line isnt there then the character essentially moves according to the world when i just want him locked. in the center of the screen, hence overwriting setlocation I fixed the problem i was having btw by just using a seperate class to get the location of the character
Totally unnecessary.
You need to login to post a reply.