12 Replies Latest reply on Apr 12, 2008 3:28 PM by Bob220867

    Add script to newly created sprite

    Bob220867 Level 1
      I've created a new sprite with imaging lingo - making a copy, setting it to a member and then applying to a new sprite. Now I'd like to apply a script from the cast to it. How can I do this?

      Many thanks
        • 1. Re: Add script to newly created sprite
          Minds Eye Level 1
          p_sprite = sprite(1) --(or whatever)
          p_sprite.scriptInstanceList.add(script("my script"))
          • 2. Re: Add script to newly created sprite
            Bob220867 Level 1
            Thanks for this I had something similar-ish based on Director's documentation - all the right words but in a different order.

            When I try though, it still doesn't respond. (For the moment the added script is just a simple mouseDown alert for testing.) Is there something I'm asking it to do that isn't allowed? The script 'dynPieceScript' is just sitting in the cast.

            Here's the code:

            on createPiece me
            -- iterate i that is method of naming sprites
            i=i+1
            -- set up ref point for new piece
            origLoc=my.loc
            -- make a copy of the play piece
            stageImage=(the stage).image
            spriteimage=stageImage.crop(my.rect)
            pMember=_movie.newMember(#Bitmap)
            -- name the member, set pMember to it's image,
            -- find an available channel, assign it to a sprite in place
            pMember.name="NewPiece"&&i
            pMember.image=spriteImage
            myChannelNum=_movie.lastChannel-i myNewSprite=channel(myChannelNum).makeScriptedSprite(member(pMember.name), origLoc)

            -- myNewSprite.name=my.member.name&&i

            myNewSprite.moveableSprite=TRUE
            myNewSprite.scriptInstanceList.add(script ("dynPieceScript"))
            end createPiece me

            Everything else works the way I expect it to - the moveable is fine etc. Any ideas?

            One other thing, there's a commented out attempt to name the newly created sprite but I get a No Access script error. Does that mean I can't name a sprite created like this?

            Many thanks
            • 3. Re: Add script to newly created sprite
              CenturyMan1979
              Just one quick thing I noticed in your make scripted sprite call, you can just use pMember for the member item in it like so,

              myNewSprite = channel(myChannelNum).makeScriptedSprite(pMember, origLoc)

              Also I am not sure what your reference to "my" is in your scripts but should probably be removed from what I can see. This might also explain why you get an error when trying to set the sprite name. So for that line try this instead,

              myNewSprite.name=pMember.name&&i

              Not sure that is the exact output you want since I am not sure what your reference to "my" is.

              Is your script dynPieceScript a behavior script or a parent script?
              • 4. Re: Add script to newly created sprite
                Level 7
                You can not name a scripted sprite. It is annoying, and I do not
                understand why this basic thing was not implemented, but it wasn't. You
                have access to every sprite property except name.
                • 5. Re: Add script to newly created sprite
                  Bob220867 Level 1
                  Thanks for those replies. Very helpful.

                  Thanks CenturyMan for the cleaning up observation, I'm always trying to make my scripting more efficient. This code is from a larger behaviour and one of the bits you don't see here is 'my=sprite(me.spriteNum)'. It's a shame about naming, that would have been really useful.

                  I think I've come to a dead end on this attempt. The reference to scriptInstanceList in the 8.5 Lingo Dictionary says 'It can be set only if the sprite already exists and has at least one instance of behaviour already attached to it'. So that goes against my scripted sprite, doesn't it? Unless that's just out of date. If anyone has any good ideas for this I'd grateful, otherwise it's back to making lots of unique cast members with scripts, which obviously for the sake of elegance and time I wanted to avoided.

                  Thanks again.
                  • 6. Re: Add script to newly created sprite
                    Level 7
                    I have had success with making a scripted sprite (using the
                    makeScriptedSprite() method), then adding a script to it using the
                    sprite's scriptInstanceList. In fact, my code specifically checks to
                    see if the scriptInstanceList.count=0 before adding the new one. It
                    will only apply the script if the sprite has no other scripts attached.
                    • 7. Re: Add script to newly created sprite
                      Bob220867 Level 1
                      Thanks for responding Mike.

                      I'll twiddle a bit more before abandoning this. Was the code you used so much different from what I have?
                      • 8. Re: Add script to newly created sprite
                        Level 7
                        Nope. It was quite similar. Apart from the fact that I was checking to
                        see if the sprite's scriptInstanceList was empty before adding it, and
                        the script I gave it was an instance of a parent script. But your code
                        looks like it should work (apart from the sprite naming thing of course)
                        • 9. Re: Add script to newly created sprite
                          CenturyMan1979 Level 2
                          Your code should work since I took what you gave us and modified it slightly to make it work. I took the createPiece function and just stuck it in a movie clip and then just put a button on the screen that would call that function. Then after setting your my referenced variables to static ones since I was not sure on the values it seemed to work fine. The sprite naming still does not seem to be working but I don't get an error when trying to name it. I will attach the code I made,
                          • 10. Re: Add script to newly created sprite
                            Bob220867 Level 1
                            Well that's pretty comprehensive, thank you.

                            I got yours working but couldn't integrate it into mine. Perhaps there's a difference in the scripts we're accessing? Yours was sitting in a movie script and mine in a cast member behaviour. Is there some sort of difference here about parents and behaviours?

                            Anyway, I'll keep on twiddling with this. You've been a big help, so I'll leave you alone now.


                            Bob
                            • 11. Re: Add script to newly created sprite
                              CenturyMan1979 Level 2
                              Is there a reason you have the script on a cast member instead of in a movie clip?
                              • 12. Re: Add script to newly created sprite
                                Bob220867 Level 1
                                Now that's an interesting question. I'm used to making scripts either by writing them by going thro' a sprite (right click, script, kind of thing) or in the script window (which obviously puts that into the cast and then I apply it to a sprite or whatever). I usually only have one movie script. But then I was reading Tab Julius' book Lingo only last night and it was talking about having multiple movie scripts. I thought I was being tidy but between his comments and your question I'm starting to see that I've got a new step to make and work out what this can do for me.

                                To answer your question I think we might be talking at slightly cross-purposes. I meant that the dynPieceScript was a cast member. The createPiece script (and its other components) was attached to a sprite.

                                I can work how to achieve pretty much anything these days. But I'm working on making the way I achieve them more efficient and elegant script-wise.