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

2017/2/28

need help making terrain

ninjaFI ninjaFI

2017/2/28

#
@danpost pls help me
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class platform here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class platform extends Actor
{
    int frame = 0;
    private int vSpeed = 0;
    private int spriteHeight = getImage().getHeight();
    private int spriteWidth = getImage().getWidth();
    private int lookForGroundDistance = (int)spriteHeight/2;
    private int lookForEdge = (int)spriteWidth/2;
    private int speed = 1;
    
    /**
     * Act - do whatever the platform wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    { 
       tryToHoldPlayer();
        checkEdge();
    }
      public void checkEdge()
    {
        if(getX() < 10 || getX() > getWorld().getWidth() - 10) speed *= -1;
    }
    public void tryToHoldPlayer()
    {
        if (canSee(CopyOfPlayer1.class) )
        {
            //Hold Player Up
        }
    }
    public boolean canSee(Class clss)
    {
        return getOneObjectAtOffset(0, 0, clss) != null;        
    }
trying to make this hold my player up , its not working
danpost danpost

2017/2/28

#
Do not have the platform try to hold up the player. Have the player stay on the platform. It is the player that moves -- not the platform (at least there is no movement code in the class given above). As is, your platform class should just be this:
public class platform extends greenfoot.Actor{}
Yes, that is the code for the entire platform class. What this says is that the platform just exists -- it does not do anything else. Now, if you decide to give movement to the platforms, then you can deal with keeping any object that may be upon it moving with it in this class (because then, the platform is making those object move). But, until then, everything should be dealt with in the class of the other objects.
ninjaFI ninjaFI

2017/3/1

#
@danpost it didnt work my guy still falls straight through , here is my actors code
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class CopyOfPlayer1 here.
 * 
 * @author (Shola Olateju) 
 * @version (23/2/17)
 */
public class CopyOfPlayer1 extends Actor
{ 
    GifImage myGif = new GifImage("ekko1.gif");
    public int gravity = 0;
    /**
     * Act - do whatever the CopyOfsmaller2 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
      gravity -= 1;
      setLocation(getX(),getY()-gravity);
      setImage( myGif.getCurrentImage() );
      int speed = 3;
        
      if(Greenfoot.isKeyDown("A"))
      setLocation(getX() - speed, getY());
      if(Greenfoot.isKeyDown("D"))
      setLocation(getX() + speed, getY());
      if (Greenfoot.isKeyDown("W"))
        {
            // I Borrowed This Code Below
            
            if (getY() > 380)
            {
                gravity = 23;
            }
            //End of Borrowed Code
    }
      checkFire();
    //leave the rest of your player's act method alone
 }
//after the "act()" method, add a new method:
 public void checkFire()
 {
   if(Greenfoot.isKeyDown("space")) {
       getWorld().addObject(new Bullet(), getX(), getY());
   }
 }
}
My thought is to add something into this code which checks for the platform coordinates, and then changes the actor's coordinates from where it jumps. Do you have any advice on this?
danpost danpost

2017/3/1

#
There are multiple collision checking methods provided in the Actor class. I would start by using one of them. Check the greenfoot.Actor class API documentation to see what methods are available.
You need to login to post a reply.