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

2017/3/2

Platforms for my game

SachaTb SachaTb

2017/3/2

#
Hey, i need platforms for my game, i have the actor called Platform all i need is the code for my character to stand on it. Main character 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;
    
     public void act()
   {
        gravity -= 1;
        setLocation(getX(),getY()-gravity);
        setImage("shroom-cropped0.png"); // image when not moving
        checkKeys();
        checkDead();
  }
  public void checkKeys()
  {
      int speed = 3;
      
      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"))
    {
     
     if (getY() > 600)
     {
     gravity = 18;
     }
     }
     if("space".equals(Greenfoot.getKey()))
     {
        fire();
     }
  }
   public void fire() 
  {
    Bullet bullet = new Bullet();
    getWorld().addObject(bullet, getX(), getY());
  } 
  public void checkDead()
  {
    if (getX() >= 834 && getX() <= 1078 && getY() >= 714)
   {
       World world;
       world = getWorld();
       Ded ded = new Ded();
       world.addObject(ded,715,524);
        Greenfoot.stop();
   }     
      
  }
}
Please help if you can!
danpost danpost

2017/3/2

#
For two actors to interact, usually proximity is involved. In this case, actual contact (touching or intersecting). That is where you must begin. Make use of the Actor class API documentation to see what you can use from it. Then, at least begin to build some code that you think might work. If you are finding that difficult. Break the problem down into simple steps -- like for what is required (1) movement of player (horizontal or vertically -- not both at the same time); (2) contact with platform (direction of movement can determine which side of the platform was hit); (3) repositioning of player so it no longer intersects with the platform; (4) if vertical movement causes contact, kill vertical speed (something similar will need to be done with horizontal speed if not controlled by user). Work one step at a time. Ask for help on specific steps when needed. For the first step, you should combine the left and right movement. You can replace lines 22 through 33 with something like this:
int dir = 0;
if(Greenfoot.isKeyDown("a")) dir--;
if(Greenfoot.isKeyDown("d")) dir++;
if (dir != 0)
{
    setLocation(getX()+speed*dir, getY());
    gifImage = dir < 0 ? gifLeft : gifRight;
    setImage(gifImage.getCurrentImage());
    // collision checking for horizontal movement here
}
This way, you have the direction available to determine where to reposition the player if encountering a platform. For vertical movement, you have the sign of the value of 'gravity' for the direction.
You need to login to post a reply.