Scripting An Animated Sprite
コード上からパラメーターを制御することで、tk2dAnimatedSpriteの動作に簡単にアクセスでき、各種のクリップの再生が可能です。
この例では、キー入力にアニメーションスプライトを反応させています。projectパネルにC#スクリプトを作って「TutorialAnimController」という名前にし、以下のコードをコピペしてください。
using UnityEngine;
using System.Collections;
public class TutorialAnimController : MonoBehaviour {
tk2dAnimatedSprite anim;
// Use this for initialization
void Start () {
anim = GetComponent<tk2dAnimatedSprite>();
}
bool walking = false;
void HitCompleteDelegate(tk2dAnimatedSprite sprite, int clipId)
{
if (walking)
{
anim.Play("walk");
}
else
{
anim.Play("idle");
}
}
// Update is called once per frame
void Update ()
{
if (Input.GetKeyDown(KeyCode.A))
{
anim.Play("hit");
anim.animationCompleteDelegate = HitCompleteDelegate;
}
if (Input.GetKeyDown(KeyCode.D))
{
anim.Play("walk");
anim.animationCompleteDelegate = null;
walking = true;
}
if (Input.GetKeyDown(KeyCode.W))
{
anim.Play("idle");
anim.animationCompleteDelegate = null;
walking = false;
}
}
}
animated spriteに、このスクリプトをアタッチし、ゲームを実行してみてください。
「A,D,W」キーを押して、動作を確認してください。
ヒットクリップのアニメーション完了のdelegateは、前のアニメーションの状態によって切り替わります。
最終更新:2011年11月12日 15:41