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

2017/1/20

moving through a level

jacquelinereilly jacquelinereilly

2017/1/20

#
the characters fall through the platform but the same code works on my other levels
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * This class Level3 displays the third level for the user to play.
 * 
 * Jacqueline Reilly 
 * January 18, 2017
 */
public class Level3 extends World
{
    Counter counter = new Counter ();
    PlanktonCounter PCounter = new PlanktonCounter ();
    /**
     * Constructor for objects of class Level3.
     * 
     */
    public Level3()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(950, 700, 1); 
        
        //Adding counter
        addObject(counter, 100, 100);
        addObject (PCounter, 800, 100);
        
        //Making background fit the screen
        GreenfootImage Bg = new GreenfootImage ("background.jpg");
        Bg.scale (getWidth (), getHeight ());
        setBackground (Bg);
        
        //Adding the platforms 
        PlatformLevel3 platform = new PlatformLevel3 ();
        addObject (platform, 160, 633);
        PlatformLevel3 platform1 = new PlatformLevel3 ();
        addObject (platform1, 501, 511);
        PlatformLevel3 platform2 = new PlatformLevel3 ();
        addObject (platform2, 792, 357);
       
        //Adding characters
        addObject (new MrKrabs(), 90, 518);
        addObject (new Plankton(), 188, 497);
        
        //Adding object for Mr. Krabs to collect
        KrabbyPatty krabbyPatty = new KrabbyPatty ();
        addObject (krabbyPatty, 488, 404);
        KrabbyPatty krabbyPatty1 = new KrabbyPatty ();
        addObject (krabbyPatty1, 865, 255);
        
        //Adding object for Plankton to collect
        FormulaBottle bottle = new FormulaBottle ();
        addObject (bottle, 300, 416);
        FormulaBottle bottle1 = new FormulaBottle ();
        addObject (bottle1, 681, 220);
    }
    public Counter getCounter(){
        return counter; 
    }
    
    public PlanktonCounter getPlankton() {
     return PCounter;
    }
}
Super_Hippo Super_Hippo

2017/1/20

#
You need to show the code of the classes which (try to) interact with the platforms.
jacquelinereilly jacquelinereilly

2017/1/20

#
these are characters
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * This class Plankton allows the user to move Plankton through the level, and collect the formu.
 * 
 * Jacqueline Reilly
 * January 18, 2017
 */
public class Plankton extends Actor
{
    public Plankton()
    {
        //Resizing image
        GreenfootImage image = getImage ();
        image.scale (image.getWidth ()-120, image.getHeight () -120);
        setImage (image);
    }
    private int vSpeed =0;
    private int acceleration =1;
    private boolean jumping;
    private int jumpStrength =16;
    private int speed =4;
    
    /**
     * Act - do whatever the MrKrabs wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        //Checkiing the platform is secure
        checkKey ();
        checkFall ();
        platformAbove ();
        kill();
}
    
    public void checkKey ()
    {
        if (Greenfoot.isKeyDown ("d")) 
        {
            //direction =1;
            setLocation (getX()+speed, getY());
        }
        if (Greenfoot.isKeyDown ("a"))
        {
            //direction =-1l
            setLocation (getX() - speed, getY());
        }
        if (Greenfoot.isKeyDown("w") && jumping == false)
        {
            jump ();
        }
}

public boolean platformAbove ()
{
    int spriteHeight = getImage ().getHeight ();
    int yDistance = (int) (spriteHeight/-2);
    Actor ceiling = getOneObjectAtOffset (0, yDistance, Platform.class);
    if (ceiling != null)
    {
        vSpeed = 1;
        bopHead (ceiling);
        return true;
    }
    else
    {
        return false;
    }
}

public void bopHead (Actor ceiling)
{
    int ceilingHeight = ceiling.getImage ().getHeight ();
    int newY = ceiling.getY () + (ceilingHeight + getImage ().getHeight())/2;
    setLocation (getX(), newY);
}

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

public boolean onGround ()
{
    int spriteHeight = getImage ().getHeight();
    int yDistance = (int)(spriteHeight/2) + 5;
    Actor ground = getOneObjectAtOffset (0, getImage ().getHeight()/2, Platform.class);
    if (ground == null)
    {
        jumping = true;
        return false;
    }
    else
    {
        moveToGround (ground);
        return true;
    }
}

public void moveToGround (Actor ground)
{
    int groundHeight = ground.getImage ().getHeight();
    int newY = ground.getY () - (groundHeight + getImage().getHeight())/2;
    setLocation (getX (), newY);
    jumping = false;
}

public void checkFall()
{
    if (onGround ())
    {
        vSpeed = 0;
    }
    else
    {
        fall ();
    }
}

public void jump ()
{
    vSpeed = vSpeed - jumpStrength;
    jumping = true;
    fall ();
}

//(Colin. J helped with this)
public void kill(){
        if(isTouching(FormulaBottle.class)){
            
           removeTouching(FormulaBottle.class);
    ((Level1) getWorld ()).PCounter.setScore ();
    ((Level2) getWorld ()).PCounter.setScore ();
}}

jacquelinereilly jacquelinereilly

2017/1/20

#
and character 2
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * This class MrKrabs allows the user to move Mr. Krabs through the level, and collect the krabby patty's.
 * 
 * Jacqueline Reilly
 * January 18, 2017
 */
public class MrKrabs extends Actor
{
    public MrKrabs()
    {
        
        GreenfootImage image = getImage ();
        image.scale (image.getWidth ()-675, image.getHeight () -780);
        setImage (image);
    }
    private int vSpeed =0;
    private int acceleration =1;
    private boolean jumping;
    private int jumpStrength =16;
    private int speed =4;
    
