我试图让我的stormTrooper图像在点击时产生声音效果 - 目前我没有运气...我检查了p5.js网站 - 但不能想办法。对物体的声音效果p5.js
想知道是否必须在storm对象中包含mousePressed函数吗?
var img;
var trooper;
var sound;
function preload() {
img = loadImage("stormy3.png");
sound = loadSound("sounds/Followme.mp3");
}
function setup() {
// background(255, 0, 0, 0.4);
background(255, 0, 0, 0.4);
var myCanvas = createCanvas(windowWidth,windowHeight);
myCanvas.parent('myContainer');
myCanvas.position(0, 0);
trooper = new storm(300,400);
}
function draw() {
clear();
trooper.show();
trooper.movement();
trooper.bounce();
}
function storm(x,y) {
this.x = x;
this.y = y;
this.xSpeed = 3;
this.ySpeed = 3;
this.img = img;
this.show = function() {
image(img,this.x,this.y);
};
this.movement = function() {
this.x = this.x + this.xSpeed;
this.y = this.y + this.ySpeed;
};
this.bounce = function() {
if(this.x > width || this.x < 0) {
this.xSpeed = this.xSpeed * -1;
}
if(this.y > height || this.y < 0) {
this.ySpeed = this.ySpeed * -1;
}
};
}
function mousePressed() {
if (trooper.contains(trooper.x, trooper.y)) {
sound.play();
}
}
非常感谢您的建议,非常感谢! –