Luke Wigley <[EMAIL PROTECTED]> wrote:
> Notice that the parent script even learns the 'spriteNum' property
> from the sprite its attached to without ever having to explicitly
> pass this as a property (unfortunately, the spriteNum property
> isn't learnt by the ancestor until after the ancestor has received
> the first 'sprite' event, such as an exitframe our mouseDown

Hi Luke,

Try changing the mouseDown handler in your ancestor script to this...

on mouseDown me
  put me
  put ("sprite" && me.spriteNum && "got mouseDown")
  me.mBehaviorHandler()
end

... and add this to your behavior script:

on mBehaviorHandler(me)
  put #mBehaviorHandler, me
end

Since the message arrives at the ancestor via the behavior instance, the
<me> parameter refers to the behavior instance and not the ancestor.
The real reason that the ancestor doesn't "learn" the behavior's
spriteNum on new() is that, at that point, <me> refers to the new
ancestor instance, which is the one that received the initial #new
message.

Note that the ancestor script can call handlers in its child instance
because the initial call was received by the child instance.

Try adding this handler to your sprite behavior:

on mouseDown(me)
  ancestor.mouseDown() -- call the ancestor itself
end

This time, the ancestor gets sent the message directly, rather than via
the behavior, and so <me> has a different value.  Since the ancestor
does not have a spriteNum property, even implicitly, you will get a
Script Error when you click on the sprite.

If you now declare a "spriteNum" property in your ancestor, the script
will run, but the spriteNum of the ancestor will be void:

-- "sprite  got mouseDown"

However, the ancestor does not know where to find mBehaviorHandler(),
so you get a different Script Error.

If you remove the mouseDown handler from the sprite behavior, everything
comes back to normal.

In other words:
* a child instance can access its ancestor's handlers and properties
  as well as its own.  While handlers in the ancestor script are
  executing, the <me> value is that of the child instance.
* the ancestor instance has no knowledge of its children if it is
  called directly.
* if a handler in an ancestor is triggered because no such handler
  appears in the child, then the ancestor handler can access properties
  and handlers in its child, so long as they are identified by "me."
  (e.g: me.spriteNum)

Further considerations:
me.getaProp(#propertyName) and me[#propertyName] can also be used.
The effect is somewhat different: the first will access a property in
the child instance, while the second will access a property in the
ancestor.  If both child and ancestor have properties with the same
name, you can use this to distinguish between them.  If a property with
the given name does not exist, both these forms will return <Void>.


Cheers,

James



[To remove yourself from this list, or to change to digest mode, go to
http://www.penworks.com/LUJ/lingo-l.cgi  To post messages to the list,
email [EMAIL PROTECTED]  (Problems, email [EMAIL PROTECTED])
Lingo-L is for learning and helping with programming Lingo.  Thanks!]

Reply via email to