perform single touch on isActionMove() - java

how to perform single touch on isActionMove() because when i move finger on sprites it takes multipal touch events and update scores twice thrice
mHardware[active] = new Sprite(pX, pY, java, this.getVertexBufferObjectManager()) {
#Override
public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float X, float Y) {
if (pSceneTouchEvent.isActionMove()) {
score++;
}
}
};
i cant use isActionDown because its a game like fruit ninja in which i need to move finger across screen
now problem is score is sometimes increasse by 2 sometimes by 3 because when i move finger on sprite application notices several short movements in place of one

you should use
private static boolean isFirstMove;
public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float X, float Y) {
if (pSceneTouchEvent.isActionDown()) {
isFirstMove = true;
}
if (pSceneTouchEvent.isActionMove()) {
if(isFirstMove) {
score++;
isFirstMove = false;
}
}
if (pSceneTouchEvent.isActionUp()) {
isFirstMove = false;
}
});

If you found some what above answer correct for you then I have some suggestion in this.
You have to implement both scene and area touch event for your game scene.
SceneTouch event method contains two events isActionDown() and isActionUp() like in the following code.
public void onSceneTouchEvent(...){
if(pSceneTouchEvent.isActionDown()){
isFirstTouch=true;
return true;
} else if(pSceneTouchEvent.isActionUp()){
isFirstTouch=false;
return true;
}
return false;
}
Area Touch method contains only single event isActionMove() like in the following code.
public void onAreaTouch(...){
if(pSceneTouchEvent.isActionMove()){
if(isFirstMove) {
score++;
isFirstMove = false;
}
return true;
}
return false;
}
You have to follow above strategy because some time desire event not occur for your sprite so you don't get your desire result.

solution given by julien dumortier is working perfectly
static boolean isFirstMove=true;
public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float X, float Y) {
if (pSceneTouchEvent.isActionMove()) {
if(isFirstMove) {
score++;
isFirstMove = false;
}
}
});

Related

Stopping an Animation in render method LibGDX

I have an animation of a flipping coin made of five Textures. I would like to delete the animation when the player's Rectangle overlaps the coin's Rectangle but since I haven't found how to do this, I want to just stop it.
With this code, I can draw a Texture above the Animation, but just if the player is overlapping. How can I make it definite? Thanks for the help.
public boolean catchcoin(Rectangle player, Rectangle coin){
if (Intersector.overlaps(player, coin)) {
return true;
}
return false;
}
public boolean changecoin(Rectangle play, Coin coin ) {
boolean b = m.pegamoeda(play, coin.getRectangle());
if(b){
return true;
}
return false;
}
Part of the render method:
public void render(float delta){
currentframe = (Texture) coin.returnsanimatio().getKeyFrame(stateTime, true);
currentframe2 =(Texture)coin2.returnsanimation().getKeyFrame(stateTime, true);
currentframe3 =(Texture)coin3.returnsanimation().getKeyFrame(stateTime, true);
sp.draw(currentframe, coin.getx(),coin.gety(), 30,30);
sp.draw(currentframe2, coin2.getx(),coin2.gety(), 30,30);
sp.draw(currentframe3,coin3.getx(),coin3.gety(), 30,30);
boolean b = changecoin(player, coin);
if(b){
sp.draw(bluecoin, coin.getx(), coin.gety(), 30,30);
}
sp.end();
}

inputprocessor touch down gives late response?

