Intan Mega
Intan Mega

Reputation: 45

AS3 sound play when hit mc

I have a script like this

 var idsya1:Idsya1 = new Idsya1(); 
    var iqlab1:Iqlab1 = new Iqlab1(); 
    var ikhsya1:Ikhsya1 = new Ikhsya1(); 


    if (idsya_1.hitTestObject(idsyabox_1))
     {
       idsya_1.enabled = false;
      //idsya_1.visible = false;
      idsya_1.buttonMode = false;
      idsya_1.x = 145.30 ;
      idsya_1.y = 168.05;
      idsya1.play();
      score+=10;
      skor.text = " " + score;
         }

     if (iqlab_1.hitTestObject(iqlabbox_1))
     {
       iqlab_1.enabled = false;
    //   iqlab_1.visible = false;
       iqlab_1.buttonMode = false;
       iqlab_1.x = 719.95;
       iqlab_1.y = 155.25;
       iqlab1.play();
      score+=10;
      skor.text = " " + score;
     }

    if (ikhsya_1.hitTestObject(ikhsyabox_1))
     {
       ikhsya_1.enabled = false;
       //idsya_1.visible = false;
       ikhsya_1.buttonMode = false;
       ikhsya_1.x = 459.95;
       ikhsya_1.y = 198.75;
      ikhsya1.play();
      score+=10;
      skor.text = " " + score;
     }

Idsya1, Iqlab1, and Ikhsya1 is a sound from a library..idsya_1, ikhsya_1 and iqlab_1 is a movieclip.

my problem is when idsya_1 hit idsyabox_1 then the idsya1 sound will playing, and it worked, but when ikhsya_1 hit ikhsyabox_1 the sound playing is ikhsya1 and idsya1 too, and when iqlab_1 hit iqlabbox_1 then all the sound will playing..why is this happen??

I mean when idsya_1 hit idsyabox_1 then the sound playing is idsya1 sound

when iqlab_1 hit iqlabbox_1 then the sound playing is iqlab1 sound

and when ikhsya_1 hit ikhsyabox_1 the sound playing is ikhsya1 sound

How can I do that?

Upvotes: 0

Views: 588

Answers (1)

Gio
Gio

Reputation: 1954

Probably because you're checking for collision in a loop and everytime that loop fires it checks if there's collision for each of your strange named MovieClips and plays sound accordingly. If you want to play sounds one time for each try having Boolean properties to test if it's been played or not. So you'd have something like this:

if (iqlab_1.hitTestObject(iqlabbox_1) && !iqlab_1SoundHasPlayed) // checking if the sound has already played
 {
   iqlab_1.enabled = false;
//   iqlab_1.visible = false;
   iqlab_1.buttonMode = false;
   iqlab_1.x = 719.95;
   iqlab_1.y = 155.25;
   iqlab1.play();
  score+=10;
  skor.text = " " + score;

  iqlab_1SoundHasPlayed = true;
 }

Upvotes: 0

Related Questions