Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

180
Vistas
How to stop an object from jumping past a certain point?

Catbus game

I'm trying to make my cat only jump once until it lands back on the ground. I've tried to add a statement that makes it only go to a certain point, but the velocity keeps working against it. It begins to act like a basketball that has been bounced to much. I wouldn't want to add a collider (even though I have debated it). It would just make it worse...

The code is as follows:

let img; //background
var bgImg; //also the background
var x1 = 0;
var x2;

var scrollSpeed = 4; //how fast background is

let bing; //for music

let cat;

var mode; //determines whether the game has started

let gravity = 0.2; //jumping forces
let velocity = 0.1;
let upForce = 6;

let startY = 730; //where cat bus jumps from
let startX = 70;

var font1; //custom fonts
var font2;

p5.disableFriendlyErrors = true; //avoids errors

function preload() {
  bgImg = loadImage("backgwound.png"); //importing background

  bing = loadSound("catbus theme song.mp3"); //importing music

  font1 = loadFont("Big Font.TTF");

  font2 = loadFont("Smaller Font.ttf");
}

function setup() {
  createCanvas(1000, 1000); //canvas size

  img = loadImage("backgwound.png"); //background in

  x2 = width;

  bing.loop(); //loops the music

  cat = {
    //coordinates for catbus
    x: startX,
    y: startY,
  };

  catGif = createImg("catgif.gif"); //creates catbus
  catGif.position(cat.x, cat.y); //creates position
  catGif.size(270, 100); //creates how big

  mode = 0; //game start
  textSize(50); //text size
}

function draw() {
  let time = frameCount; //start background loop

  image(img, 0 - time, 0);

  image(bgImg, x1, 2, width, height);
  image(bgImg, x2, 2, width, height);

  x1 -= scrollSpeed;
  x2 -= scrollSpeed;

  if (x1 <= -width) {
    x1 = width;
  }
  if (x2 <= -width) {
    x2 = width;
  } //end background loop

  fill("white"); //text colour
  if (mode == 0) {
    textSize(20);
    textFont(font1);
    text("press SPACE to start the game!", 240, 500); //what text to type
  }

  if (mode == 0) {
    textSize(35);
    textFont(font2);
    text("CATBUS BIZZARE ADVENTURE", 90, 450); //what text to type
  }

  cat.y = cat.y + velocity; //code for jumping
  velocity = velocity + gravity;

  if (cat.y > startY) {
    velocity = 0;
    // cat.y = startY;
  }

  catGif.position(cat.x, cat.y);
}

function keyPressed() {
  if (keyCode === 32) {   //spacebar code
   // if ((cat.y = 730)) {
     // cat.y > 730;
      mode = 1;
      velocity += -upForce;
    }
 // }
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can simply do this to the keyPressed function

  function keyPressed() {
      if (keyCode === 32 && velocity == 0) {   //spacebar code
       // if ((cat.y = 730)) {
         // cat.y > 730;
          mode = 1;
          velocity += -upForce;
        }
      }
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda