如何使元素在点击时再次执行某些操作

时间:2019-04-08 21:25:35

标签: javascript loops onclick move

单击时,我需要使球再次跳跃,但是我不知道如何将我的功能放在一起使其再次跳跃。谁能帮我这个忙吗?我重复一遍,单击时我需要使球跳近一点,并且它需要能够空中进行,不需要反弹。

var canvas, ctx, container;
canvas = document.createElement('canvas');
ctx = canvas.getContext("2d");
var ball;
var vy;
var gravity = 0.5;
var bounce = 0.7;
var xFriction = 0.1;


function init() {
  setupCanvas();
  vy = (Math.random() * -15) + -5;
  ball = {
  x: canvas.width / 2,
  y: 100,
  radius: 20,
  status: 0,
  color: "red"
  };
}

function draw() {
  ctx.clearRect(0, 0, canvas.width, canvas.height);
  ctx.beginPath();
  ctx.arc(ball.x, ball.y, ball.radius, 0, Math.PI * 2, false);
  ctx.fillStyle = ball.color;
  ctx.fill();
  ctx.closePath()
  ballMovement();
}
setInterval(draw, 1000 / 35);

function ballMovement() {
  ball.y += vy;
  vy += gravity;
  if (ball.x + ball.radius > canvas.width || ball.x - ball.radius < 0) {
  vx *= -1;
}

 if (ball.y + ball.radius > canvas.height) {
 ball.y = canvas.height - ball.radius;
 vy *= -bounce;

 vy = 0;

 if (Math.abs(vx) < 1.1)
 vx = 0;
 xF();
 }
}

function setupCanvas() { //setup canvas
  container = document.createElement('div');
  container.className = "container";
  canvas.width = window.innerWidth;
  canvas.height = window.innerHeight;
  document.body.appendChild(container);
  container.appendChild(canvas);
  ctx.strokeStyle = "#ffffff";
  ctx.lineWidth = 2;
}

我需要它能够在空中跳跃,并且能够使增益下降到停止位置。

0 个答案:

没有答案