简体   繁体   English

对对象的声音效果p5.js

[英]Sound effect on objects p5.js

I'm trying to get my stormTrooper image to produce a sound effect when clicked - I'm having no luck so far...I checked the p5.js website - but can't figure it out. 我试图让我的stormTrooper图像在单击时产生声音效果-到目前为止我还没有运气...我检查了p5.js网站-但无法弄清楚。

Wondering if I have to include the mousePressed function inside the storm object? 想知道我是否必须在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();
   }
}

You wouldn't move the sketch-level mousePressed() function to be inside the Storm object (objects should start with an upper-case letter btw). 您不会将草图级别的mousePressed()函数移动到Storm对象内部(对象应以大写字母btw开头)。 Instead, you would just call another function inside the Storm object from the sketch-level mousePressed() function. 相反,您只需从草图级别的mousePressed()函数中调用Storm对象内的另一个函数mousePressed()

Also note that your Storm class does not contain a contains() function, so your current code won't work. 还要注意,您的Storm类不包含contains()函数,因此您当前的代码将无法使用。

Here's a skeleton of what you'd need to do: 这是您需要做的事情的概要:

function Storm(x, y){
   //other variables and functions here

   this.contains = function(x, y){
      //return true if x,y is inside this Storm's hitbox
   }
}

function mousePressed(){
   if(trooper.contains(someX, someY)){
      //play your sound or do whatever you want
   }
}

You also need to start breaking your problem down into smaller steps . 您还需要开始将问题分解为更小的步骤 It looks like you're confused about a few different things, so you should ask about each of them in their own question, with their own MCVE that isolates just that step. 看起来您对一些不同的事情感到困惑,因此您应该在自己的问题中询问每一个问题,而他们自己的MCVE只会隔离这一步骤。

For example, can you create a small example sketch that just plays a sound? 例如,您可以创建一个只播放声音的小示例草图吗? Get that working perfectly, without worrying about objects or collision detection or anything else. 无需担心物体或碰撞检测或其他任何问题,即可使其完美工作。 Separately from that, can you create an example program that just handles collision detection, say by changing the color of a rectangle whenever the mouse is inside it? 除此之外,您是否可以创建一个仅处理碰撞检测的示例程序,比如说只要鼠标位于矩形内就通过更改其颜色? Separately from that, can you create an example program that sets up objects? 除此之外,您可以创建一个设置对象的示例程序吗? Get each of those working perfectly by themselves before you start thinking about combining them into one program. 在开始考虑将它们组合到一个程序中之前,让每个人都能完美地工作。 Then if you get stuck on a specific step, you can post a MCVE along with a specific question, and we'll go from there. 然后,如果您陷于特定步骤,则可以发布MCVE以及特定问题,我们将从那里继续。 Good luck. 祝好运。

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

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