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

2013/7/22

Jump Engine Problem

8bitorangejuice 8bitorangejuice

2013/7/22

#
Hello, I am currently attempting to make my own (crappy) jumping engine, but I have encountered a problem I have pondered over for many a hour: I can not get the object to leave ground or, if I can, it jumps infinetly. Here is my code and by the way I am using static int's if you are wondering.
import greenfoot.*;
import java.awt.Color;
public class Cube extends Conector
{
    public boolean fall = false;
    public boolean jump;
    private double gravity=1.1;
    private double yspeed;
    private double exactX;  
    private double exactY;   
    private boolean ready=true;
    public void addedToWorld(World world)  
    {
        GreenfootImage image=new GreenfootImage(25,25);  
        image.setColor(new Color(205,201,201,175));  
        image.fill(); 
        setImage(image);
}
    public void setLocation(int x, int y) {  
    exactX = x;  
    exactY = y;  
    super.setLocation(x, y);  
}        
    public void setLocation(double x, double y) {  
    exactX = x;  
    exactY = y;  
    super.setLocation((int) x, (int) y);  
}  
    public double getexactX() {  
    return exactX;  
}  
    public double getexactY() {  
    return exactY;  
}  
    public void act() 
    {
        setLocation(getexactX(),getexactY()+gravity);
        Actor platform = getOneObjectAtOffset(0, getImage().getHeight()/2, Platform.class);        
        int groundHeight = 20;
        int newY = platy - (groundHeight + getImage().getHeight())/2;          
        if(Greenfoot.isKeyDown("a"))
        {
            setLocation(getexactX()-2,getexactY()+gravity);
        }
        if(Greenfoot.isKeyDown("d"))
        {
            setLocation(getexactX()+2,getexactY()+gravity);
        }
        if(ready=true&&Greenfoot.isKeyDown("w"))         
        {
            yspeed=-5;
            setLocation(getexactX(), getexactY()+yspeed);  
            fall=true;
        }
        if(fall=true&&platy<getY())
        {
            yspeed-=gravity;
        }
        if(fall=false)
        {
            vspeed=0;
        }      
        if(getOneObjectAtOffset(0,20,Platform.class)!=null)
        {
            setLocation(getexactX(),platy-20);
            fall=false;
        }
        
}}
Thanks for the help.
danpost danpost

2013/7/22

#
It would help to see the Conector class also (since this is an extension of that class). Also, please explain precisely what the 'ready' boolean is being used for.
8bitorangejuice 8bitorangejuice

2013/7/22

