2014年9月18日木曜日

libGDXで回転するジェット機2

今回のサンプルは、「LibGDX Tutorial 3B: Basic graphics2 / Simple Animation」、
ジェット機が回転するアニメーションをする。
見た目は前回とほとんど変わらないが、プログラムの中身はずいぶん違う。

1.雛形は前回のを利用


今回は雛形は作らずに前回のを利用する。


2.Animationクラスを利用する


Game class の修正。
BasicsGraphics-core の MyGdxGame.java を修正。



実行してみる。(※ステップ1)


前回と同様、ジェット機がくるくる回転している。
画像が小さくなったような気がするが、見た目はほぼ同じ。

3. 今回の重要なポイント


前回はタイマーを用いて自分で画像を更新することにより
アニメーションを実現していた。
が、今回はAnimationというlibGDXのクラスを使うことで
タイマーの処理を実装する必要がない。

animation = new Animation(1/15f, textureAtlas.getRegions());

When creating an animation you pass in the amount of time per frame
アニメーションを作成する際には、フレームあたりの時間を渡す

(15 frames per second in this case)
(この場合、毎秒15フレーム)

and an array of TextureRegions.
それとTextureRegionsの配列。

These represent the individual frames of animation within the TextureAtlas.
これらはTextureAtlas内アニメーションの各フレームを表す。

In this particular example we are simply using all of the frames available within the Atlas as a single animation.
この特定の例では、単に一つのアニメーションとしてアトラス内で使用可能なすべてのフレームを使用しています。

The next key change is:
次のキー変更がある

elapsedTime += Gdx.graphics.getDeltaTime();
batch.draw(animation.getKeyFrame(elapsedTime, true), 0, 0);

Here we are simply drawing the current frame from the animation to the screen.
ここでは、単に画面に​​アニメーションから、現在のフレームを集めている。

We pass in the amount of time that has elapsed so the animation knows where it is in the sequence.
私たちは、それが配列である場合、アニメーションが知っているように経過した時間の長さを渡します。

The true parameter is telling it to loop the animation.
真のパラメータは、ループにアニメーションを、それを語っている。

Of course, you can have multiple animations from a single texture Atlas.
もちろん、単一のテクスチャアトラスから複数のアニメーションを持つことができます。

Consider the spritesheet we are currently working with.
私たちは、現在作業しているspritesheetを考えてみましょう。

If you take a look at the .atlas file, you will see each individual frame is named:
あなたは.atlasファイルを見てみる場合は、各個別のフレームの名前は表示されます。


4.ステップ2



What we want to do is treat this as two sepeate animations.  Frames 1 through 10 represent an upward roll, while 11-20 are a downward roll.  Let’s take a look at how we do that:
私たちがやりたいことは2 sepeateアニメーションとして扱いです。 11-20が下方にロールしている間のフレーム1〜10は、上方向にロールを表します。それでは私たちはそれを行う方法を見てみましょう:

Game class の修正。
BasicsGraphics-core の MyGdxGame.java を修正。



実行してみる。(※ステップ2)

背面にあるウインドウがステップ1の実行結果で
前面にあるウインドウがステップ2の実行結果です。


以上、終わり。

0 件のコメント:

コメントを投稿