Ancestor

From Director Online Wiki
Jump to: navigation, search

Ancestors can be used to inherit functionality from other scripts, but Director also allows ancestors to be [Director Types]. With this you can inherit entire chunks of functionality, expand them and override them. For example, lets say that you want an image object that will allow extractAlpha() to work on an 8 bit image rather than giving an error. You can do this like this...

on new me, vWidth, vHeight, vDepth, vPalette
 if vPalette = void then
   me.ancestor = image(vWidth, vHeight, vDepth)
 else
   me.ancestor = image(vWidth, vHeight, vDepth, vPalette)
 end if
 return me
end


on extractAlpha me
 if me.ancestor.depth = 32 then
   return me.ancestor.extractAlpha()
 else
   iAlpha = image(me.ancestor.width, me.ancestor.height, 8, #grayscale)
   iALpha.fill(iALpha.rect, rgb(0,0,0))
   return iAlpha
 end if
end


So instead of creating a new image like this...

i = image(100,100,16)

use

i = script("superimage").new(100,100,16)
a = i.extractAlpha()

This can be used to expand any major functions as needed and make some very powerful script controllers.

See also Single Inheritance