So I am currently busy with a kind of pong game for android. All i have currently is the paddle and the ball. The controls for the player (the paddle) is as followed:
If player touches on left side of screen, paddle goes left
If player touches on right side of screen, paddle goes right
Actually the above 2 points work fine, but what bothers me, is that when im holding right with my right finger, and after that release it and really fast holding left with my left finger, the paddle only executes one time "going left", and then stops, while I am still holding left. (and vice versa)
I discovered this only happens if I use two fingers. If im holding down right with my finger, and try to go really fast to press the other side, it doesnt stop and actually keeps going left.
But it is important to use two fingers, since that is the way how the game should be played.
This all may be explained unclear, so you can ask in the comments more specific questions.
Player.java: http://pastebin.com/pdFZJTRB
MyInputProcessor.java: http://pastebin.com/XPzi8JPB
I think your problem resides in the fact that you have the touchDown boolean being shared between the left and right sides. If you press the left side fast enough, the touchDown for the left finger gets set to true before the touchDown for the right finger is set to false, thus making them all false. Try something like this instead:
package com.nahrot.teleportball;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.math.Vector2;
public class MyInputProcessor implements InputProcessor
{
public Vector2 leftTouchPos = new Vector2();
public Vector2 rightTouchPos = new Vector2();
public boolean touchLeft = false;
public boolean touchRight = false;
#Override
public boolean keyDown(int keycode)
{
return false;
}
#Override
public boolean keyUp(int keycode)
{
return false;
}
#Override
public boolean keyTyped(char character)
{
return false;
}
#Override
public boolean touchDown(int screenX, int screenY, int pointer, int button)
{
if(sideTouched(screenX, true))
{
touchLeft = true;
leftTouchPos.set(screenX, screenY);
}
if(sideTouched(screenX, false))
{
touchRight = true;
rightTouchPos.set(screenX, screenY);
}
return false;
}
#Override
public boolean touchUp(int screenX, int screenY, int pointer, int button)
{
if(sideTouched(screenX, true))
{
touchLeft = false;
}
if(sideTouched(screenX, false))
{
touchRight = false;
}
return false;
}
#Override
public boolean touchDragged(int screenX, int screenY, int pointer)
{
return false;
}
#Override
public boolean mouseMoved(int screenX, int screenY)
{
return false;
}
#Override
public boolean scrolled(int amount)
{
return false;
}
private boolean sideTouched(int x, boolean checkLeft)
{
if(checkLeft)
{
if(x <= Gdx.graphics.getWidth() / 2)
return true;
}
else
{
if(x > Gdx.graphics.getWidth() /2)
return true;
}
return false;
}
}
Here, I separated the booleans and vectors into left side and right side, and made a convenience function for checking whether a touchDown or touchUp is on the right or left side of the screen, and adjusted the corresponding booleans and vectors accordingly. I assume the vector was needed to check which side was pressed anyway, so I suspect you could work it so that all you need is the two booleans for the sides touched. This code is untested, by the way, but you should get the idea if it doesn't work.

Processing, redraw on mouse click within a class

I'm trying to make a simple click to change color in class draw. I tried to print the statement to see if it would re-draw but it's not re-drawing at all. The click works. Does anybody know why this is happening?
Here's the code so far.
Monster firstmonster;
Monster secondmonster;
void setup() {
size(600,400);
firstmonster = new Monster(100,200);
secondmonster = new Monster(300,200);
firstmonster.draw();
secondmonster.draw();
noLoop();
}
class Monster {
float xpos;
float ypos;
boolean isAngry;
int timeAngry;
Monster(float x, float y) {
xpos = x;
ypos = y;
isAngry = false;
timeAngry = 0;
}
void draw() {
if(isAngry = true && timeAngry<60){
print(int(timeAngry));
timeAngry=timeAngry+1;
rectMode(CENTER);
fill(127-timeAngry*5,0,0);
rect(xpos+100,ypos+100,20,100);
fill(255,200,200);
ellipse(xpos+100,ypos+70,60,60);
ellipse(xpos+81,ypos+70,16,32);
ellipse(xpos+119,ypos+70,16,32);
line(xpos+90,ypos+150,xpos+80,ypos+160);
line(xpos+110,ypos+150,xpos+120,ypos+160);
} else {
timeAngry = 0;
rectMode(CENTER);
print(int(timeAngry));
fill(127,0,0);
rect(xpos+100,ypos+100,20,100);
fill(255,200,200);
ellipse(xpos+100,ypos+70,60,60);
ellipse(xpos+81,ypos+70,16,32);
ellipse(xpos+119,ypos+70,16,32);
line(xpos+90,ypos+150,xpos+80,ypos+160);
line(xpos+110,ypos+150,xpos+120,ypos+160);
}
}
void mousePressed(){
poke();
}
void poke(){
isAngry = true;
print(timeAngry);
timeAngry=timeAngry+1;
redraw();
}
}
void mousePressed(){
firstmonster.mousePressed();
}
Also I can't seem to make the two models differentiate. If I put firstmonster.poke() then both the first and second model color change.
You don't have a global void draw() { .. } in your code, so there is nothing for redraw to trigger. Move code that should be draw as part of a frame out of setup(). Setup is for, unsurprisingly, one time setup code.
void setup() {
size(600,400);
firstmonster = new Monster(100,200);
secondmonster = new Monster(300,200);
noLoop();
}
void draw() {
firstmonster.draw();
secondmonster.draw();
}
Should do the trick.

