We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Okay Guys! So, basically I want to be able to incorporate a 'Ding' sound using the minim library that is played every time the Target is hit, which in this case is our Saucer.png. The problem is not with registering a click on the saucer but rather being able to incorporate the code necessary so when the Target is hit, it will play the sound and then continue to do so everytime it is hit
Here is my code:
import ddf.minim.*;
int rad = 60; // Width of the shape
float xpos, ypos; // Starting position of shape
float xspeed = 2.0; // Speed of the shape
float yspeed = 2.0; // Speed of the shape
int xdirection = 1; // Left or Right
int ydirection = 1; // Top to Bottom
int score=0; //Inital score
int lives=5; //Number of lives you start with
boolean lost=false; //Have you lost yet?
int speed=1;
PImage bg;
PImage Target;
int y;
AudioPlayer player;
Minim minim;
void setup()
{
size(736,460);
minim = new Minim(this);
fill(255,255,255);
noStroke();
frameRate(60);
Target = loadImage("Saucer (1).png");
xpos = width/2; // Set the starting position of the shape
ypos = height/2;
bg = loadImage("space 2.jpg"); //Load Image (jpg,png,gif)
player = minim.loadFile("Song 1.mp3"); //Load Audio File (mp3,wav)
player.loop();
}
void draw()
{
background(bg);
image(Target,xpos,ypos,60,60);
xpos = xpos + ( xspeed * xdirection ); // Update the position of the shape
ypos = ypos + ( yspeed * ydirection );
if (xpos > width-rad || xpos < rad) { // Test to see if the shape exceeds the boundaries of the screen
// If it does, reverse its direction by multiplying by -1
xdirection *= -1;
}
if (ypos > height-rad || ypos < rad) {
ydirection *= -1;
}
text("score = "+score,10,10); //Print the score on the screen
text("lives = "+lives,width-80,10); //Print remaining lives
if (lives<=0) //Check to see if you lost
{
textSize(50);
text("Click to Restart", 450,200);
noLoop(); //Stop looping at the end of the draw function
lost=true;
textSize(13);
}
}
void mousePressed() //Runs whenever the mouse is pressed
{
if (dist(mouseX, mouseY, xpos, ypos)<=rad) //Did we hit the target?
{
score=score+speed; //Increase the speed
speed=speed+1; //Increase the Score
}
else //We missed
{
if (speed<1) //If speed is greater than 1 decrease the speed
{
speed=speed-1;
}
lives=lives-1; //Take away one life
}
if (lost==true) //If we lost the game, reset now and start over
{
speed=1; //Reset all variables to initial conditions
lives=5;
score=0;
xpos=width/2;
xdirection=1;
lost=false;
loop(); //Begin looping draw function again
}
}
Thank you to everyone that can help me, feel free to give me code or modify the code above as well as provide links to reference pages!
Answers
Learn how to format the code.
And please don't post duplicates
https://forum.processing.org/two/discussion/18860/how-to-get-a-sound-to-play-when-i-click-an-object
https://forum.processing.org/two/discussion/18930/how-to-upload-image-for-the-ball-as-well-as-adding-a-sound-to-ball-everytime-it-s-clicked-urgent
Okay. What is exactly meant by formating the code?
Dw guys! i figured it out
you missed a bit
it needs, i think, a blank line before the first import
load a file the same way you do with the song. called it ding. don't call loop on ding.
use ding.play() to play the sample. after line 46 or so.
to get it to continue, use ding.rewind() to move the audio cursor back to the start of the file.
And then place the code under here?
that's line 46, so yes 8)
that's checking distance between the mouse and the position and incrementing the score, i guess that's when you want to ding.
i think calling rewind() and then play() is safest - it won't matter the first time through that you are rewinding before playing but is necessary for later dings.