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

2022/1/22

Actor going through objects from underneath (Help pls)

Axcess_Ha Axcess_Ha

2022/1/22

#
I'm making a side-scrolling ninja turtle game and my actor keeps going through the platforms from the bottom but stays on it when it goes from the top. How do I make the actor fall back down when it hits its head on the bottom of the platform? (First game by the way and I've got 6 hours left to finish it, please help) The character (Mikey):
public class Mikey extends Actor
{
    private int upSpeed = 0;
    private int accelerataion = 1;
    private int jumpDistance= -20;
    private int collect = 0;
    public Mikey()
        {
        GreenfootImage image = getImage();
        image.scale(image.getWidth() - 440, image.getHeight() - 490);
        setImage(image);
    }
    
    /**
     * Act - do whatever the Mikey wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        moveMikey();
        checkFalling();
        collect();
    }
    
    private void fall()
    {
        setLocation(getX(), getY() + upSpeed);
        upSpeed = upSpeed + accelerataion;
    }
    
    public void moveMikey()
    {
        if(Greenfoot.isKeyDown("right"))
        {
            move(6);
        }
        if(Greenfoot.isKeyDown("left"))
        {
            move(-6);
        }  
        if (Greenfoot.isKeyDown("space")&&(onPlatform()==true || onGround() == true))
        {
            upSpeed = jumpDistance;
            fall();
        }
    }
    
    boolean onPlatform()
    {
        Actor bottom = getOneObjectAtOffset(0, getImage().getHeight()/2, Platform.class);
        return bottom != null;
    }
    boolean onGround()
    {
        Actor bottom = getOneObjectAtOffset(0, getImage().getHeight()/2, Ground.class);
        return bottom != null;
    }
    
    public void checkFalling()
    {
        if (onPlatform()== false|| onGround() == true)
        {
            fall();
        }
        if (onPlatform() == true|| onGround() == true)
        {
            upSpeed = 0;
        }
    }
}
The platform(red bricks):
public class Platform extends Actor
{
    public Platform()
    {
        getImage().scale(getImage().getWidth()*2,getImage().getHeight()/4);
    }
    /**
     * Act - do whatever the Floors wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        move (-2);
        if (getX() == 0) 
        {
            setLocation(getWorld().getWidth() - 1, getY());
        }
    }
}
Background:
public class Background extends World
{
    /**
     * Constructor for objects of class Background.
     * 
     */
    public Background()
    {    
        super(1080, 550, 1); 
        prepare();
    }
private void prepare()
    {
        Building building = new Building();
        addObject(building,1526,192);
        building.setLocation(1009,280);
        
        Platform platform = new Platform();
        addObject(platform,830,120);
        Platform platform2 = new Platform();
        addObject(platform2,189,138);
        Platform platform3 = new Platform();
        addObject(platform3,534,302);
        Platform platform4 = new Platform();
        addObject(platform4,846,435);
        Platform platform5 = new Platform();
        addObject(platform5,213,462);
        Platform platform6 = new Platform();
        addObject(platform6,958,341);
        
        platform3.setLocation(635,291);
        platform4.setLocation(871,129);
        platform5.setLocation(898,419);
        platform2.setLocation(351,443);
        platform.setLocation(413,120);
        platform6.setLocation(140,233);
        
        Mikey mikey = new Mikey();
        addObject(mikey,315,297);
        mikey.setLocation(58,423);
    }
}
cake_9098 cake_9098

2022/1/22

#
I need help with this too!
Super_Hippo Super_Hippo

2022/1/22

#
‘onPlatform’ and ‘onGround’ only check for actors below mikey. You also need to check for platforms above mikey and potentially also right/left of mikey to not move into a platform from the side.
Axcess_Ha Axcess_Ha

2022/1/22

#
@Super_Hippo Ohhh, do you know how I could implement that in this code?
danpost danpost

2022/1/23

#
@Axcess_Ha, line 61 in your Mikey class looks suspect. I doubt you want mikey to fall when onGround returns true. @Both of you, I have a Jump and Run Demo w/Moving Platform scenario you can look at.
cake_9098 cake_9098

2022/1/23

#
danpost wrote...
@Axcess_Ha, line 61 in your Mikey class looks suspect. I doubt you want mikey to fall when onGround returns true. @Both of you, I have a Jump and Run Demo w/Moving Platform scenario you can look at.
Do you have a link where we can go inside the scenerio and look at the code? I cant see the "open in greenfoot" option
danpost danpost

2022/1/23

#
cake_9098 wrote...
Do you have a link where we can go inside the scenerio and look at the code? I cant see the "open in greenfoot" option
Run the scenario and click on the buttons along the bottom of the scenario window.
cake_9098 cake_9098

2022/1/23

#
danpost wrote...
cake_9098 wrote...
Do you have a link where we can go inside the scenerio and look at the code? I cant see the "open in greenfoot" option
Run the scenario and click on the buttons along the bottom of the scenario window.
thanks so much!
Axcess_Ha Axcess_Ha

2022/1/23

#
Thanks Danpost! I ended up changing it up while I was waiting for a response but I have a new question now, for which I'll post as a new discussion. Please check it out whenever you can! :)
You need to login to post a reply.