2017-03-20 3 views
0

클릭하면 사운드 효과를 내기 위해 stormTrooper 이미지를 얻으려고합니다 - 지금까지 행운이 없습니다 ... p5.js 웹 사이트를 확인했지만 할 수 없습니다. 그것을 알아 내라.오브젝트에 대한 사운드 효과 p5.js

폭풍 개체 안에 mousePressed 함수를 포함해야하는지 궁금하십니까?

당신은 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(); 
    } 
} 

답변

1

(객체 BTW 대문자 문자로 시작해야합니다). 대신 스케치 수준 mousePressed() 함수의 Storm 개체 안에 다른 함수를 호출하면됩니다.

Storm 클래스에는 함수가 없으므로 현재 코드가 작동하지 않습니다.

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 
    } 
} 

breaking your problem down into smaller steps을 시작 또한 필요

여기에 당신이해야 할 것입니다 무엇의 골격입니다. 몇 가지 다른 것들에 대해 혼란스러워하는 것처럼 보입니다. 따라서 자신의 질문에서 각 단계에 대해 질문해야합니다. MCVE은 해당 단계를 격리합니다.

예를 들어 사운드를 재생하는 작은 예제 스케치를 만들 수 있습니까? 객체 또는 충돌 감지 또는 다른 것에 대한 걱정없이 완벽하게 작동하도록하십시오. 이와 별도로 충돌 감지를 처리하는 예제 프로그램을 만들 수 있습니까? 예를 들어 사각형이 마우스 안에있을 때마다 사각형의 색을 변경하면됩니까? 이와 별도로 객체를 설정하는 예제 프로그램을 만들 수 있습니까? 하나의 프로그램으로 결합하려고 생각하기 전에 스스로 완벽하게 작동하는 각자를 얻으십시오. 특정 단계를 고수하면 특정 질문과 함께 MCVE를 게시 할 수 있습니다. 행운을 빕니다.

+0

귀하의 조언을 많이 주셔서 감사합니다, 대단히 감사합니다! –