Re: Need help with Script: How can I play a sound?
Posted: Fri Jun 21, 2013 2:10 am
I cleaned a few bits up. You assigned the SoundSource as a private variable in condition 0 which means it wasn't available to the rest of the script.
Try this as your world script.
Try this as your world script.
Code: Select all
this.name = "test";
this.author = "DarkSoul";
this.copyright = "Do what you want with it";
this.description = "test";
this.version = "1.0";
/* Setup the sound source for use. */
this.startUp = function () {
/* Use public variable that can be accessed by the rest of the script. */
this.$testSound = new SoundSource;
/* Play once, no repeat. */
this.$testSound.loop = false;
/* Default custom sound assignment. */
this.$testSound.sound = "[test_01]";
}
this.alertConditionChanged = function (newCondition, oldCondition) {
switch (newCondition) {
case 0: // we're docked
{
this.$testSound.sound = "[test_docked]";
this.$testSound.play();
}
break;
case 1: // we're at green alert
case 2: // or we're at yellow alert
{
if (player.ship.energy > 108) // if we're not using the damaged HUD
{
this.$testSound.sound = "[test_alert1or2]";
this.$testSound.play();
}
}
break;
case 3: // we're at red alert
{
if (player.alertHostiles && player.ship.energy > 108) // and under attack and not using the damaged HUD
{
this.$testSound.sound = "[test_alert3]";
this.$testSound.play();
}
}
break;
}
}
this.shipLaunchedFromStation = function () {
this.$energyCheckTimer = new Timer(this, this.$energyCheck, 0, 2) // use a timer to keep an eye on the HUD state
}
this.shipDied = this.$stopEnergyCheckTimer = function () {
if (this.$energyCheckTimer) {
if (this.$energyCheckTimer.isRunning) {
this.$energyCheckTimer.stop();
}
delete this.$energyCheckTimer;
}
}
this.$energyCheck = function () {
if (player.ship.docked) {
this.$stopEnergyCheckTimer();
} else {
if (player.ship.energy < 109) {
if (!this.$testSound.isPlaying) {
/* Start the sound playing if it has stopped. */
this.$testSound.play();
}
} else if (player.ship.hud == "kwsnafuhud.plist") {
this.alertConditionChanged(player.alertCondition, 0); // if energy is >49 and we're still displaying the damaged HUD, use other code to repair
}
}
}