    /**
     * Act - do whatever the MrKrabs wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        //Checkiing the platform is secure
        checkKey ();
        checkFall ();
        platformAbove ();
        kill();
}
    
    public void checkKey ()
    {
        if (Greenfoot.isKeyDown ("right")) 
        {
            //direction =1;
            setLocation (getX()+speed, getY());
        }
        if (Greenfoot.isKeyDown ("left"))
        {
            //direction =-1l
            setLocation (getX() - speed, getY());
        }
        if (Greenfoot.isKeyDown("up") && jumping == false)
        {
            jump ();
        }
}

public boolean platformAbove ()
{
    int spriteHeight = getImage ().getHeight ();
    int yDistance = (int) (spriteHeight/-2);
    Actor ceiling = getOneObjectAtOffset (0, yDistance, Platform.class);
    if (ceiling != null)
    {
        vSpeed = 1;
        bopHead (ceiling);
        return true;
    }
    else
    {
        return false;
    }
}

public void bopHead (Actor ceiling)
{
    int ceilingHeight = ceiling.getImage ().getHeight ();
    int newY = ceiling.getY () + (ceilingHeight + getImage ().getHeight())/2;
    setLocation (getX(), newY);
}

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

public boolean onGround ()
{
    int spriteHeight = getImage ().getHeight();
    int yDistance = (int)(spriteHeight/2) + 5;
    Actor ground = getOneObjectAtOffset (0, getImage ().getHeight()/2, Platform.class);
    if (ground == null)
    {
        jumping = true;
        return false;
    }
    else
    {
        moveToGround (ground);
        return true;
    }
}

public void moveToGround (Actor ground)
{
    int groundHeight = ground.getImage ().getHeight();
    int newY = ground.getY () - (groundHeight + getImage().getHeight())/2;
    setLocation (getX (), newY);
    jumping = false;
}

public void checkFall()
{
    if (onGround ())
    {
        vSpeed = 0;
    }
    else
    {
        fall ();
    }
}

public void jump ()
{
    vSpeed = vSpeed - jumpStrength;
    jumping = true;
    fall ();
}

//(Colin. J helped with this)
public void kill(){
        if(isTouching(KrabbyPatty.class)){
            
           removeTouching(KrabbyPatty.class);
    ((Level1) getWorld ()).counter.setScore ();
    ((Level2) getWorld ()).counter.setScore ();
}}


} 
jacquelinereilly jacquelinereilly

2017/1/20

#
platforms
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * This class PlatformLevel3 displays the platforms for level 3 only.
 * 
 * Jacqueline Reilly
 * January 18, 2017
 */
public class PlatformLevel3 extends Actor
{
     //Resizing the image
    public PlatformLevel3 ()
    {
        //Resizing image
        GreenfootImage image = getImage ();
        image.scale (image.getWidth ()-180, image.getHeight () -32);
        setImage (image);
    }
Super_Hippo Super_Hippo

2017/1/20

#
So what you are saying is that in Level 1, so intersection of the characters work correctly, but in Level 2 the characters go through them like there wasn't a platform? Usually this is caused by too small platform or too fast speed, so the object moves over the platform in one act cycle without "touching" it.
jacquelinereilly jacquelinereilly

2017/1/20

#
level 1 and level 2 work. it is level 3 that they go right through when i press run this could be the size, i did shrink the platforms in level 3. I will adjust that and see if it works
jacquelinereilly jacquelinereilly

2017/1/20

#
it still does the same thing
jacquelinereilly jacquelinereilly

2017/1/20

#
the characters go straight down when i press run
Nosson1459 Nosson1459

2017/1/20

#
Your getOneObjectAtOffset is looking for Platform.class not PlatformLevel3.class. If the only difference between the platforms is the image, then you should probably just have a parameter in ONE platform class for both types of platforms. Like:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * This class Platform displays the platforms for levels 1, 2, and 3.
 * 
 * 
 * 
 */
public class Platform extends Actor
{
    //Resizing the image
    public Platform (int type)
    {
        if (type == 1)
        {
            // code for platform for levels 1 and 2
        }
        if (type == 2)
        {
            //Resizing image
            GreenfootImage image = getImage ();
            image.scale (image.getWidth ()-180, image.getHeight () -32);
            setImage (image);
        }
    }
jacquelinereilly jacquelinereilly

2017/1/20

#
ahh ! thank you thank you
Nosson1459 Nosson1459

2017/1/20

#
I didn't test your code because it will be too complicated to remake your scenario so I left the testing to you. I ASSUMED you didn't test it yet because you replied about 4 minutes after me.
jacquelinereilly jacquelinereilly

2017/1/20

#
haha yeah, but i got it. thank you :)
You need to login to post a reply.