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

2012/11/14

2d game + Gravity?

Johnathon Johnathon

2012/11/14

#
Hey, so im making a game for my programming class. The basic idea is to be a simple 2d old school street fighter style game, the problem I'm having happens to be with jumping. I would like to make it, so if the up key is pressed, the character will raise a predetermined amount (A constant I have) and then descend back down to the ground. I;m still rather new to greenfoot and java so please explain how you make something work please :] Heres my current code that works for walking, but not for jumping.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.*;
import java.awt.*;
/**
 * Write a description of class char1 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class char1 extends Characters
{
    public static final double TE_SPD = 5.0;
    public static final double TE_JMP = 100.0;
    /**
     * Act - do whatever the char1 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
      move1();
      jump1();
    }    
    
    public void move1()
    {
        if(Greenfoot.isKeyDown("right"))
        {
            int x = (int) Math.round(getX() + TE_SPD);
            int y = (int) Math.round(getY());
            setImage("Char1Right.png");
            setLocation(x,y);
        }
        if(Greenfoot.isKeyDown("left"))
        {
            int x = (int) Math.round(getX() - TE_SPD);
            int y = (int) Math.round(getY());
            setImage("Char1Left.png");
            setLocation(x,y);
        }
        else
        {
        }
    }

    public void jump1()
    {
        if(Greenfoot.isKeyDown("up"))
        {
            jumptiming();
            jumptiming();
            jumptiming();
            gravity();
            gravity();
            gravity();
        }
    }
    
    public void jumptiming()
    {
            int x = (int) Math.round(getX());
            int y = (int) Math.round(getY() + TE_JMP);
            setLocation(x,y);
            try
                {
                    Thread.sleep(500);
                }
            catch(InterruptedException e)
                {
                }
    }
    
    public void gravity()
    {
            int x = (int) Math.round(getX());
            int y = (int) Math.round(getY() - TE_JMP);
            setLocation(x,y);
            try
                {
                    Thread.sleep(500);
                }
            catch(InterruptedException e)
            {
            }
    }
 }
danpost danpost

2012/11/14

#
Thread sleeping is not the preferred way to perform such actions. You might want to look at some flight trajectory, bounce, or jump scenarios that have code supplied to see how they work them. I have recently created a scenario called Flight Trajectory Demo, whose code in the Ball2 class would be sufficient for your needs.
Johnathon Johnathon

2012/11/14

#
Thanks a lot! I'm actually quite busy right now and checking this from school, I will however try taking a look at that when I get home and will reply again if I come across something I don't understand, but again, thanks so much for this! Edit: Thanks! I pulled it apart and figured it out. Now i'm just stuck at the next bit of code I don't understand. I plan to post a separate thread for that though, Thanks again for all the help!
You need to login to post a reply.