• Advertisement
Sign in to follow this  

triggering audio with collision conumdrum

This topic is 3610 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
Sign in to follow this  

  • Advertisement