Jump to content

Recommended Posts

I am trying to make and execute the following function:

 

function makesoundobj(mSO, id, url) {
  var mSO = soundManager.createSound(
      {
         id:= id,
         url: = url,
         onfinish: function()
         {
            playerButton.src = "http://www.birminghammusiclive.com/images/bigplay.png";
         }
      }); //uses object literal notation just to be safe
  }

makesoundobj('mySoundObj','mysound','http://www.birminghammusiclive.com/Rick.mp3');

 

parameter 1 (mso) is the name of the sound object i want to create

paramater 2(id) and 3(url) are 2 parameters of the sound object.

 

The code works fine when it's not written as a function: 

<script type="text/javascript">
   soundManager.onload = function()
   {
      soundManager.url = "http://www.birminghammusiclive.com/soundmanager2.swf"; // override default SWF url
      soundManager.debugMode = false;
      soundManager.consoleOnly = false;

      var playerButton = document.getElementById("player"); //need it before the onfinish event

      var mySoundObj = soundManager.createSound(
      {
         id: "mySound",
         url: "http://www.birminghammusiclive.com/Rick.mp3",
         onfinish: function()
         {
            playerButton.src = "http://www.birminghammusiclive.com/images/bigplay.png";
         }
      }); //uses object literal notation just to be safe

      playerButton.onclick = function()
      {
         if(this.src == "http://www.birminghammusiclive.com/images/bigplay.png")
         {
            this.src = "http://www.birminghammusiclive.com/images/bigpause.png";
            mySoundObj.play();
         }
         else
         {
            this.src = "http://www.birminghammusiclive.com/images/bigplay.png";
            mySoundObj.pause();
         } //end else
      } //end onclick
   } //end onload
</script>

 

Link to comment
https://forums.phpfreaks.com/topic/107933-intermediary-function-fails/
Share on other sites

Sorry!  Let me clean that up a bit:

 

If the original code to make a specific sound object is this (soundmanager2 script): 

var mySoundObj = soundManager.createSound(
      {
         id: "mySound",
         url: "http://www.birminghammusiclive.com/Rick.mp3",
       });

 

I want to change this into a function whose arguments are the essetial parameters, so I can create any sound object I want just by calling that function.  But I think the use of my function arguments may be wrong here?

 

function makesoundobj(mSO, id, url) {
  var mSO = soundManager.createSound(
      {
         id:= id,
         url: = url,
        
      }); 
  }

 

 

 

Sorry!  Let me clean that up a bit:

 

If the original code to make a specific sound object is this (soundmanager2 script): 

var mySoundObj = soundManager.createSound(
      {
         id: "mySound",
         url: "http://www.birminghammusiclive.com/Rick.mp3",
       });

 

I want to change this into a function whose arguments are the essetial parameters, so I can create any sound object I want just by calling that function.  But I think the use of my function arguments may be wrong here?

 

function makesoundobj(mSO, id, url) {
  var mSO = soundManager.createSound(
      {
         id:= id,
         url: = url,
        
      }); 
  }

 

You shouldn't be passing MSO into the function, as that's what you're trying to create.  So, it makes more sense to do something like:

function createSoundObject(myId, myUrl)
{
   var mSO = soundManager.createSound(
   {
      id: myId,
      url: myUrl
   });

   return mSO; //gotta remember to return it so it can be assigned to a variable outside of this function
}

 

A better way to do it, though, would be to pass it an object filled with options:

function createSoundObject(options)
{
   var mSO = soundManager.createSound(options);
   return mSO;
}

var myOptions =
{
   id: "some id",
   url: "some url",
   onfinish: function()
   {
      alert("Hi, I'm the alert telling you that the onfinish event fired!");
   }
};

var testSoundObj = createSoundObject(myOptions);

 

You keep the flexiblity of the default soundManager.createSound() function, since you can set what options/parameters you like, but save on some typing.  Unfortunately, it's difficult creating a function to generate the options object as the soundManager has a ton of properties that can be assigned to.  I don't think there's a very clean way to get around some of the grunt work.

How is testSoundObj assigned when the function executes? 

 

 var testSoundObj = createSoundObject(myOptions); 

 

Would it be a reference to mSO?  Or is it a copy based on the return value?

 

 

I believe it's a reference.  It shouldn't really matter, though, as the temporary mSO object will be a unique object each time the function is called.  Not only that, but mSO lives only for the lifespan of the function anyway.  The 'var' keyword used before it forces scope. 

 

The only collision that I think may occur is if you try creating two objects with the same exact options.  That's because their IDs would be the same, so the second object would most likely overwrite the first.

How is testSoundObj assigned when the function executes? 

 

 var testSoundObj = createSoundObject(myOptions); 

 

Would it be a reference to mSO?  Or is it a copy based on the return value?

 

 

I believe it's a reference.  It shouldn't really matter, though, as the temporary mSO object will be a unique object each time the function is called.  Not only that, but mSO lives only for the lifespan of the function anyway.  The 'var' keyword used before it forces scope. 

 

The only collision that I think may occur is if you try creating two objects with the same exact options.  That's because their IDs would be the same, so the second object would most likely overwrite the first.

 

Actually, now that I think about it, the following should happen.

 

Say you do something like:

var myOptions =
{
   id: "Song1",
   url: "music/rock_on.mp3"
};

var musicObj = createSoundObject(myOptions);
var musicObj2 = createSoundObject(myOptions);

 

I believe that musicObj and musicObj2 should point to the same object.  So, changing something with one variable may change it for both.

 

Of course, this depends on how soundManager behaves under the hood.  It may check to see if any duplicate sounds are active.  But, generally, they should point to the same object.

 

There's really only one way to know for sure -- create a test page that attempts to create two different objects using the same options for both.

yeah, hopefully i'll get some time to try those things out after work.  8)

 

so, if you left the keywork 'var' out, I guess it would create a global object? Or does leaving 'var' out imply that one already exists (required)?

 

 

In php i guess the scope is automatically set at function level.  Unless, using the 'global' keyword, but i've never tried using the 'global' keyword on an object.

 

yeah, hopefully i'll get some time to try those things out after work.   8)

 

so, if you left the keywork 'var' out, I guess it would create a global object? Or does leaving 'var' out imply that one already exists (required)?

 

 

In php i guess the scope is automatically set at function level.  Unless, using the 'global' keyword, but i've never tried using the 'global' keyword on an object.

 

 

Yeah, scope is a bit weird in JavaScript.  I believe it has function-level scope.  So, with some block statements:

var example = 5;

if(myElem.value == "add")
{
   alert("Adding");
   example += 2;
}

document.write(example);

 

It will write 5 instead of 7.

 

It makes sense to leave 'var' out if it was used in a higher level of scope.  So:

var global = "Hi, I'm a global variable!";

function example()
{
   global += "  And this is me in a function!";
}

example();

document.write(global);

 

Will output: Hi, I'm a global variable! And this is me in a function!

 

But, if you do this:

var global = "Hi, I'm a global variable!";

function example()
{
   var global = ""; 
   global += "  And this is me in a function!";
}

example();

document.write(global);

 

It will output: Hi, I'm a global variable!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.