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

2021/3/27

Help with global variables and falling

Samuaelk Samuaelk

2021/3/27

#
Hi, I have used a global variable in my code to have a character selection page. I want the only thing in the character selection page to be selection, not any movement. My characters are still falling. Why is this happening. Also, when I initiated a fall command in my Game, the character is floating. Please help
Gbasire Gbasire

2021/3/27

#
can you show the code of the classes involved in the problem ?
danpost danpost

2021/3/27

#
At beginning of act in character classes, add something like:
if (getWorld() instanceof SelectionWorld) return;
That should stop unwanted movement
Samuaelk Samuaelk

2021/3/27

#
Here is my code, and dan could you show it to me in the context of my code? I'm a beginner to greenfoot. This is my Hero code
public class Hero extends Actor
{   GreenfootImage i1 = new GreenfootImage("wizard_1.png");
    GreenfootImage i2 = new GreenfootImage("princess.png");
    GreenfootImage i3 = new GreenfootImage("goblin_2.png");
    GreenfootImage i4 = new GreenfootImage("barbarian_1.png");
    int vSpeed = 0;
    int acceleration = 0; 
    /**
     * Act - do whatever the Hero wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act ()
    {
        jump();
        checkFalling();
        fall();
        landOnTop();
    }

    public void fall()
    {
        setLocation(getX(), getY() + vSpeed);
    }

    public void jump()
    {
        if(Greenfoot.isKeyDown("space"))
        {
            vSpeed = -12;
        }
    }

    public void landOnTop()
    {
        if (isTouching(Ground.class))
        {
            setLocation(getX(), getY() -1);
        }
    }

    public void checkFalling()
    {
        if(!isTouching(Ground.class))
        {
            vSpeed++;

        }
        else
            vSpeed = 0;
    }

    public Hero() 
    {
        setImage(i1);
    }    

    public void changeImage (int x)
    {
        if (x == 1) 
        {
            setImage(i1);
        }
        if (x == 2) 
        {
            setImage(i2);
        }
        if (x == 3) 
        {
            setImage(i3);
        }
        if (x == 4) 
        {
            setImage(i4);

        }
    }

}
Here is my Globals code
public class Globals
{
    protected static Hero h =  new  Hero();
}
Here is my Charselect code
public class CharSelect extends World
{

    /**
     * Constructor for objects of class CharSelect.
     * 
     */
    public CharSelect()
    {    
        super(600, 400, 1);
        addObject(Globals.h, 300, 255);

        prepare();
    }

    public void act()
    {
        if (Greenfoot.isKeyDown("1"))
        {
            Globals.h.changeImage(1);
        }
        else if (Greenfoot.isKeyDown("2"))
        {
            Globals.h.changeImage(2);
        }
        else if (Greenfoot.isKeyDown("3"))
        {
            Globals.h.changeImage(3);
        }
        else if (Greenfoot.isKeyDown("4"))
        {
            Globals.h.changeImage(4);
        }
        else if (Greenfoot.isKeyDown("space"))
        {
            Greenfoot.setWorld(new Game());
        }
    }

    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
    }
}
And this is the code of the Game, where the jump and fall commands are there
private int speed = 7;
    private int vSpeed = 0;
    private int acceleration = 1 ;
    private int jumpStrenght = -6;
    /**
     * Constructor for objects of class Game.
     * 
     */
    public Game()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1000, 700, 1, false); 
        addObject(Globals.h, 50, 400);
        addObject(new Long(), 375, 488);
        addObject(new Long(), 971, 488);
        addObject(new Hump(), 511, 656);
        addObject(new Long(), 1200, 488);

    }
}
Sorry to bother you guys, and thank you so much!
danpost danpost

2021/3/27

#
Place line given after line 13 in Hero class. Replace "SelectionWorld" with "CharSelect".
Samuaelk Samuaelk

2021/3/27

#
Thank you! That is working correctly now. My other issue is the jumping thing. My character is floating up and not working right. I have no idea what is going wrong. There is no code in the Ground. class. I photoshopped the images with a lot of height and then removed the background. Could this be the problem, and if so, how do I fix it?
danpost danpost

2021/3/27

#
Samuaelk wrote...
My character is floating up and not working right. I have no idea what is going wrong. There is no code in the Ground. class. I photoshopped the images with a lot of height and then removed the background. Could this be the problem, and if so, how do I fix it?
If "removed the background" means you made the upper part of the image transparent, then yes. It is still considered part of the ground image. If the visual part is level at the top, reduce the height so no top transparency remains.
Samuaelk Samuaelk

2021/3/28

#
I changed the image and the code but it still isn't working. Here is the updated code for the Hero class. The other classes are the same as before.
public class Hero extends Actor
{   GreenfootImage i1 = new GreenfootImage("wizard_1.png");
    GreenfootImage i2 = new GreenfootImage("princess.png");
    GreenfootImage i3 = new GreenfootImage("goblin_2.png");
    GreenfootImage i4 = new GreenfootImage("barbarian_1.png");
    int vSpeed = 0;
    int acceleration = 0; 
    int speed = 7;
    int jumpStrenght = -6;
    /**
     * Act - do whatever the Hero wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act ()
    {
        if (getWorld() instanceof CharSelect) return;
        checkKeys();
        checkFall();

    }

    private void checkKeys()
    {   if(Greenfoot.isKeyDown("left"))
        { 
            moveLeft();
        }
        if(Greenfoot.isKeyDown("right"))
        {
            moveRight();
        }
        if(Greenfoot.isKeyDown("space"))
        {
            jump();
        }
    }

    public void checkFall()
    {
        if (onGround())
        {
            vSpeed = 0;
        }
        else{
            fall();
        }
    }

    public void jump()
    {
        vSpeed= jumpStrenght;
        fall();
    }

    public boolean onGround()
    {
        Actor under = getOneObjectAtOffset( 0,getImage().getHeight()/4,Ground.class);
        return under !=null;
    }

    public void moveRight()
    {
        setLocation (getX() + speed,getY() );
    }

    public void moveLeft()
    {
        setLocation (getX() - speed,getY());
    }

    public void fall()
    {
        setLocation (getX(),getY()+vSpeed);
        vSpeed = vSpeed + acceleration;
    }

    public Hero() 
    {
        setImage(i1);
    }    

    public void changeImage (int x)
    {
        if (x == 1) 
        {
            setImage(i1);
        }
        if (x == 2) 
        {
            setImage(i2);
        }
        if (x == 3) 
        {
            setImage(i3);
        }
        if (x == 4) 
        {
            setImage(i4);

        }
    }

}
My character is floating upwards whenever I press "space", and he doesn't come down
danpost danpost

2021/3/28

#
Samuaelk wrote...
My character is floating upwards whenever I press "space", and he doesn't come down
It won't with an acceleration of zero (see line 7).
Samuaelk Samuaelk

2021/3/28

#
Thanks a bunch man. Love your dedication to this forum. I have been going through the comments and find it awesome that you have been replying to these comments for over 10 years! Keep up the great work!
You need to login to post a reply.