5 Replies Latest reply on Aug 8, 2007 7:44 PM by Kielz_the_Great

    Adding JavaScript behaviours using scriptInstanceList

    Kielz_the_Great
      Hi all,

      I have been having issues adding behaviour scipts to many sprites programmatically using the sprite's scriptInstanceList property. My scripts are written using the JavaScript syntax.

      My code I would use is in this example line:
      sprite(10).scriptInstanceList.add(new script("Behaviour Script"));

      In practice, this causes a problem. I add this script to many sprites on the screen and I find that the last sprite to receive this behaviour on it's scriptInstanceList receives all the events from the other sprites including itself (for example, a mouseEnter event).

      Spending hours trying to figure out why my sprites where not acting independently, I realised that I needed something in my script that is only possible with the Lingo syntax:
      on new(me)
      return me
      end

      Is there some way I can write an equivalent "on new" handler/constructor using the JavaScript syntax?

      Thanks,

      Kiel
        • 1. Re: Adding JavaScript behaviours using scriptInstanceList
          Ex Malterra Level 1
          that's an interesting question. my first guess, and it's only a guess, would be

          function new(this)
          {
          return this
          }

          because "me" and "this" are syntacticly approximate. but i've found that sometimes you still have to use "me" so maybe

          function new(me)
          {
          return me
          }

          but again this is all guess work. i've never had occasion to add behaviors dynamically so i'm out of my element. at any rate i hope you figure it out. please let us know what you discover.
          • 2. Re: Adding JavaScript behaviours using scriptInstanceList
            Lukewig Level 1
            Hi,

            Javascript objects are created using a constructor that is the class name, not the script cast member name (!:?), So, if you had a script like this

            function SomeClass(x)
            {
            // this is the constructor function
            this.SomeField = x;
            }


            SomeClass.prototype.mouseDown = function()
            {
            trace ('You clicked ' + this.toString());
            }


            You would add a 'SomeClass' object this to a sprite using javascript like this:

            obj = new SomeClass(1);
            sprite(1).scriptInstanceList.append(obj);


            If you want to add an instance using Lingo, then I think you need to create a wrapper function to return the object (there might be a more direct method, I just can't think of it right now):

            // Javascript
            function GetSomeClass(x)
            {
            return new SomeClass(x);
            }

            -- Lingo Script
            obj = GetSomeClass()
            sprite(1).scriptInstanceList.append(obj)

            -- Luke
            • 3. Re: Adding JavaScript behaviours using scriptInstanceList
              Kielz_the_Great Level 1
              Thanks for the replies, everyone!

              @Ex Malterra: using function new() { /* implementation */ } will not work because new is a reserved word in the JavaScript syntax and function names cannot be equal to reserved words.

              @Lukewig: Thanks for the perspective - for some reason I did not think to use a movie script instead of a behaviour script. I was using a behaviour script but of course they cannot inherit properties and functions of a particular object. Also, the scope of their functions is not great enough to allow me to create an instance of an object and, not to mention, you cannot write constructors for behaviour scripts (that is, write a new handler equivalent in JavaScript).
              • 4. Re: Adding JavaScript behaviours using scriptInstanceList
                Ex Malterra Level 1
                quote:

                Originally posted by: Kielz the Great
                @Ex Malterra: using function new() { /* implementation */ } will not work because new is a reserved word in the JavaScript syntax and function names cannot be equal to reserved words.




                oh yeah, duh. see what lack of sleep does to ya? sorry.