Создание нескольких платформ для персонажа - PullRequest
0 голосов
/ 06 октября 2018

Я пытаюсь создать простую игру, в которой мой персонаж может прыгать на несколько платформ, чтобы попытаться добраться до вершины, немного похоже на игру в паркур в P5.JS.Однако я не могу создать надежную платформу для персонажа, на которой можно прыгать.

Вот мой код:

https://repl.it/@AadilSaiyad/EdgeDetection06-2

var groundY = 570;
var playerX = 140;
var playerY = 100;
var playerWidth = 20;
var playerHeight = 50;
var playerSpeedY = 0;

// introduce a speed increment value for the left-right player movement
var playerSpeedX = 8;

//only allow jumping if the player is not already jumping
var jumping = true;

function setup() {
  createCanvas(600, 600);
 }

function draw() {
  background(64);

  //draw the ground
  stroke(255);
  line(0, groundY, width, groundY);

  //move the player
  playerY += playerSpeedY;
  if (keyIsDown(37)) {
    if (playerX < 0) {
      playerX == 0;
    }
    else {
      // remember to indent your condition blocks
      //playerX -= 8;
      playerX -= playerSpeedX;
    }
  }
  if (keyIsDown(39)) {
    if (playerX >= (width - playerWidth)) {
      playerX == width - playerWidth;
    }
    else {
      // remember to indent your condition blocks
      //playerX += 8;
      playerX += playerSpeedX;
    }
  }
 
  //Stops player from going out of top
  if (playerY < 0  || playerY > height) {
    // why 0.5? should use -1 to change direction
    // check out the difference this will make!
    playerSpeedY *= -1;
  }



  //is the player colliding with the ground?
  if (playerY + playerHeight > groundY) {

    //snap the player's bottom to the ground's position
    playerY = groundY - playerHeight;

    //allow jumping again
    jumping = false;
    //stop the player falling
    playerSpeedY = 0;

  }
  //player is not colliding with the ground
  else {
    //gravity accelerates the movement speed
    playerSpeedY ++;
  }



  //draw the player rectangle
  rect(playerX, playerY, playerWidth, playerHeight);
}

function keyPressed() {
  
  
  if (!jumping && keyCode === 32) {
    
    //going up
    playerSpeedY = -15;
    
    //disallow jumping while already jumping
    jumping = true;
  }

  //you can only jump if you aren't already jumping
  
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.6.1/p5.js"></script>
...