※この翻訳ドキュメントはスクリプトによって出力・同期されています。内容が怪しそうな場合はGitHubにissueを追加したり英語の原文の確認をお願いします。
AnimationBase クラス target 属性のインターフェイス¶
このページではAnimationBaseクラスのtarget属性のインターフェイスについて説明します。
属性の概要¶
target属性はアニメーション対象のインスタンス(例: SpriteやRectangleなどのインスタンス)を返却します。
基本的な使い方¶
AnimationBaseクラスの各サブクラス(例: AnimationMoveやAnimationXクラスなど)はgetterのtarget属性を持っています。
import apysc as ap
ap.Stage(
    stage_width=200,
    stage_height=150,
    background_color=ap.Color("#333"),
    stage_elem_id="stage",
)
sprite: ap.Sprite = ap.Sprite()
sprite.graphics.begin_fill(color=ap.Color("#00aaff"))
rectangle: ap.Rectangle = sprite.graphics.draw_rect(x=50, y=50, width=50, height=50)
animation_x: ap.AnimationX = rectangle.animation_x(x=100)
assert isinstance(animation_x.target, ap.Rectangle)
ジェネリックの型アノテーションについて¶
AnimationBaseクラスとその各サブクラスにはジェネリックの型アノテーションを行うことができます。型アノテーションをした場合target属性の型はその型のインスタンスとなります。
以下のコードでは[ap.Rectangle]というジェネリックの型アノテーションを行っています。
import apysc as ap
ap.Stage(
    stage_width=200,
    stage_height=150,
    background_color=ap.Color("#333"),
    stage_elem_id="stage",
)
sprite: ap.Sprite = ap.Sprite()
sprite.graphics.begin_fill(color=ap.Color("#00aaff"))
rectangle: ap.Rectangle = sprite.graphics.draw_rect(x=50, y=50, width=50, height=50)
animation_x: ap.AnimationX[ap.Rectangle] = rectangle.animation_x(x=100)
assert isinstance(animation_x.target, ap.Rectangle)
イベントハンドラのAnimationEventのインスタンスへジェネリックの型アノテーションを行うことも有益なケースがあります。この型アノテーションもtarget属性(e.this.target)の型に影響します。
import apysc as ap
def on_animation_complete(e: ap.AnimationEvent[ap.Rectangle], options: dict) -> None:
    """
    The handler the animation calls when its end.
    Parameters
    ----------
    e : ap.AnimationEvent
        Event instance.
    options : dict
        Optional arguments dictionary.
    """
    rectangle: ap.Rectangle = e.this.target
    rectangle.animation_x(x=50).start()
ap.Stage(
    stage_width=200,
    stage_height=150,
    background_color=ap.Color("#333"),
    stage_elem_id="stage",
)
sprite: ap.Sprite = ap.Sprite()
sprite.graphics.begin_fill(color=ap.Color("#00aaff"))
rectangle: ap.Rectangle = sprite.graphics.draw_rect(x=50, y=50, width=50, height=50)
animation_x: ap.AnimationX = rectangle.animation_x(x=100)
animation_x.animation_complete(on_animation_complete)
target 属性のAPI¶
特記事項: このAPIドキュメントはドキュメントビルド用のスクリプトによって自動で生成・同期されています。そのためもしかしたらこの節の内容は前節までの内容と重複している場合があります。
[インターフェイス概要]
アニメーション対象のインスタンスを取得します。
[返却値]
- target: VariableNameMixIn- アニメーション対象のインスタンス。 
 
[コードサンプル]
>>> import apysc as ap
>>> def on_animation_complete(
...     e: ap.AnimationEvent[ap.Rectangle], options: dict
... ) -> None:
...     rectangle: ap.Rectangle = e.this.target
>>> stage: ap.Stage = ap.Stage()
>>> sprite: ap.Sprite = ap.Sprite()
>>> sprite.graphics.begin_fill(color=ap.Color("#0af"))
>>> rectangle: ap.Rectangle = sprite.graphics.draw_rect(
...     x=50, y=50, width=50, height=50
... )
>>> _ = (
...     rectangle.animation_x(
...         x=100,
...     )
...     .animation_complete(on_animation_complete)
...     .start()
... )
