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

2013/4/3

Problems with detection

JetLennit JetLennit

2013/4/3

#
Whenever this person touches the ground or the top he stops... what is wrong with my code?
public int ySpeed;
    public void act() 
    {
        if(getY() > 10 && getY() < 363) setLocation(getX(), getY() + ySpeed);
        if(Greenfoot.isKeyDown("space") && getY() > 10)
        {
            ySpeed --;
        }
        else if(getY() < 363)
        {
            ySpeed ++;
        }         
        else
        {
            setLocation(getX(), getY());
        }
        
    }    
erdelf erdelf

2013/4/3

#
you never move the player when he is at the ground or the top why are limiting the moving section for movement?
JetLennit JetLennit

2013/4/3

#
Because he would partially go off screen
erdelf erdelf

2013/4/3

#
you should check if the player is beyond or below that limit if he is, you should move him in the limit
JetLennit JetLennit

2013/4/3

#
this
if(getY() > 10 && getY() < 363) setLocation(getX(), getY() + ySpeed);
        else if(getY() > 363) setLocation(getX(), 365);
        else if(getY() < 10) setLocation(getX(), 11);
        if(Greenfoot.isKeyDown("space") && getY() > 10)
        {
            ySpeed --;
        }
        else if(getY() < 363)
        {
            ySpeed ++;
        }         
        else
        {
            setLocation(getX(), getY());
        }
any other ways to do this?
erdelf erdelf

2013/4/3

#
looks good, but if he falls down, he will be stuck there. he will constantly be moved between the ycoordinates 11 and ySpeed
JetLennit JetLennit

2013/4/3

#
exactly.... any good ways to do this?
erdelf erdelf

2013/4/3

#
maybe you should remove the limit for the space command, so you can use it below it
JetLennit JetLennit

2013/4/3

#
im not following you...
erdelf erdelf

2013/4/3

#
I mean, the command that is triggered with space, remove the limit so you can use it in the stuck place
JetLennit JetLennit

2013/4/3

#
I have done this
public int ySpeed;
    public void act() 
    {
        setLocation(getX(), getY() + ySpeed);
        if(Greenfoot.isKeyDown("space") && getY() > 10)
        {
            ySpeed --;
        }
        else if(Greenfoot.isKeyDown("space") && getY() > 363)
        {
            ySpeed --;
        }
        else if(getY() < 363)
        {
            ySpeed ++;
        }         
        else
        {
            setLocation(getX(), getY());
        }
    }    
but now it falls through the bottom and builds up more and more gravity
danpost danpost

2013/4/3

#
Instead of trying to limit to y range at the beginning, just adjust the y location at the end:
ySpeed++;
if (Greenfoot.isKeyDown("space")) ySpeed = -10; // adjust value
setLocation(getX(), getY()+ySpeed);
if (getY()<10) setLocation(getX(), 10);
if (getY()>363)
{
    setLocation(getX(), 363);
    ySpeed = 0;
}
}
erdelf erdelf

2013/4/3

#
what is this for? you are asking if the player is above 10 and if not if he is about 363 which is above 10.
        if(Greenfoot.isKeyDown("space") && getY() > 10)  
        {  
            ySpeed --;  
        }  
        else if(Greenfoot.isKeyDown("space") && getY() > 363)  
        {  
            ySpeed --;  
        }  
just change remove the limit and change the lines to this:
       if(Greenfoot.isKeyDown("space") )  
        {  
            ySpeed --;  
        } 
Edit: I was writing at the same time as danpost
danpost danpost

2013/4/3

#
You can also set the speed to zero when at the highest location:
ySpeed++;
if (Greenfoot.isKeyDown("space")) ySpeed = -10; // adjust value
setLocation(getX(), getY()+ySpeed);
if (getY()<10)
{
    setLocation(getX(), 10);
    ySpeed = 0;
}
if (getY()>363)
{
    setLocation(getX(), 363);
    ySpeed = 0;
}
This wil remove the delay between the time the "space" key is released and the player starts to fall.
You need to login to post a reply.