PropertyResolver (library)

Categories:Flash

Overview

Summary

A set of routines to safely get, or gracefully skip, potentially unreachable properties

Contents

Concept

There are various properties on objects in Flash which throw errors when accessed, namely properties relating to color and tint on Stage elements.

To demonstrate, create a new Symbol on the stage, then run the following code:

trace($selection[0].tintPercent);

Flash will throw a meaningless "The following JavaScript error(s) occurred: " error, with no further details, and the script will halt.

The reason for this is that tintPercent is only accessible if the element has a colorMode of "tint". This is where the PropertyResolver class comes into play. If you need to access potentially script-halting properties on an object, you can first test it with the PropertyResolver methods.

Usage

The following properties (and more will be added as they are found) currently throw these kinds of errors:

  • element.brightness
  • element.tintColor
  • element.tintPercent

If you find yourself needing to acess these properties, consider using the PropertyResolver class. For example, the xJSFL Table library uses the PropertyResolver class, otherise it errors when printing summaries of stage elements.

API

testObject(object)

Tests whether an element is one of the few elements that needs resolving

Parameters:

  • object Object An object or class instance

Returns:

  •   Boolean true or false, depending on the result

The following example preemptively tests an element to see if it may have properties that need resolving

if(PropertyResolver.testObject(object))
{
    var needsResolving = PropertyResolver.testProperty('tintPercent');
    trace(needsResolving);
};
true

testProperty(name)

Tests a property name to see if a property name needs resolving

Parameters:

  • name String A property name

Returns:

  •   Boolean true or false, depending on the result

The following example tests a property name to see if it needs resolving:

var needsResolving = PropertyResolver.testProperty('tintPercent');
trace(needsResolving);
true

resolve(element, name)

Resolves the property value of an element

Parameters:

  • element Object An object or class instance
  • name String A property name

Returns:

  •   Object The value of the property

The following example demonstrates how resolve() is used to grab properties without Flash throwing an error:

// grab the element
	var element = $selection[0];

// set its colorMode to "tint"
	element.colorMode = 'tint';

// grab properties
	trace('>' + PropertyResolver.resolve(element, 'tintPercent'));
	trace('>' + element.tintPercent)

// set its colorMode to "none"
	element.colorMode = 'none';

// grab properties
	trace('>' + PropertyResolver.resolve(element, 'tintPercent'));
	trace('>' + element.tintPercent)
>100
>100

>undefined
The following JavaScript error(s) occurred:

Comments are closed.