Ajout d'étage en fonction de l'image ou comment optimiser la méthode pré-existante

Hay E:

Je crée un jeu avec Java pour un projet scolaire. J'ai actuellement une classe Player et je crée le sol, il va marcher à l'intérieur. En ce moment, je viens de faire une méthode Clamp où j'ajouter des sections différentes en fonction de l'image afin qu'il marche sur le « plancher ». Finalement, je les plates-formes qu'il doit être en mesure de sauter, mais tombe s'il manque. Ce code est actuellement ne fonctionne pas et je ne suis pas sûr de savoir comment s'y prendre. Y at-il un moyen plus facile ou plus efficace de faire le sol? Sinon, comment puis-je faire fonctionner les plates-formes?

Je l'ai déjà essayé d'utiliser une variable à la fin de la méthode pour tester si elle est une plate-forme ou non.

package com.sewd.sophomorevisitation.objects;

import java.awt.Graphics;
import java.awt.image.BufferedImage;

import com.sewd.sophomorevisitation.Background;
import com.sewd.sophomorevisitation.KeyListener;
import com.sewd.sophomorevisitation.Main;
import com.sewd.sophomorevisitation.sprites.SpriteAnimation;
import com.sewd.sophomorevisitation.sprites.SpriteSheet;

public class Player extends GameObject {

    Handler handler;
    static int times = 0;

    private static BufferedImage[] shreccRun = {SpriteSheet.getSprite(0, 0, 128, 106, 0, 0), SpriteSheet.getSprite(1, 0, 128, 106, 0, 0), SpriteSheet.getSprite(0, 0, 128, 106, 0, 0), SpriteSheet.getSprite(2, 0, 128, 106, 0, 0)};
    private static BufferedImage[] shreccJump = {SpriteSheet.getSprite(3, 0, 128, 106, 0, 0), SpriteSheet.getSprite(4, 0, 128, 106, 0, 0)};
    public static SpriteAnimation shreccRunAnimation = new SpriteAnimation(shreccRun, 5);
    public static SpriteAnimation shreccJumpAnimation = new SpriteAnimation(shreccJump, 5);
    public static SpriteAnimation animation = shreccRunAnimation;


    public Player(int x, int y, ID id) {
        super(x, y, id);
    }

    public void tick() {
        y += velY;

        if(x < 0) {
            death();
        }

        if(Main.started) {
            /*floor(1076, 1215, -500, Main.screenSize.width, 0, 972);
            floor(1215, 1374, -500, Main.screenSize.width, 0, 969);
            floor(1374, 1538, -500, Main.screenSize.width, 0, 936);
            floor(1538, 1667, -500, Main.screenSize.width, 0, 925);
            floor(1667, 3711, -500, Main.screenSize.width, 0, 919);*/
            floor(0, 473, -500, Main.screenSize.width, 0, (Main.screenSize.height/ 4) * 3 - 40);
            floor(473, 600, -500, Main.screenSize.width, 0, (Main.screenSize.height/ 4) * 3 - 70);
            floor(600, 775, -500, Main.screenSize.width, 0, (Main.screenSize.height/ 4) * 3 - 90);
            floor(775, 2900, -500, Main.screenSize.width, 0, (Main.screenSize.height/ 4) * 3 - 100);
            floor(2900, 2980, -500, Main.screenSize.width, 0, (Main.screenSize.height/ 4) * 3 - 140);
            floor(2980, 3060, -500, Main.screenSize.width, 0, (Main.screenSize.height/ 4) * 3 - 190);
            floor(2980, 3060, -500, Main.screenSize.width, 0, 873);
            floor(3060, 3073, -500, Main.screenSize.width, 0, 843);
            floor(3073, 3827, -500, Main.screenSize.width, 0, 813);
            floor(3827, 3857, -500, Main.screenSize.width, 0, 784);
            floor(3827, 10000, -500, Main.screenSize.width, 0, 784);

        }

        if(animation != shreccJumpAnimation) {
            animation.update();
        }else {
            if(times <= 5)animation.update();
            times++;
        }
    }

    public void death() {
        System.out.println("Dead");
    }

    public static void floor(int start, int stop, int minX, int maxX, int minY, int maxY) {
        if( start <= Background.x && Background.x < stop) Player.clamp(minX, maxX, minY, maxY);
    }

    public static void clamp(int minX, int maxX, int minY, int maxY) {
        if(x <= minX) x = minX;
        else if(x >= maxX) x = maxX;
        else if(y <= minY) {
            y = minY;
            KeyListener.isInAir = true;
            velY = -velY;
            if(velY == 0) velY = 10;
        }else if(y >= maxY) {
            y = maxY;
            KeyListener.isInAir = false;
            if(times > 0) {
                times = 0;
                animation.stop();
                animation.restart();
                animation = shreccRunAnimation;
                animation.start();
            }

        }else return;
    }

    public void render(Graphics g) {
        g.drawImage(animation.getSprite(), x - shreccRun[0].getWidth() - 50, y, shreccRun[0].getWidth() * 2, shreccRun[0].getHeight() * 2, null);
    }


}

Le sol doit fonctionner correctement dans tous les lieux où il n'y a pas des plates-formes. Il marche juste là-dessus. Dans le cas contraire, s'il atterrit sur une plate-forme dont il a besoin de rester jusqu'à ce que se termine la plate-forme, puis il tombe. Ou, s'il manque une plate-forme puis il tombe.

Hay E:

Deviner

public static boolean onFloor;


public void tick() {
    if(Main.isStarted()) {

        floor();

        if(!onFloor) {
            Player.setVelY(20);
        }else {
            Player.setVelY(0);
        }
    }
}

private void floor() {
    clamp(0, 5000, 824, "f");
    clamp(5000, 10000, 750, "p");
}

private void clamp(int start, int stop, int minY, String type) {
    if(x > start && x < stop) {
        if(Player.getY() != minY) {
            if(Player.getY() > minY) {
                if(type != "p") {
                    Player.setY(minY);
                    Player.setVelY(0);
                    onFloor = true;
                    return;
                }
            }
            if(!Player.jumping) {
                Player.setVelY(10);
                onFloor = false;
            }
        }else if(Player.getY() == minY) {
            Player.setY(minY);
            Player.setVelY(0);
            onFloor = true;
        }else if(Player.getY() > minY) {
            if(type != "p") {
                Player.setY(minY);
                Player.setVelY(0);
            }
        }
    }
}

Je suppose que tu aimes

Origine http://43.154.161.224:23101/article/api/json?id=215216&siteId=1
conseillé
Classement