Articles Archive
Articles Search
Director Wiki
 

Restricting Behaviors to the Proper Sprites

August 30, 2000
by Gary Rosenzweig

Director 8 introduced a new behavior handler that can come in handy if you are developing Lingo behaviors for non-programmers. In many workplaces, the Lingo programmer (also known as the "Lingo guru", "Lingo guy/gal", "Lingo God", or simply "software engineer") works to keep up a set of behaviors to be used by the rest of the staff. These non-programmers (also known as "designers", "producers", or "multimedia authors") animate and design in Director every day, but rely on the Lingo programmer for the behaviors.

I've been in this situation many times before. I'm sure that many of you out there are in this situation now. It actually works quite well. Anyway, Macromedia has built in some tools to help programmers create drag-and-drop behaviors for the non-programmers. One such tool is the on isOKToAttach handler. This will insure that the behaviors you write will only be used for the appropriate sprites.

It works quite simply. The behavior runs when the script is dragged from the cast or library to the Score or Stage. As it passes over a sprite, the on isOKToAttach handler runs and sends a TRUE or FALSE value back to Director. A TRUE means that the behavior can be attached to the sprite; a FALSE means that it cannot. The behavior can only be dropped on a sprite if the returned value is TRUE. Any other way that you can attach a behavior to a sprite, such as the behavior inspector or the Score window pop-up menu, will also use the on isOKToAttach handler to determine if it is allowed. If not, the behavior simply isn't attached to the sprite.

The handler gets two parameters passed into it to help your Lingo code determine if the sprite is OK. The first is the sprite type, which is either #script or #graphic. This first only happens if the behavior is going to be attached to the frame script channel. The second happens for all numbered sprites. The second parameter passed into the on isOKToAttach handler is the sprite number. This is necessary because you cannot simply use me.spriteNum to get the sprite number, as me doesn't exist until the movie is running and the behavior is used.

Here is a simple looping frame behavior that will only allow itself to be attached to a script channel.

on exitFrame me

  go to the frame

end


on isOKToAttach me, spriteType, spriteNum

  if spriteType = #script then
    return TRUE
  else
    return FALSE
  end if

end

Notice that I put the on isOKToAttach handler last, after the part of the behavior that actually does something. This is so that the script preview area of the score will show me something useful. It usually shows the first two lines of a behavior, which in this case gives you all that you need to know about what the behavior does. Since the sprite type passed into the on isOKToAttach handler only returns one of two values, it is not very useful for limiting your behavior's usage. Instead, you can use the sprite number along with the member type to restrict it more. Here is a handler that is restricted to bitmap members.

on exitFrame me

  sprite(me.spriteNum).rotation = (sprite(me.spriteNum).rotation + 1) mod 360

end


on isOKToAttach me, spriteType, spriteNum

  if sprite(spriteNum).member.type = #bitmap then
    return TRUE
  else
    return FALSE
  end if

end

You can also restrict the use of a behavior to more than one sprite type. Here is one that will work on both fields and Text members.

on beginSprite me

  sprite(me.spriteNum).member.text = ""

end


on isOKToAttach me, spriteType, spriteNum

  if sprite(spriteNum).member.type = #text or sprite(spriteNum).member.type = #field then
    return TRUE
  else
    return FALSE
  end if

end

You can take this a lot further. You can check the sprite type against a whole list of sprite member types, for instance. You can even make sure that the behavior is only attached to sprites that contain certain specific members. The big advantage of the isOKToAttach handler is that you can assume for the rest of the behavior that the right member type is present. This can cut down on your error checking throughout the behavior. After all, a text behavior attached to a bitmap sprite will usually produce a script error. And who do they blame when there is a script error in a shipped product? Not the designer...

Gary Rosenzweig's new book "Special Edition Using Director 8" is now available. It's the most comprehensive guide to Director ever, including tons of examples and demo movies. It's suitable for novices and experts alike. More information about the book can be found at http://clevermedia.com/resources/bookstore/book5.html. It can be purchased there, or in your local bookstore.

Gary Rosenzweig is the Chief Engineer, founder, and owner of CleverMedia, a game and multimedia development company in Denver, Colorado. He is the author of ten books on Macromedia Director and Flash, including his latest, Special Edition Using Director MX.

Copyright 1997-2024, Director Online. Article content copyright by respective authors.