gooブログはじめました!

写真付きで日記や趣味を書くならgooブログ

rokonのspriteにTouchEventを設定

2010-12-12 17:45:37 | android

せっかく使っていたrokonですが、開発者が大学の最終学年および、いそがしいからこれ以上をcommitしないと http://code.google.com/p/rokon/   
のトップに書かれていました。ちょっと残念です。
そうなると、現状、使いやすいのはAndEngineしかなくなるような気がします。

AndEngineはSpriteにTouchAreaを指定できるが、rokonはそんな機能がないが、Spriteを作成して、setTouchable();
を設定してTouchEvnetを受け取れるようにした後なら、onTouchDown,onTouchUp,onTouch,onTouchMoveの eventを受け取れるようになる。Spriteでeventを受け取るか、GameScne(Sceneを継承したクラス)で受け取るかはアプリに より決定したほうがいいみたい。



rokonのspriteにTouchEventを設定 rokonのsampleでGameScene.javaを修正

<form>

rokonのspriteにTouchEventを設定

<textarea rows="8" cols="75" readonly="readonly">package org.jp.rokon2; import android.view.MotionEvent; import com.stickycoding.rokon.FPSCounter; import com.stickycoding.rokon.Scene; import com.stickycoding.rokon.Sprite; import com.stickycoding.rokon.background.FixedBackground; import com.stickycoding.rokon.device.Accelerometer; import com.stickycoding.rokon.modifiers.Blink; import com.stickycoding.rokon.Modifier; import android.util.Log; public class GameScene extends Scene { private FixedBackground background; private Sprite bob, bob2, bob3; private Sprite m_kazan,m_kazan2; private Blink  m_blink; public GameScene() { //super(); //input sample start これだと1毎のlayerに1つだけspriteを表示できる。 //super(1, 1); //sprite sample super(5, 10); // Setup the scene to have 1 layer of sprites with a maximum of 3 sprites per layer. //layer1つでsprite10こ使える //super(1,10); //super(2,10); // (note that the background does NOT count as a sprite) // Create a fixed background (ie a non-changing background) with the texture loaded in Textures.load(). setBackground(background = new FixedBackground(Textures.background));     // Create the Bob sprite.    // bob = new Sprite(100, 220, Textures.bob.getWidth(), Textures.bob.getHeight()); bob = new Sprite(100, 50, Textures.bob.getWidth(), Textures.bob.getHeight()) { public void onTouchDown(float x, float y, MotionEvent event, int pointerCount, int pointerId) { Log.d("spriteevent","bob1 onTouchDown" + "x:" + String.valueOf( x ) + "y;" + String.valueOf( y )); } public void onTouchUp(float x, float y, MotionEvent event, int pointerCount, int pointerId) { Log.d("spriteevent","bob1 onTouchUp" + "x:" + String.valueOf( x ) + "y;" + String.valueOf( y )); } public void onTouch(float x, float y, MotionEvent event, int pointerCount, int pointerId) { Log.d("spriteevent","bob1 onTouch" + "x:" + String.valueOf( x ) + "y;" + String.valueOf( y )); } public void onTouchMove(float x1, float y1, MotionEvent event, int pointerCount, int pointerId) { Log.d("spriteevent","onTouchMove" + "x:" + String.valueOf( x1 ) + "y;" + String.valueOf( y1 )); //bobの座標をTouchMoveに合わせて更新する x = x1; y = y1; } }; //sprite bob.setTouchable();     bob.setTexture(Textures.bob); //     // Add the Bob sprite to the first layer.     add(0, bob);     bob2 = new Sprite(100, 200, Textures.bob.getWidth(), Textures.bob.getHeight()) { public void onTouchDown(float x, float y, MotionEvent event, int pointerCount, int pointerId) { Log.d("spriteevent","bob2 onTouchDown" + "x:" + String.valueOf( x ) + "y;" + String.valueOf( y )); } public void onTouchUp(float x, float y, MotionEvent event, int pointerCount, int pointerId) { Log.d("spriteevent","bob2 onTouchUp" + "x:" + String.valueOf( x ) + "y;" + String.valueOf( y )); } public void onTouch(float x, float y, MotionEvent event, int pointerCount, int pointerId) { Log.d("spriteevent","bob2 onTouch" + "x:" + String.valueOf( x ) + "y;" + String.valueOf( y )); } public void onTouchMove(float x1, float y1, MotionEvent event, int pointerCount, int pointerId) { Log.d("spriteevent","bob2 onTouchMove" + "x:" + String.valueOf( x1 ) + "y;" + String.valueOf( y1 )); } };     bob2.setTouchable();     bob2.setTexture(Textures.bob); //     // Add the Bob sprite to the first layer.     add(0, bob2); } @Override public void onGameLoop() { // This is the game loop that is called once every frame. } @Override public void onPause() { // This is called when the game is hidden. (ie when the user switches to another app without turning this one off) // (this should be used to pause the game logic so the user doesn't miss anything while he/she's gone) } @Override public void onResume() { // And when the user return to this app. } @Override public void onReady() { // This is called when the scene has been successfully created and is ready to be used. } public void onTouchDown(float x, float y, MotionEvent event, int pointerCount, int pointerId) { // This is called when you press down on the screen. } @Override public void onTouchMove(float x, float y, MotionEvent event, int pointerCount, int pointerId) { // This is called when you move your finger over the screen. (ie pretty much every frame if your holding your finger down) // Here we'll just make Bob follow your finger. } @Override public void onTouchUp(float x, float y, MotionEvent event, int pointerCount, int pointerId) { // And this is called when you stop pressing. } } </textarea>

</form>

コメントを投稿