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

2019/3/5

Character Falling Through Floor

1
2
Underdemon Underdemon

2019/3/5

#
Character falling too fast for detection to occur and falls through floor. Trying to reduce "private int acceleration = 1;" into double/float but error appears. Code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
 * Write a description of class Character here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Character extends Actor
{
    private int Speed = 0;
    private int acceleration = 1;
    private boolean jumping;
    private int jumpStrength = 11;
    private int speed = 2;
    /**
     * Act - do whatever the Character wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */

    public void act() 
    {
        Movement();
        checkFall();
    }    

    public void Movement()
    {
        if (Greenfoot.isKeyDown("d"))
        {
            move(3);
        }
        if (Greenfoot.isKeyDown("a"))
        {
            move(-3);
        }   
        if (Greenfoot.isKeyDown("w") && jumping == false)
        {
            Jump();
        }
   }

    public void Jump()
    {
        Speed = Speed - jumpStrength;
        jumping = true;
        Fall();
    }

    public void Fall()
    {
        setLocation(getX(), getY() + Speed);
        if (Speed <= 9)
        {
            Speed = Speed + acceleration;
        }
        jumping = true;
    }

    public void checkFall()
    {
        Level1 level1 = getWorldOfType(Level1.class);
        GreenfootImage img = level1.getBackground();
        if (Color.GREEN.equals(img.getColorAt(getX(), getY() + getImage().getHeight()/2)))
        {
            Speed = 0;
            jumping = false;
        }
        else
        {
            Fall();
        }
    }
}
P.S Working in v 3.0.4
Underdemon Underdemon

2019/3/5

#
Changing line 12 to this:
    private int acceleration = 1;
results in an error in line 55 at acceleration Error reads "incompatible types: possible lossy conversion from double to int"
danpost danpost

2019/3/5

#
Start by changing line 45 to:
Speed = -jumpStrength;
See if that helps.
Underdemon Underdemon

2019/3/5

#
danpost wrote...
Start by changing line 45 to:
Speed = -jumpStrength;
See if that helps.
Unfortunately not
danpost danpost

2019/3/5

#
Underdemon wrote...
Unfortunately not
Try this for line 64:
if (img.getColorAt(getX(), getY() + getImage().getHeight()/2).getGreen() > 128)
Underdemon Underdemon

2019/3/5

#
danpost wrote...
Underdemon wrote...
Unfortunately not
Try this for line 64:
if (img.getColorAt(getX(), getY() + getImage().getHeight()/2).getGreen() > 128)
Still doesn't work, this time it completely removes gravity
danpost danpost

2019/3/5

#
Underdemon wrote...
Still doesn't work, this time it completely removes gravity
Try 240 instead of 128.
Underdemon Underdemon

2019/3/5

#
danpost wrote...
Underdemon wrote...
Still doesn't work, this time it completely removes gravity
Try 240 instead of 128.
Tried a bunch of numbers (some negative), still doesn't work
Underdemon Underdemon

2019/3/5

#
Underdemon wrote...
Character falling too fast for detection to occur and falls through floor. Trying to reduce "private int acceleration = 1;" into double/float but error appears. Code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
 * Write a description of class Character here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Character extends Actor
{
    private int Speed = 0;
    private int acceleration = 1;
    private boolean jumping;
    private int jumpStrength = 11;
    private int speed = 2;
    /**
     * Act - do whatever the Character wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */

    public void act() 
    {
        Movement();
        checkFall();
    }    

    public void Movement()
    {
        if (Greenfoot.isKeyDown("d"))
        {
            move(3);
        }
        if (Greenfoot.isKeyDown("a"))
        {
            move(-3);
        }   
        if (Greenfoot.isKeyDown("w") && jumping == false)
        {
            Jump();
        }
   }

    public void Jump()
    {
        Speed = Speed - jumpStrength;
        jumping = true;
        Fall();
    }

    public void Fall()
    {
        setLocation(getX(), getY() + Speed);
        if (Speed <= 9)
        {
            Speed = Speed + acceleration;
        }
        jumping = true;
    }

    public void checkFall()
    {
        Level1 level1 = getWorldOfType(Level1.class);
        GreenfootImage img = level1.getBackground();
        if (Color.GREEN.equals(img.getColorAt(getX(), getY() + getImage().getHeight()/2)))
        {
            Speed = 0;
            jumping = false;
        }
        else
        {
            Fall();
        }
    }
}
P.S Working in v 3.0.4
Is it possible to change the variable on Line 12 to a decimal (so change the data type to a double or something like that) and maybe change something else so the character will decelerate more while falling, without any errors occuring
danpost danpost

2019/3/5

#
Underdemon wrote...
Is it possible to change the variable on Line 12 to a decimal (so change the data type to a double or something like that) and maybe change something else so the character will decelerate more while falling, without any errors occuring
I doubt that will not fix your issue.
Underdemon wrote...
Tried a bunch of numbers (some negative), still doesn't work
Only numbers that might work are in the range 129 to 255, inclusive (provided color detection is looking for a green color).
Underdemon Underdemon

2019/3/8

#
Sorry, I took long, I tried, it didn't work.
Underdemon Underdemon

2019/3/8

#
danpost wrote...
Underdemon wrote...
Is it possible to change the variable on Line 12 to a decimal (so change the data type to a double or something like that) and maybe change something else so the character will decelerate more while falling, without any errors occuring
I doubt that will not fix your issue.
Underdemon wrote...
Tried a bunch of numbers (some negative), still doesn't work
Only numbers that might work are in the range 129 to 255, inclusive (provided color detection is looking for a green color).
The reason i want to do that is to slow down the falling speed of a jump, because what I think is happening is that my character falls through the floor too fast, and so it cannot detect that he's touching the floor and then falls through it
danpost danpost

2019/3/8

#
Underdemon wrote...
The reason i want to do that is to slow down the falling speed of a jump, because what I think is happening is that my character falls through the floor too fast, and so it cannot detect that he's touching the floor and then falls through it
Are you saying that there IS (always) an actor that it is to land on? Is it the image of the actor that is green or is the background of the world green (or both)?
Underdemon Underdemon

2019/3/8

#
The background of the world is green
danpost danpost

2019/3/8

#
Underdemon wrote...
The background of the world is green
Can you upload your project with source for review?
There are more replies on the next page.
1
2