#
Hi, thanks for the quick response; all I have in the conector class - so far- is only a static int:
import greenfoot.*;
public class Conector extends Actor
{
    public static int platy;
    public void act() 
    {
        
    }    
}
and the ready was ment to be the checker if you can jump but it did not work for me (e.g. if(getOneObjectAtOffset(0,20,Platform.class)!=null){ ready=true; fall=false; setLocation(getexactX(),platy-20);} if(ready=true&&Greenfoot.isKeyDown("w")) {yspeed=-5; ready=false; fall=true;}
8bitorangejuice 8bitorangejuice

2013/7/22

#
oh and the code in the platform class is:
import greenfoot.*;
import java.awt.Color;
public class Platform extends Conector
{ 
    public void addedToWorld(World world)  
    {
        GreenfootImage image=new GreenfootImage(280,15);  
        image.setColor(new Color(205,201,201,255));  
        image.fill();
        setImage(image);
    }
    public void act() 
    {
        platy=getY();
    }   
}
here is the link to the scenario: http://www.greenfoot.org/scenarios/9044
danpost danpost

2013/7/22

#
For one thing, it does not appear you are setting 'platy' to anything.
8bitorangejuice wrote...
it did not work for me (e.g. if(getOneObjectAtOffset(0,20,Platform.class)!=null){ ready=true; fall=false; setLocation(getexactX(),platy-20);}
This should be fine for the end of a fall (or no fall). What code would you think you would need to start a jump? Also, could you explain why you are using the 'Conector' class as a superclass for the 'Cube' class? (are you going to subclass 'Conector' with other classes? if so, what kind?).
8bitorangejuice 8bitorangejuice

2013/7/22

#
Hey Dan, I am using it as a superclass for all my other classes inorder to store static int's and not have the hassle of not doing it this way. if(getOneObjectAtOffset(0,20,Platform.class)!=null){ ready=true; fall=false; setLocation(getexactX(),platy-20);} if(ready=true&&Greenfoot.isKeyDown("w")) {yspeed=-5; ready=false; fall=true;} this code is not working, check out my scenario: http://www.greenfoot.org/scenarios/9044
danpost danpost

2013/7/22

#
Have a copy. Will look into.
danpost danpost

2013/7/22

#
I know, without attempting to run your scenario, that line 49, 55, and 59 in the Cube class have incorrect conditionals. You are using 'ready=true', 'fall=true' and 'fall=false' which will always return 'true', 'true', and 'false' consecutively. Anytime you use a single equal sign, you are setting the evaluated value on the right to the field on the left. For conditional equality, use two equal signs ('ready == true', 'fall == true' and 'fall == false'). For boolean fields, however, it is always better to just use the field itself ('ready', 'fall' and '!fall').
danpost danpost

2013/7/22

#
Something else I noticed was that you are adjusting the vertical location multiple times (see lines 37, 43 and 47; which all have '+gravity' in them). Just move the actor left for the 'a' key and right for the 'd' key. Then, add gravity to yspeed and move the actor down before checking for a platform and checking for jumping.
public void act()
{
    if(Greenfoot.isKeyDown("a")) // check left
    {
        setLocation(getexactX()-2,getexactY()); // move left
    }
    if(Greenfoot.isKeyDown("d")) // check right
    {
        setLocation(getexactX()+2,getexactY()); // move right
    }
    yspeed += gravity; // add gravity to speed
    setLocation(getexactX(), getexactY()+yspeed); // move vertically
    Actor platform = getOneObjectAtOffset(0, getImage().getHeight()/2, Platform.class);
    // etc.
}
However, the following should be removed:
if (fall == false)
{
    yspeed = 0;
}
The only time you may wish to zero the yspeed value is when an intersecting object exists (or it is on the platform or ground).
8bitorangejuice 8bitorangejuice

2013/7/22

#
Alright, thanks Dan, but now I have a problem where the cube wont leave the ground; can someone help me with that? By the way, I updated the code for the cube:
import greenfoot.*;
import java.awt.Color;
public class Cube extends Conector
{
    public boolean jump;
    private double gravity=0.2;
    private double yspeed;
    private double exactX;  
    private double exactY;   
    private boolean ready=true;
    public void addedToWorld(World world)  
    {
        GreenfootImage image=new GreenfootImage(25,25);  
        image.setColor(new Color(205,201,201,175));  
        image.fill(); 
        setImage(image);
}
    public void setLocation(int x, int y) {  
    exactX = x;  
    exactY = y;  
    super.setLocation(x, y);  
}        
    public void setLocation(double x, double y) {  
    exactX = x;  
    exactY = y;  
    super.setLocation((int) x, (int) y);  
}  
    public double getexactX() {  
    return exactX;  
}  
    public double getexactY() {  
    return exactY;  
}  
    public void act() 
    {
        setLocation(getexactX(),getexactY()+gravity);
        Actor platform = getOneObjectAtOffset(0, getImage().getHeight()/2, Platform.class);        
        int groundHeight = 20;
        int newY = platy - (groundHeight + getImage().getHeight())/2;          
        if(Greenfoot.isKeyDown("a"))
        {
            setLocation(getexactX()-2,getexactY());
        }
        if(Greenfoot.isKeyDown("d"))
        {
            setLocation(getexactX()+2,getexactY());
        }
        setLocation(getexactX(), getexactY()+yspeed); 
        yspeed+=gravity;
        if(ready==true&&Greenfoot.isKeyDown("w"))         
        {
            yspeed=-5; 
            ready=false;
        }
        if(getOneObjectAtOffset(0,20,Platform.class)!=null)
        {
            setLocation(getexactX(),platy-20);
            yspeed=0;
            ready=true;
        }
        
}}
I also updated the scenario: http://www.greenfoot.org/scenarios/9044
8bitorangejuice 8bitorangejuice

2013/7/22

#
Managed to solve it, thanks for the help. //closed
You need to login to post a reply.