registerSpecialProperty()

言語バージョン

AS2 and AS3.

語法

Tweener.registerSpecialProperty(name:String, getFunction:Function, setFunction:Function [, parameters:Array]);

パラメーター

name:String — 新規に作成する特殊(オリジナル?)プロパティの名前。既存のtweening パラメーターと重複してはならない。また、既存の特殊プロパティと重複してしまうと、既存のものが上書きされてしまうので注意すること。

getFunction:Function — この特殊プロパティに対応するプロパティや値を読み出すためのファンクション。引数として対象となるオブジェクトを受け取らなければならない。

setFunction:Function — この特殊プロパティに対応する値やプロパティを設定するためのファンクション。以下の2つのパラメーターを受け取らなければならない:変更の対象となるオブジェクト、目的の(変更後の?)値(Numberオブジェクト)。

parameters:Array — 様々なタイプのプロパティの配列を含むパラメーター(オプション)。このパラメーターは、特殊プロパティの値を更新するときにgetFunctionsetFunctionに渡される。これによって、様々な特殊プロパティに対して同じように動作し、渡されたパラメーターを基準に異なった機能を提供するようなファンクションを作ることができる。

解説

Creates and registers a new special property, which is like a normal property, but with additional functionality. From an Object Oriented Programming point of view, this is like a getter/setter - meaning you create a function that reads a property of an object, and another function that sets the value of this same property, then tie them both to a special name.

A feature like this shouldn't be needed most of the times, as changing the value of a given property is all that's needed to create a transition of some kind. Some objects or classes don't have exposted properties, or some properties might need additional code for a propert tweening, and that's where the need for special properties arise.

Almost all special properties used on Tweener are created and registered with this method; check the SpecialPropertiesDefault.as file to check what they do. Some special properties, however, are grouping of other properties. This is the case of _color and _colorTransform, for example, which are related to a number of different properties, and as such don't really fit into the whole getter/setter architecture. Cases like these are created with the registerSpecialPropertySplitter method instead.

返り値

なし

用例

例として_autoAlphaという特殊プロパティを新たに作ります。このプロパティはMovieClip_alphaを設定し、もし値が0だった場合には_visibleの値を自動的にfalseにして、マウスイベントなども受け取らない、本当に不可視・無効な状態にします。このような働きをする特殊プロパティはデフォルトで別に用意されていますが、ここでは例として作ってみます。

今回のような場合、下記の要領で_autoAlphaという新しいプロパティを登録することができます:

_autoAlpha_get = function(p_obj:Object):Number {
    return p_obj._alpha;
};
_autoAlpha_set = function(p_obj:Object, p_value:Number):Void {
    p_obj._alpha = p_value;
    p_obj._visible = (p_value != 0);
};
Tweener.registerSpecialProperty("_autoAlpha", _autoAlpha_get, _autoAlpha_set);

まずgetファンクション(getterメソッド?)とsetファンクション(setterメソッド?)を作ります。その後、新しい特殊プロパティを登録します。この特殊プロパティはこの時点からTweenerで使えるようになります。

例えば、この特殊プロパティを使ってフェードアウトをさせたい場合、このようにします:

Tweener.addTween(myMC, {_autoAlpha:0, time:1});

フェードアウト後、myMCは自動的に不可視になります。

違うやり方と比較してみましょう。新しく作ったプロパティを使わず、トゥイーンの開始・終了のタイミングと可視属性の切り替えのタイミングをシンクロさせるイベントを使ったやり方は、このようになります:

// フェードイン
Tweener.addTween(myMC, {_alpha:100, time:1, onStart:function() { this._visible = true; }});

// フェードアウト
Tweener.addTween(myMC, {_alpha:0, time:1, onComplete:function() { this._visible = false; }});

また、追加のパラメーターを指定することで、さらにパワフルな機能を作り出せます。別々のプロパティに対して同じような処理を行わせるために、ファンクションを複製する必要はありません。例えば、getValueX(), getValueY(), getValueZ()、それとセットのsetValueX(), setValueY(), setValueZ()というファンクションをもったオブジェクトがあるとします。そして、これらの値を更新する特殊プロパティを作るとします。通常のやり方ではこのようになります:

_valueX_get = function(p_obj:Object):Number {
    return p_obj.getValueX();
};
_valueX_set = function(p_obj:Object, p_value:Number):Void {
    p_obj.setValueX(p_value);
};
_valueY_get = function(p_obj:Object):Number {
    return p_obj.getValueY();
};
_valueY_set = function(p_obj:Object, p_value:Number):Void {
    p_obj.setValueY(p_value);
};
_valueZ_get = function(p_obj:Object):Number {
    return p_obj.getValueZ();
};
_valueZ_set = function(p_obj:Object, p_value:Number):Void {
    p_obj.setValueZ(p_value);
};
Tweener.registerSpecialProperty("_valueX", _valueX_get, _valueX_set);
Tweener.registerSpecialProperty("_valueY", _valueY_get, _valueY_set);
Tweener.registerSpecialProperty("_valueZ", _valueZ_get, _valueZ_set);

これでも十分動作します。が、パラメーターを使うことで、同じようなものをたった2つのファンクションで実現できます:

_valueAny_get = function(p_obj:Object, p_parameters:Array):Number {
    return p_obj[p_parameters[0]]();
};
_valueAny_set = function(p_obj:Object, p_value:Number, p_parameters:Array):Void {
    p_obj[p_parameters[1]](p_value);
};
Tweener.registerSpecialProperty("_valueX", _valueAny_get, _valueAny_set, ["getValueX", "setValueX"]);
Tweener.registerSpecialProperty("_valueY", _valueAny_get, _valueAny_set, ["getValueY", "setValueZ"]);
Tweener.registerSpecialProperty("_valueZ", _valueAny_get, _valueAny_set, ["getValueZ", "setValueZ"]);

参照

registerSpecialPropertySplitter, Special Properties

メニューの表示について

このページの左側にメニューが表示されていない場合は、ここをクリックするとメニューを表示させることができます。

本ドキュメントについて