我正在尝试构建乒乓球,在球击中球拍 x 次后,会出现圣诞节动画。我不确定如何注册它,以及是否应该使用 if else 切换到动画。Stackoverflow 不会让我发布我的问题,因为它说它“主要是代码”所以这是我试图占用更多的文本空间。到目前为止,这是我的代码:
function setup() {
createCanvas(windowWidth, windowHeight);
//frameRate(6);
paddleA = createSprite(30, height / 2, 10, 100);
paddleA.immovable = true;
paddleB = createSprite(width - 28, height / 2, 10, 100);
paddleB.immovable = true;
wallTop = createSprite(width / 2, -30 / 2, width, 30);
wallTop.immovable = true;
wallBottom = createSprite(width / 2, height + 30 / 2, width, 30);
wallBottom.immovable = true;
ball = createSprite(width / 2, height / 2, 10, 10);
ball.maxSpeed = MAX_SPEED;
paddleA.shapeColor = paddleB.shapeColor = ball.shapeColor = color(
255,
0,
255
);
ball.setSpeed(MAX_SPEED, -180);
}
function draw() {
background(0);
paddleA.position.y = constrain(
mouseY,
paddleA.height / 2,
height - paddleA.height / 2
);
paddleB.position.y = constrain(
mouseY,
paddleA.height / 2,
height - paddleA.height / 2
);
ball.bounce(wallTop);
ball.bounce(wallBottom);
var swing;
if (ball.bounce(paddleA)) {
swing = (ball.position.y - paddleA.position.y) / 3;
ball.setSpeed(MAX_SPEED, ball.getDirection() + swing);
}
if (ball.bounce(paddleB)) {
swing = (ball.position.y - paddleB.position.y) / 3;
ball.setSpeed(MAX_SPEED, ball.getDirection() - swing);
}
if (ball.position.x < 0) {
ball.position.x = width / 2;
ball.position.y = height / 2;
ball.setSpeed(MAX_SPEED, 0);
}
if (ball.position.x > width) {
ball.position.x = width / 2;
ball.position.y = height / 2;
ball.setSpeed(MAX_SPEED, 180);
}
drawSprites();
//switch to Xmas animation
//if (ball.bounce(paddleA)) {
//background("brown");