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

2017/2/27

Putting down a platform

SachaTb SachaTb

2017/2/27

#
Hello, i need to add a platform or block for my player to jump on or stop when he hits the block. Here is my code,
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 

public class Shroom extends Actor
{
    public int gravity = 0;
    GifImage gifLeft = new GifImage("Shroom-Flipped.gif");
    GifImage gifRight = new GifImage("Shroom-Cropped.gif");
    GifImage gifImage = gifRight;
    private boolean jumping;
     public void act()
    {
        gravity -= 1;
        setLocation(getX(),getY()-gravity);
        int speed = 3;
        setImage("shroom-cropped0.png"); // image when not moving
    
    if(Greenfoot.isKeyDown("a"))
    {
        setLocation(getX() - speed, getY());
        gifImage = gifLeft;
        setImage(gifImage.getCurrentImage());
    }
    if(Greenfoot.isKeyDown("d"))
    {
        setLocation(getX() + speed, getY());
        gifImage = gifRight;
        setImage(gifImage.getCurrentImage());
    }
    if(Greenfoot.isKeyDown("w") && jumping == false)
    {
     if (getY() > 600)
     {
     gravity = 18;
     }
    }
    }
  }
 
Hope someone can help me! :)
danpost danpost

2017/2/27

#
SachaTb wrote...
i need to add a platform or block for my player to jump on or stop when he hits the block.
What do you have for a Platform class? What have you tried as far as the player jumping on or hitting a platform?
SachaTb SachaTb

2017/2/27

#
danpost wrote...
What do you have for a Platform class? What have you tried as far as the player jumping on or hitting a platform?
I haven't try'd anything with having a platform, i only have the actor called Platform
danpost danpost

2017/2/27

#
SachaTb wrote...
I haven't try'd anything with having a platform, i only have the actor called Platform
There are two places where you will need to check for touching a platform. After line 14. where you move vertically and after line 29, immediately after possibly moving horizontally left or right. You will need to use the value of 'gravity' to determine whether touching from above (when 'gravity' is less than zero) or from below (when 'gravity' is greater than zero).
You need to login to post a reply.