I'm attempting to read a property on a series of Sprites. This property may or may not be present on these objects, and may not even be declared, worse than being null.
My code is:
if (child["readable"] == true){ // this Sprite is activated for reading }
And so Flash shows me:
Error #1069: Property selectable not found on flash.display.Sprite and there is no default value.
Is there a way to test if a property exists before reading its value?
Something like:
if (child.isProperty("readable") && child["readable"] == true){ // this Sprite is activated for reading }
Method 3: Check if a property exists in an object using the “in” operator. JavaScript offers a built-in “in” operator that determines whether the specified property belongs to an object or not. It returns “true” if the particular property exists in the object and “false” for the case when the property is not found.
Objects in AS3 have the hasOwnProperty
method which takes a string argument and returns true
if the object has that property defined.
if(myObj.hasOwnProperty("someProperty")) { // Do something }
if ("readable" in child) { ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With