Sign in to follow this  
Thomas Bartholemew

triggering audio with collision conumdrum

Recommended Posts

Hi, newcomer in need a bit of direction here! I have some code and I'm successfully triggering a wave file when a turtle collides with a starfish in a game. I am now trying to trigger the same audio file when the turtle collides with a Jack fish. Here's my code that works:
[source lang=cpp]

//Makes the starfish
		if (stars.size() < MAX_STARFISH ){
	
			Starfish *s = new Starfish();
			stars.push_back(*s);
	
		}
		int star_off_screen = 0;
		int star_rand_limit = rand() % 500;
		for (int i = 0; i < stars.size(); i++){
	
			stars[i].move();
			stars[i].show();
	
			//cout << "Starfish " << i << " vs Turtle: " << endl;
			if (check_collision( stars[i].return_box(), myTurtle.return_box() )){
	
				if( Mix_PlayChannel(2, gulp, 0 ) == -1 ){
					return 1;
				}
	
				//cout << "Score!" << endl;
				myTurtle.adjust_score(10);
				stars.erase(stars.begin()+i);
				
			}
			if (stars[i].pos() < -200 - star_rand_limit){
				star_off_screen++;
			}
		}
		if (star_off_screen >= MAX_STARFISH){
			stars.clear();
		}
	//Makes the starfish
		if (stars.size() < MAX_STARFISH ){
	
			Starfish *s = new Starfish();
			stars.push_back(*s);
	
		}
		int star_off_screen = 0;
		int star_rand_limit = rand() % 500;
		for (int i = 0; i < stars.size(); i++){
	
			stars[i].move();
			stars[i].show();
	
          //cout << "Starfish " << i << " vs Turtle: " << endl;
          if (check_collision( stars[i].return_box(),myTurtle.return_box() )){
	
				if( Mix_PlayChannel(2, gulp, 0 ) == -1 ){
					return 1;
				}
	
				//cout << "Score!" << endl;
				myTurtle.adjust_score(10);
				stars.erase(stars.begin()+i);
				
			}
			if (stars[i].pos() < -200 - star_rand_limit){
				star_off_screen++;
			}
		}
		if (star_off_screen >= MAX_STARFISH){
			stars.clear();
		}
	

Here's my code that doesn't work:

//Makes a jack fish
		if (jacks.size() < 1){
			Jack *j = new Jack();
			jacks.push_back(*j);
			jack_finished = 0;
		}
		int jack_off_screen = 0;
		int jack_rand_limit = rand() % 500;
		for (int i = 0; i < jacks.size(); i++){
	
			jacks[i].move();
			jacks[i].show();
	
	 //cout << "Jack " << i << " vs Turtle: " << endl;
	 if (check_collision( jacks[i].return_box(), myTurtle.return_box() )){
				collision = 1;
				
				if (Mix_PlayChannel(2, gulp, 0 ) == -1 ){
					return 1;
				}	
			}
			
			if (jacks[i].pos() < -200 - jack_rand_limit){
				//cout << i << " " << shoal[i].pos() << endl;
				jack_off_screen++;
			}		
		}
		if (jack_off_screen == 1){
			jacks.clear();
			jack_finished = 1;
		}
		if (collision){	
			beta += 4;				
		}
		collision = 0;	

		// Reduce beta after collision
		beta -= 3;

		if (beta < 0){
			beta = 0;
		}		
		if (beta > 250){
			beta = 250;
		}


Can anyone help me out? Help is much appreciated! thanks.


[Edited by - Thomas Bartholemew on March 2, 2008 12:15:58 PM]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this