javascript - 如何阻止物体跳过某个点?

标签 javascript p5.js

Catbus game

我试图让我的猫只跳一次,直到它回到地面。我试图添加一个语句,让它只到达某个点,但速度一直在反对它。它开始表现得像一个被弹得太多的篮球。我不想添加对撞机(尽管我已经对此进行了辩论)。这只会让事情变得更糟......

代码如下:

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;
    }
 // }
}

最佳答案

您可以简单地对 keyPressed 函数执行此操作

  function keyPressed() {
      if (keyCode === 32 && velocity == 0) {   //spacebar code
       // if ((cat.y = 730)) {
         // cat.y > 730;
          mode = 1;
          velocity += -upForce;
        }
      }

关于javascript - 如何阻止物体跳过某个点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69635277/

相关文章:

javascript - KnockoutJS 在值后加载选项时丢失以前的值

javascript - jeditable - 在运行时禁用它

rendering - p5 中 createGraphics() 缓冲区上的 tint()

javascript - P5 - 从左向右和向后弹球

javascript - 我如何将 p5.js 与 nuxt 集成

javascript - 在 p5.js 中绘图时转换坐标

javascript - 映射一个数组并根据对象键返回一个新数组

javascript - 如何仅更新Google Firebase数据库中的一个属性

javascript - typescript 键盘事件 : argument of type 'Event' is not assignable to parameter of type 'KeyboardEvent'

javascript - 需要帮助使每个猫头鹰对象在 Canvas 上自行弹跳,而不是在同一路径上弹跳