Adding in the collision sounds

Everything is functioning, though a bit boring, due to the lack of sound. The Debris being destroyed so quickly is also not very satisfying. Let's fix both of these issues.

  1. First we need to bring in some sounds. Create a new Sound, snd_Shatter_Glass, and load Chapter 6/Sounds/Shatter_Glass.wav. The default values will work, just make sure that Kind is set to Normal Sound. This effect will be for when the Glass breaks apart.
  2. We also want a sound for when the Glass Pillars do not break. Create another new Sound, snd_Impact_Glass and load Chapter 6/Sounds/Impact_Glass.wav.
  3. Repeat this process for the Wood and Steel sound effects.
  4. We need to initialize some variables, so reopen scr_Pillar_Glass_Create and add the following at the end of the script:
    impact = snd_Impact_Glass;
    shatter = snd_Shatter_Glass;
    
    isTapped = false;
    isActive = false;
    alarm[0] = room_speed;

    We start by assigning variables to the Impact and Shatter sounds. We will only want to allow the impact sound to play a single time, so we have created the isTapped variable. The isActive variable and the alarm are going to be used, so that no sound is made when the game starts. When the physics system begins, all active instances in the world will have gravity applied, which will cause collisions. This in turn means that the impact sound will occur when nothing appears to be moving.

  5. Reopen scr_Pillar_Wood_Create and scr_Pillar_Steel_Create, and add the same code with the appropriate sounds.
  6. Now we can start implementing the sounds. Open scr_Pillar_BreakApart and insert the following line of code before the instance is destroyed:
    sound_play(shatter);

    When the Debris spawns, we will play the Shatter sound a single time. Notice that we have given this sound a priority of 10, which means that if too many sounds need to be played, this sound will be chosen over lower priority sounds.

  7. Still in the script, we need to play the impact sound if a collision occurs, but does not break the Pillar. We will add an else statement immediately after the instance is destroyed.
    } else {
        if (!isTapped)
        {
            sound_play(impact);
            isTapped = true;
        }
    }

    If only a minor collision has occurred, we check to see if we have run the sound before. If we haven't, then we play the impact sound, with a low priority, and stop this code from executing again.

  8. We only have one thing left to do in this script and that is to place all of the code into a conditional statement, so that it executes only if the instance is active. Add the check at the top of the script and place braces around all the existing code. When it is done, the whole script will look like the following:
    if (isActive)
    {
        if (abs(other.phy_linear_velocity_x) > myDamage || abs(other.phy_linear_velocity_y) > myDamage)
        {
            if (phy_mass < other.phy_mass)
            {     
                p1 =instance_create(x, y, debris01);
                p1.phy_speed_x = phy_speed_x;
                p1.phy_speed_y = phy_speed_y;
                p1.phy_rotation = phy_rotation;
    
                p2 =instance_create(x, y, debris02);
                p2.phy_speed_x = phy_speed_x;
                p2.phy_speed_y = phy_speed_y;
                p2.phy_rotation = phy_rotation;
    
                p3 =instance_create(x, y, debris03);
                p3.phy_speed_x = phy_speed_x;
                p3.phy_speed_y = phy_speed_y;
                p3.phy_rotation = phy_rotation;
    
                sound_play(shatter);
    
                instance_destroy();
            } else {
                if (!isTapped)
                {
                    sound_play(impact);
                    isTapped = true;
                }
            }
        } 
    }
  9. We need to repeat this process for scr_Pillar_Destroy, so that the shatter sound is played on destruction, the impact sound on a light collision, and all of this when the instance is active. Here is the code in its entirety:
    if (isActive)
    {
        if (abs(other.phy_linear_velocity_x) > myDamage || abs(other.phy_linear_velocity_y) > myDamage)
        { 
            if (phy_mass < other.phy_mass)
            { 
                sound_play(shatter);
                instance_destroy();
            }        else {
                if (!isTapped)
                {
                    sound_play(impact);
                    isTapped = true;
                }
            }
        }
    }
  10. In order for the sounds to work, we need to make them active. Create a new Script, scr_Pillar_Alarm0 and set isActive to true.
  11. Rather than adding an alarm for every pillar and debris, we can just add an Alarm 0 event to obj_Pillar_Parent. This will not cause any conflicts because the alarm will only be run once per instance and only changes a variable.
  12. Run the game, explode the TNT and listen. We can hear the different sounds as the Pillars break apart and collide with each other. Also notice that there is more Debris now remaining. This is because there is now a one second delay before they can destroy themselves, which allows time for the Debris to escape any collision that happens upon creation.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.136.84