Setter with default getter + Actuate = problem

Let’s say you need to animate some custom property of object. And you choose to use Actuate as tweening library:

Actuate.tween(someObject, 1.0, { myOffsetX: 100 });

Moreover, you need to do some work when property updated, so you decide to create a setter:

public var myOffsetX(default, set):Int;

private function set_myOffsetX(value:Int):Int {
    if (value != myOffsetX) {
        myOffsetX = value;
        doSomeWork();
    }

    return value;
}

It works fine while you building it to flash, but stop working on html5 or native.

Solution is very simple – define getter:

private var _myOffsetX:Int;
public var myOffsetX(get, set):Int;

private function get_myOffsetX():Int {
    return _myOffsetX;
}

private function set_myOffsetX(value:Int):Int {
    if (value != _myOffsetX) {
        _myOffsetX = value;
        doSomeWork();
    }

    return value;
}

You must do it because of these lines of code inside GenericActuator:

if (#if flash false && #end Reflect.hasField (target, i)) {
    Reflect.setField (target, i, Reflect.field (properties, i));
} else {
    Reflect.setProperty (target, i, Reflect.field (properties, i));
}

Because when you have default getter Haxe generates real field (and it work fine on flash just because of #if flash false && #end).

I’m not try to say that it is a big problem, but it is confusing.

Leave a Reply

Your email address will not be published. Required fields are marked *

*