There is a button picture in my 2D game. I placed the picture in certain region of screen. When the touch occurs within the region of button, some action executes. But I want to implement it like the picture itself listen to the touch input. How to implement that?
My current code is shown below:
sbMidSq1X ,sbMidSq2X, sbMidSq3Y and sbMidSq2Y are boundaries of picture region.
public void onTouchEvent(TouchEvent evt) {
if ((Boolean) player.getUserData("alive")) {
if (evt.getType().TAP.equals(TouchEvent.Type.TAP)) {
//shoot Bullet
if(evt.getX() >= sbMidSq1X && evt.getX() <= sbMidSq2X
&& evt.getY() >= sbMidSq3Y && evt.getY() <= sbMidSq2Y)
{
if (System.currentTimeMillis() - bulletCooldown > 83f) {
bulletCooldown = System.currentTimeMillis();
Vector3f aim = getAimDirection();
Vector3f offset = new Vector3f(aim.y/3,-aim.x/3,0);
// init bullet 1
Spatial bullet = getSpatial("Bullet");
Vector3f finalOffset = aim.add(offset).mult(30);
Vector3f trans = player.getLocalTranslation().add(finalOffset);
bullet.setLocalTranslation(trans);
bullet.addControl(new BulletControl(aim, settings.getWidth(), settings.getHeight(), particleManager/*,grid*/));
bulletNode.attachChild(bullet);
// init bullet 2
Spatial bullet2 = getSpatial("Bullet");
finalOffset = aim.add(offset.negate()).mult(30);
trans = player.getLocalTranslation().add(finalOffset);
bullet2.setLocalTranslation(trans);
bullet2.addControl(new BulletControl(aim, settings.getWidth(), settings.getHeight(), particleManager/*,grid*/));
bulletNode.attachChild(bullet2);
sound.shoot();
}
}
}
}
}