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

2020/10/4

Gravity in FlappyBird

catbear catbear

2020/10/4

#
I need help implementing gravity into FlappyBird using the applyGravity() method. I'm told it should be public and void. I've also been instructed that there should be 2 statements. a) Use the setLocation(int x, int y) method and write a statement that will get the current x and y for the bird and add the velocity variable to the Y coordinate. b) Write an expression that will update the velocity variable by adding the gravity variable to it. This is the code for my bird.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Bird1 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Bird1 extends SharedResources
{
    /**
     * Act - do whatever the Bird1 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        Bird1 bird1 = new Bird1();  
        setImage("flappybird1.png");
        
    }
This is the code for my world.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class FlyingBirdWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class FlyingBirdWorld extends World
{

    /**
     * Constructor for objects of class FlyingBirdWorld.
     * 
     */
    public FlyingBirdWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        populate();
        
    }
    
    /**
    * Objects added into game
    *
    */
    public void populate()
    {
        Bird1 b = new Bird1();
        addObject(b, 40, 200);
        BottomPipe a = new BottomPipe();
        addObject(a, 300, 400);
        TopPipe c = new TopPipe();
        addObject(c, 300, 0);
        BottomPipe d = new BottomPipe();
        addObject(d, 450, 380);
        TopPipe e = new TopPipe();
        addObject(e, 450, -100);
        Ground f = new Ground();
        addObject(f, 600, 400);
        Ground g = new Ground();
        addObject(g, 300, 400);
    }
    public void act() 
    {
        FlyingBirdWorld bgImage = new FlyingBirdWorld();
        setBackground("background.png");
    }
    }
Any help is appreciated.
danpost danpost

2020/10/5

#
The statements in your act methods are not actions (not anything that would be done repeatedly). The first line in both act methods need to be removed, first and foremost. It is almost never wise to create an object in a class that describes (or defines) that type. The second lines only need to be done once, during the creation of said object. That means you can move both those lines to their respective class constructors. As far as the "gravity" issue, you will need to post the SharedResources class codes (so we know what you are working with).
You need to login to post a reply.