I tried to write a program that works on mobile (apple) devices. Everything works except for the sound.
This is my code:
// A sound file object
var dingdong;
function preload() {
// Load the sound file.
// We have included both an MP3 and an OGG version.
soundFormats('mp3', 'ogg');
dingdong = loadSound('bell.mp3');
}
function setup(){
createCanvas(windowWidth,windowHeight);
background(100);
}
function draw(){
background(100);
fill(0,256,0);
ellipse(mouseX,mouseY,80,100);
if(millis()>=5000){
fill(256);
if(touchEnded());
playSound(dingdong);
}
}
This is not the correct usage of touchEnded()
. Touch ended is not a function you call to get a true
/false
value. It is a function that you implement that gets called when a touch action ends (the user lifts their finger of the screen).
This is what you probably want:
function touchEnded() {
playSound(dingdong);
}
Also your if statement syntax is wrong:
// An if statement with no block (i.e. curly braces { }) will apply to the subsequent statement, however the ; brings an end to the if statement, so the next line is not part of it.
if (touchEnded());
// So in this case playSound would run regardless of what touchEnded() returned
playSound(dingdong);
In this case it is mostly academic because this code just crashes because touchEnded
is undefined. However, as a rule I think it is best to always use the block form of if statements and loops:
if (condition) {
playSound(dingdong);
}
Also it is a good idea to check the JavaScript console for errors as a first step when debugging issues.