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

2016/1/16

Flappy Bird

sosimple sosimple

2016/1/16

#
I keep trying to figure out how to make my bird smoothly hop up but cant seem to figure it out. My idea below was to have a for loop continue to subtract 1 to the Y but it does it too quick. Maybe if i add a delay it'd work but im not exactly sure on how to do it. any suggestions or help would be appreciated
 public void move()
    {

        if(Greenfoot.mouseClicked(null))
        {
            for(int i = 0; i<20; i++)
            {
                setLocation(getX(), getY()-3);

            }

            setRotation(-15);

        }
        else
        {
            setLocation(getX(), getY()+2);
            setRotation(15);
        }
    }
danpost danpost

2016/1/16

#
You cannot use a 'for' loop for this action. The looping is created by the repeated calling of the act method by greenfoot. You can add an instance int field for vertical speed and increment it each act cycle to emulate the effect of gravity. When the mouse is clicked, either subtract a specific amount from the vertical speed or set the vertical speed to some nominal negative number. Use the value of the field as an offset for vertical movement (add it to the value returned when you call 'getY', instead of the literal '3' as in line 8 above).
You need to login to post a reply.