Slick Forums

Discuss the Slick 2D Library
It is currently Wed Nov 20, 2019 5:52 pm

All times are UTC




Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: Wed Apr 26, 2017 8:25 pm 
Offline

Joined: Sat Apr 22, 2017 3:40 pm
Posts: 4
I made a platform class to make it easier to place out platforms on my map in different sizes, only problem is I need the collidesWith function in World.java to also check the platforms I make with the platform class. So far I've got nowhere.

This is the class where the world is created, I call new platforms with platform1 = new Platform(600,300,100,50); < But when I do it like this, I'm unable to implement the platform in Platform.java into the collidesWith function at the bottom. This function is used in Player.java to make player have collision with the world, so without the platforms in there. The player will be unable to crash, and stand on the platforms.

Code:
public class World extends Scene {

    int speed = 12;

    private Shape /*platform,*/ levelBase, baseR, baseL;

    private Platform platform1;
    private Platform platform2;


    public World() {
        super();
        setPriority(2);
    }

    protected void CustomRender(GameContainer gameContainer, Graphics graphics) throws SlickException
    {
        graphics.clear();

        graphics.setColor(Color.white);

        // Base of level
        graphics.draw(levelBase);
        graphics.fill(levelBase);
        graphics.draw(baseR);
        graphics.fill(baseR);
        graphics.draw(baseL);
        graphics.fill(baseL);

        // Platforms
        platform1.render(gameContainer, graphics);
        platform2.render(gameContainer, graphics);
    }

    protected void CustomUpdate(GameContainer gameContainer, int i) throws SlickException {

    }

    public void init(GameContainer gameContainer) throws SlickException {

        levelBase = new Rectangle(0,680,1024,100);
        baseR = new Rectangle(0 - 100,0,100,2560);
        baseL = new Rectangle(1024,0,100,2560);

        platform1 = new Platform(600,500,100,50);
        platform2 = new Platform(200,100,100,50);


        platform1.init(gameContainer);
        platform2.init(gameContainer);
    }

    public boolean collidesWith(Shape s)
    {
        return levelBase.intersects(s) ||
                baseR.intersects(s) ||
                baseL.intersects(s);
    }

    public String toString()
    {
        return "Sence3";
    }
}




Javafile for the player

Code:
public class Player extends Scene {

    //private Animation player;
    public Shape playerHitboxR, playerHitboxL, slashHitboxR, slashHitboxL, flagHitbox;

    private World world;
    private Platform platform;

    public Player(World world) {
        super();
        setPriority(3);
        this.world = world;
        System.out.println("Player Ran");
    }

    protected void CustomUpdate(GameContainer gameContainer, int i) throws SlickException
    {
   if( world.collidesWith(playerHitboxR) || world.collidesWith(playerHitboxL)) {
            playerHitboxR.setY( playerHitboxR.getY() - yPlayer );
            playerHitboxL.setY( playerHitboxL.getY() - yPlayer );
            slashHitboxR.setY( slashHitboxR.getY() - yPlayer );
            slashHitboxL.setY( slashHitboxL.getY() - yPlayer );
            yPlayer = 0;
        }
   
   if(world.collidesWith(playerHitboxR) || world.collidesWith(playerHitboxL)) {
            playerHitboxR.setX( playerHitboxR.getX() - xPlayer );
            playerHitboxL.setX( playerHitboxL.getX() - xPlayer );
            slashHitboxR.setX( slashHitboxR.getX() - xPlayer );
            slashHitboxL.setX( slashHitboxL.getX() - xPlayer );
            xPlayer = 0;
        }
    }
}



And here's the class where the non crashing platforms are made:

Code:
public class Platform extends Scene {
    private int platPosX;
    private int platPosY;
    private int platSizeWidth;
    private int platSizeHeight;

    private Shape platform;

    public Platform(int platPosX, int platPosY, int platSizeWidth, int platSizeHeight) {
        super();
        this.platPosX = platPosX;
        this.platPosY = platPosY;
        this.platSizeWidth = platSizeWidth;
        this.platSizeHeight = platSizeHeight;

        System.out.println(platPosY);
    }

    protected void CustomRender(GameContainer gameContainer, Graphics graphics) throws SlickException
    {
        graphics.draw(platform);
        graphics.fill(platform);
    }

    protected void CustomUpdate(GameContainer gameContainer, int i) throws SlickException {

    }

    public void init(GameContainer gc) throws SlickException {
        platform = new Rectangle(platPosX,platPosY,platSizeWidth,platSizeHeight);
    }
   
    // Getters and Setters here VV
}


I have a picture of the game here: http://www.gauteabrahamsen.com/wip/5xzr ... ag8myv2gam as you see the player just falls through the platform.


Top
 Profile  
 
PostPosted: Fri Apr 28, 2017 1:26 pm 
Offline

Joined: Sat Apr 22, 2017 3:40 pm
Posts: 4
We ended up just making the platforms straight into the World class, little bit more messy but it works.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC


Who is online

Users browsing this forum: MSN [Bot] and 178 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group