LibGdx: Utilizing a Gesture Listener

I would like to use some of the more complex touch screen gestures that you can't access from
Gdx.input
I saw that to do this i must create a Gesture listener so i created the class GestureHandler and copied the code from the wiki. My gesture handler looks like this:
public class GestureHandler implements GestureListener {
#Override
public boolean touchDown(float x, float y, int pointer, int button) {
return false;
}
#Override
public boolean tap(float x, float y, int count, int button) {
return false;
}
#Override
public boolean longPress(float x, float y) {
return false;
}
#Override
public boolean fling(float velocityX, float velocityY, int button) {
return false;
}
#Override
public boolean pan(float x, float y, float deltaX, float deltaY) {
return false;
}
#Override
public boolean zoom(float initialDistance, float distance) {
return false;
}
#Override
public boolean pinch(Vector2 initialPointer1, Vector2 initialPointer2, Vector2 pointer1, Vector2 pointer2) {
return false;
}
}
My question is now that i have set up the gesture listener how can i use it. How can i get the info from these methods? Thank you for any help!
From the wiki:
A GestureDetector is an InputProcessor in disguise. To listen for
gestures, one has to implement the GestureListener interface and
pass it to the constructor of the GestureDetector. The detector is
then set as an InputProcessor, either on an InputMultiplexer or as
the main InputProcessor
I admit that is rather dense. But a bit farther down on the wiki you'll see:
Gdx.input.setInputProcessor(new GestureDetector(new MyGestureListener()));
To rephrase the above in hopefully less dense English: Your GestureHandler instance is passed to a Libgdx GestureDetector instance. That object will accumulate "raw" inputs and convert them into higher-level "gestures". To get the raw inputs, it needs to be installed where the raw inputs will be delivered to it. The most basic way to install it via Gdx.input.setInputProcessor, but you could also install it via an InputMultiplexer (but that's not worth getting into here).

Score increases randomly onActionMove

when i touch finger on sprite, score increses randomly not by fix rate i mean that i set score
to increase 50 each time but when i scroll on sprite sometimes it increases by 100 sometimes by 150 and 200;
mHardware1[active] = new Sprite(pX, pY, samsung,
this.getVertexBufferObjectManager()) {
#Override
public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
float X, float Y) {
if (pSceneTouchEvent.isActionMove()) {
scene.detachChild(mHardware1[active]);
score+50;
}
it works when i use isActionDown in place of isActionMove but m working a game like fruit ninja in which i need to scroll finger on screen that why i cant use isActionDown
You can use a SurfaceScrollDetector, which detects when the user is sliding his finger across the screen. It has the following events associated with it:
onScrollStarted
onScroll
onScrollFinished
I guess you could increase by 50 when you reach the onScrollFinished.
I am currently using the SurfaceScrollDetector for a project, but I haven't used it in a way you are asking about, so I can't say forsure if it will work as expected.
Here is one of the examples that uses it (in addition to the PinchZoomDetector):
https://github.com/nicolasgramlich/AndEngineExamples/blob/GLES2/src/org/andengine/examples/PinchZoomExample.java
Scene updates happen on a different thread to UI events, so possibly multiple queued touchscreen events are getting through before the event handler realises that the sprite has been detached.
Set a local boolean variable to prevent this, e.g.:
boolean touchProcessed = false;
mHardware1[active] = new Sprite(pX, pY, samsung,
this.getVertexBufferObjectManager());
#Override
public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
float X, float Y) {
if (!touchProcessed & pSceneTouchEvent.isActionMove()) {
touchProcessed = true;
scene.detachChild(mHardware1[active]);
score += 50;
}
}
Note that you can use: mHardware1[active].detachSelf() instead of scene.detachChild(...).
Also note that you should be detaching in the sprite in the update thread, thus:
boolean touchProcessed = false;
mHardware1[active] = new Sprite(pX, pY, samsung,
this.getVertexBufferObjectManager());
#Override
public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
float X, float Y) {
if (!touchProcessed & pSceneTouchEvent.isActionMove()) {
touchProcessed = true;
score += 50;
engine.runOnUpdateThread(new Runnable() {
#Override
public void run() {
mHardware1[active].detachSelf();
}
});
}
}

Categories