如何在p5.js中使用箭头键跳跃对象?

huangapple go评论74阅读模式
英文:

How to jump object using arrow key in p5js?

问题

这是我尝试的代码。当我按下“上箭头键”或“空格键”时,球会跳跃。问题是当球跳跃时,我无法让球稍微前进一点。有人可以帮我吗?

let jumper;

function setup() {
    createCanvas(400, 400);
    jumper = new Jumper();
}

function draw() {
    clear();
    jumper.run();
    
    push();
    fill(217);
    textFont('Avenir');
    textAlign(CENTER, CENTER);
    textSize(33);
    text('按空格键跳跃', width >> 1, height * 0.15);
    pop();
}

function keyPressed() {
    jumper.vel.y = -4;
    jumper.vel.x = +5;
}

希望对你有帮助!

英文:

This is my code that I tried. The ball is jumping when I pressing "up arrow key" or "space bar".The problem is that I am unable to move ball little bit forward when ball jumped.Can anybody help me please?

let jumper;

function setup() {
	createCanvas(400, 400);
	jumper = new Jumper();
}

function draw() {
	clear();
	jumper.run();
	
	push();
	fill(217);
	textFont('Avenir');
	textAlign(CENTER,CENTER);
	textSize(33);
	text('space bar to jump', width>>1, height*0.15);
	pop();
}

function keyPressed() {
	jumper.vel.y = -4;
    jumper.vel.x=+5;
}

答案1

得分: 0

你需要在倒数第二行修复bug。

英文:

You need to remove the bug in the second last line.

jumper.vel.x += 5;

答案2

得分: 0

我设置了一个非常基本的示例,我认为这可能是你所努力实现的内容:

let jumper;

function setup() {
    createCanvas(400, 400);
    jumper = new Jumper();
}

function draw() {
    clear();
    jumper.run();
 
    push();
    fill(217);
    textFont('Avenir');
    textAlign(CENTER, CENTER);
    textSize(33);
    text('按空格键跳跃', width >> 1, height * 0.15);
    pop();
}

function keyPressed() {
    jumper.x += 5;
}

class Jumper {
  
  constructor() {
    this.x = 10;
    this.y = height/2;
  }
  
  run() {
    ellipse(this.x, this.y, 10, 10);
  }

}

@Dishant 在你的代码中指出了一个错误,就是你写了 jumper.vel.x =+ 5,实际上这只是将 jumper 的位置设置为 x 位置为 5。你想要的是实际上让 jumper 向前移动 5 个像素 - 在我的示例中,这是通过 jumper.x += 5 来实现的,但你当然可以使用 jumper.vel.x += 5,因为你的问题中没有显示该代码!

英文:

I set up a very basic example of what I think you might be working towards:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

let jumper;

function setup() {
    createCanvas(400, 400);
    jumper = new Jumper();
}

function draw() {
    clear();
    jumper.run();
 
    push();
    fill(217);
    textFont(&#39;Avenir&#39;);
    textAlign(CENTER,CENTER);
    textSize(33);
    text(&#39;space bar to jump&#39;, width&gt;&gt;1, height*0.15);
    pop();
}

function keyPressed() {
    jumper.x+=5;
}

class Jumper {
  
  constructor() {
    this.x = 10;
    this.y = height/2;
  }
  
  run() {
    ellipse(this.x, this.y, 10, 10);
  }

}

<!-- language: lang-html -->

&lt;script src=&quot;https://cdn.jsdelivr.net/npm/p5@0.10.2/lib/p5.js&quot;&gt;&lt;/script&gt;

<!-- end snippet -->

@Dishant was correct in that your code had a typo in where you did jumper.vel.x =+ 5 which is basically just setting the position of the jumper to the x position of 5. When what you want is to actually make the jumper move 5 pixels forward - in my example this done using jumper.x += 5 but you'd of course use jumper.vel.x += 5 as that code wasn't shown in your question!

huangapple
  • 本文由 发表于 2020年1月6日 18:09:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/59610145.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定