Touch event for custom object

7 views
Skip to first unread message

noon

unread,
Apr 2, 2010, 4:18:03 AM4/2/10
to Rokon
Hi, I want to create a new object called "brick" for a iphone's "Un
block me" like application.

Here a sample screen shot of it:
http://blog.appboy.com/wp-content/uploads/2010/01/20090518_unblock_me_v1.0_iphone_game.jpg

I don't want have to pass touch event from main class that extended
from RokonActivity class to Brick object.

How can I make my brick object handle touch, touchUp, touchDown event
for it self?

Thanks!

noon

unread,
Apr 2, 2010, 7:21:28 AM4/2/10
to Rokon
This is my solution.

package com.stickycoding.RokonExamples;

import android.view.KeyEvent;
import android.view.MotionEvent;

import com.stickycoding.Rokon.Debug;
import com.stickycoding.Rokon.Hotspot;
import com.stickycoding.Rokon.Rokon;
import com.stickycoding.Rokon.RokonActivity;
import com.stickycoding.Rokon.Sprite;
import com.stickycoding.Rokon.Texture;
import com.stickycoding.Rokon.TextureAtlas;
import com.stickycoding.Rokon.TextureManager;
import com.stickycoding.Rokon.Backgrounds.FixedBackground;
import com.stickycoding.Rokon.Handlers.InputHandler;
import com.stickycoding.Rokon.Menu.Menu;
import com.stickycoding.Rokon.Menu.MenuLayers;
import com.stickycoding.Rokon.Menu.MenuObject;
import com.stickycoding.Rokon.ParticleModifiers.*;
import com.stickycoding.Rokon.SpriteModifiers.Grow;
import com.stickycoding.Rokon.SpriteModifiers.Spin;

public class Brick
{
private static final int TOUCH_THRESHOLD_TIME = 100;


private Sprite _sprite;
private Texture _texture;
private Hotspot _hotspot;
private boolean _depressed;
private long _lastTouch = 0;
private int _id;

private boolean _touchDown = false;
private int _touchX, _touchY;
private Hotspot _lastHotspot;

public float xOffset, yOffset;


public void onTouchDown(int x, int y, boolean hotspot) {
Debug.print("BRICK TOUCH DOWN ");
}
public void onTouchUp(int x, int y, boolean hotspot) {
Debug.print("BRICK TOUCH UP");
}
public void onTouch() {
Debug.print("BRICK TOUCH ");
}

public boolean depressed() {
return _depressed;
}

public void onTouch(int x,int y, boolean hotspot) {
_sprite.setXY(x - xOffset, y - yOffset);
}

public Brick ()
{
super();
}

private InputHandler touchHandler = new InputHandler() {
public void onTouchEvent(int x, int y, int action, boolean
hotspot) {
switch(action) {
case MotionEvent.ACTION_DOWN:
onTouchDown(x, y, hotspot);
_touchDown = true;
break;
case MotionEvent.ACTION_MOVE:
onTouch(x, y, hotspot);
break;
case MotionEvent.ACTION_UP:
onTouchUp(x, y, hotspot);
_lastHotspot = null;
_touchDown = false;
break;
}
}
};


public Brick (int id, int x,int y, int type, Texture texture, boolean
createHotspot) {
//super(id, x, y, 0, 0);
_id = id;
_sprite = new Sprite(x, y, texture);
_texture = texture;
if(createHotspot)
_hotspot = new Hotspot(_sprite);


}

public void addToScene(Rokon rokon) {

if(_sprite != null)
rokon.addSprite(_sprite, MenuLayers.LAYER_1);
if(_hotspot != null)
rokon.addHotspot(_hotspot);

rokon.setInputHandler(touchHandler);
}

}


On Apr 2, 3:18 pm, noon <doqkh...@gmail.com> wrote:
> Hi, I want to create a new object called "brick" for a iphone's "Un
> block me" like application.
>

> Here a sample screen shot of it:http://blog.appboy.com/wp-content/uploads/2010/01/20090518_unblock_me...

Reply all
Reply to author
Forward
0 new messages