简体   繁体   English

使用 javascript p5 play 努力创建流畅的玩家精灵运动

[英]Struggling to create smooth player sprite movement using javascript p5 play

I'm building a project where I have a player sprite moving to dodge oncoming enemy sprites.我正在构建一个项目,其中我有一个玩家精灵移动以躲避迎面而来的敌人精灵。 Got the basic movement of sprite input down from the example here ( https://creative-coding.decontextualize.com/making-games-with-p5-play/ ), but I hoped to find a way to make it so that pressing up and left would make it go up and to the left at the same time instead of just moving upwards then to the left right after.从这里的例子( https://creative-coding.decontextualize.com/making-games-with-p5-play/ )中得到了精灵输入的基本动作,但我希望找到一种方法来制作它,以便按下up and left 将使它同时向上和向左移动 go 而不是仅向上移动然后向左移动。

Also want to know if it's possible to have the sprite move in a way that looks smoother like it is being affected by friction like in this example here ( https://editor.p5js.org/leaves2thedark/sketches/KW_P5qqLK , also taken from same website as before), only using keyboard inputs instead of mouse cursor positions.还想知道是否有可能让精灵以一种看起来更平滑的方式移动,就像它受到摩擦的影响一样,就像这里的这个例子( https://editor.p5js.org/leaves2thedark/sketches/KW_P5qqLK ,也取自与以前相同的网站),仅使用键盘输入而不是鼠标 cursor 位置。 Thanks yall.谢谢你们。

    let spr;
function setup() {
  createCanvas(400, 400);
  spr = createSprite(
    width/2, height/3, 40, 40);
  spr.shapeColor = color(255);
}
function draw() {
  background(50);
  fill(255);
  noStroke();
  textAlign(CENTER, CENTER);
  text("use arrow keys, or SPACE to stop",
    width/2, height*0.67);
  drawSprites();
}
function keyPressed() {
  if (keyCode == RIGHT_ARROW) {
    spr.setSpeed(2.5, 0);
  }
  else if (keyCode == DOWN_ARROW) {
    spr.setSpeed(2.5, 90);
  }
  else if (keyCode == LEFT_ARROW) {
    spr.setSpeed(2.5, 180);
  }
  else if (keyCode == UP_ARROW) {
    spr.setSpeed(2.5, 270);
  }
  else if (key == ' ') {
    spr.setSpeed(0, 0);
  }
  return false;
}
```

You need to register/unregister the active keys.您需要注册/取消注册活动密钥。 Something like this:像这样:

 let activeKeys; let spr; function setup() { createCanvas(400, 400); spr = createSprite( width/2, height/3, 40, 40); spr.shapeColor = color(255); activeKeys = { [UP_ARROW]: false, [RIGHT_ARROW]: false, [DOWN_ARROW]: false, [LEFT_ARROW]: false }; } function draw() { background(50); fill(255); noStroke(); textAlign(CENTER, CENTER); text("use arrow keys, or SPACE to stop", width/2, height*0.67); drawSprites(); } function keyPressed() { if (activeKeys[keyCode] === false) { activeKeys[keyCode] = true; } // UP KEYS COMBO if (activeKeys[UP_ARROW] && activeKeys[RIGHT_ARROW]) { spr.setSpeed(2.5, 315); } else if (activeKeys[UP_ARROW] && activeKeys[LEFT_ARROW]) { spr.setSpeed(2.5, 225); } // DOWN KEYS COMBO else if (activeKeys[DOWN_ARROW] && activeKeys[RIGHT_ARROW]) { spr.setSpeed(2.5, 45); } else if (activeKeys[DOWN_ARROW] && activeKeys[LEFT_ARROW]) { spr.setSpeed(2.5, 135); } // SINGLE KEYS else if (activeKeys[UP_ARROW]) { spr.setSpeed(2.5, 270); } else if (keyCode == DOWN_ARROW) { spr.setSpeed(2.5, 90); } else if (keyCode == LEFT_ARROW) { spr.setSpeed(2.5, 180); } else if (keyCode == RIGHT_ARROW) { spr.setSpeed(2.5, 0); } return false; } function keyReleased() { if (activeKeys[keyCode]) { activeKeys[keyCode] = false; } }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.0/p5.js"></script> <script src="https://cdn.jsdelivr.net/gh/molleindustria/p5.play/lib/p5.play.js"></script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM