Score increases randomly onActionMove - java

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();
}
});
}
}

Related

How do I add touch controls to an Object in libGDX?

I'm making a balloon popping game for android in libGDX and I'm currently stuck on how to make the balloons pop when the user touches them. I tried using the touchDown() method,
b = new Balloon();
b.addListener(
new InputListener()
{
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button)
{
b.remove();
popped++;
return true;
}
});
mainStage.addActor(b);
}
but it doesn't work. I need a way so that only the balloons that I touch get popped and the others don't.
p.s I'm still learing libGDX so I'm a pretty big noob at it.
Edit: The Balloon is an Actor, and I did set the InputProcessor as well. The touch thing works but it doesn't pop the balloon that I touch it only pops the balloons that spawn at the starting x-axis.
At first you have to set your stage as input processor with Gdx.input.setInputProcessor(mainStage)
Then you can just add a ClickListener where you override the clicked method like this:
b.addListener(new ClickListener() {
#Override
public void clicked(InputEvent event, float x, float y) {
super.clicked(event, x, y);
//YOUR_CODE
}
});

onTouch calling invalidate onDraw - not smooth

I have a code in my custom view, where if I touch, a circle is drawn. When I move the circle follow along. Code as below
// override onDraw
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (circleBounds != null) {
canvas.drawArc(circleBounds, 360, 360, false, mPaint);
}
}
// when ACTION_DOWN start touch according to the x,y values
private void startTouch(float x, float y) {
mPath.moveTo(x, y);
mX = x;
mY = y;
circleBounds.set(mX - mRadius, mY - mRadius, mX + mRadius, mY + mRadius);
invalidate();
}
//override the onTouchEvent
#Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
startTouch(x, y);
break;
case MotionEvent.ACTION_MOVE:
startTouch(x, y);
break;
}
return true;
}
When I move slowly, the movement is smooth. But when I move fast, one could feel the flickering. I guess invalidate() happens many time, causing the flickering. Is there a way to make it smoother (reduce the flickering on the drawn circle)?
Invalidate is not your issue, the system has double buffering, and you should not see a flicker.
From your code, I can think of 2 way to improve:
in onStartTouch you call mPath.moveTo(). It is not clear why you need mPath, but this path will keep growing and growing and will need more memory
canvas.drawArc is rendered by by CPU (it creates a bitmap and draws the arc into it on every call to onDraw). It would be faster to render yourself the circle once into a bitmap, and draw that bitmap in onDraw. If you keep using the same bitmap, it will be loaded once to the GPU and drawing will be much faster
I know this question is old, but in case someone else has same problem, the solution is to use postInvalidate() instead of the invalidate() in the onTouchEvent method, which will make it much more fluid.
As we receive many events in onTouchEvent, it's more effective to put invalidate call into events queue for future processing instead of doing it immediately.
public class MyView extend FrameLayout implement OnTouchListener{
public MyView(params){
init();
}
private void init(){
ImageView imageView = new ImageView();
imageView.setImageResource(R.mipmap.logo);
addView(imageview)
imageView.setOnTouchListener(this);
}
private boolean onTouch(params){
//some logic to control
//use imageView.setLayoutParams() instead of invalidate();
}
}

2D Moving player with canvas

So I was told to create a simple game in android using canvas only for a school project (meaning I can't use any extensions or game engines beside the ones that come with android), and I wanted to create a simple 2d game with a player walking around.
I did it like that:
public GameView(Context c) {
// TODO Auto-generated constructor stub
super(c);
this.c=c;
this.Sprite=BitmapFactory.decodeResource(getResources(), R.drawable.walk1);
this.Sprite=Bitmap.createScaledBitmap(Sprite, Sprite.getWidth()*2, Sprite.getHeight()*2, false);
sprite2=new Sprite("Spicy",Sprite);
this.requestFocus();
this.setFocusableInTouchMode(true);
animate1();
}
I created a view class, loaded a simple player sprite, created sprite class and an handler -
public void animate1(){
handlerAnimation100 = new Handler();
final Runnable r = new Runnable() {
public void run() {
invalidate();
handlerAnimation100.postDelayed(this, 1);
}
};
handlerAnimation100.postDelayed(r, 1);
}
that does invalidate every 0.001 seconds for the game time and animation.
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
sprite2.Draw(canvas);
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
sprite2.Update(keyCode);
invalidate();
return false;
}
on onDraw i called the draw function in the sprite class and onkeydown I sent the key pressed to the update function in the sprite class.
Now for the sprite class there isn't something special:
public class Sprite {
enum State
{
Walking
}
State mCurrentState = State.Walking;
int mDirection = 0;
int mSpeed = 0;
int mPreviousKeyboardState;
private String spriteName;
Bitmap sprite;
int SPRITE_SPEED = 5;
int MOVE_LEFT = -1;
int MOVE_RIGHT = 1;
private float mScale = 1.0f;
Point Position;
public Sprite(String name,Bitmap sprite) {
this.sprite=sprite;
this.spriteName=name;
Position=new Point(150,150);
}
public void Update(int keyboard)
{
int aCurrentKeyboardState = keyboard;
UpdateMovement(aCurrentKeyboardState);
mPreviousKeyboardState = aCurrentKeyboardState;
}
private void UpdateMovement(int aCurrentKeyboardState)
{
if (mCurrentState == State.Walking)
{
mSpeed = 0;
mDirection = 0;
if (aCurrentKeyboardState==KeyEvent.KEYCODE_A)
{
mSpeed = SPRITE_SPEED;
mDirection = MOVE_LEFT;
}
else if(aCurrentKeyboardState==KeyEvent.KEYCODE_D)
{
mSpeed = SPRITE_SPEED;
mDirection= MOVE_RIGHT;
}
Position.x += mDirection * mSpeed;
}
}
public void Draw(Canvas c)
{
c.drawBitmap(sprite, Position.x,Position.y, null);
}
}
I just change the position of the image and move it depending on the key pressed.
Now here is the problem:
Using handler and invalidate was the only option I was able to find to replace "Game time" that appears in game engines, and although it works, it works very choppily, if the speed of the player is high it looks like it jumps pixels and if its low the animation is blurry and very slow, it looks like the invalidate takes more time and it happens not every 0.001 seconds but 0.5 seconds or so.
Here is how it looks like:
Slow speed (blurry and very slow):
Faster Speed (choppy not smooth):
Is there a better way to do it, again with only using what android offers?
Threads! However the handler is not the root of your problem judging by how slow your animation is running and how choppy the fast one is, you are probably using a canvas tied into an ImageView? This is not very fast at all. You should look into using a SurfaceView (works with lower APIs but not the fastest) or into TextureView (super fast, I had to delay my thread cause the animation was just a blur). Both of these rely on canvas at their core.
There are lots of examples out there on in internets on how to code these and you can adapt them for your purposes. To give you a place to start you can look HERE at some samples I wrote.

perform single touch on isActionMove()

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;
}
}
});

libGDX Multiplexer- removing a processor

I have a platform button the game screen, and I'm trying to make it so the user presses it once, clicks somewhere on the screen to draw one platform, and then if they clicked the screen again, nothing would happen.
Right now, before I click the platform button, nothing happens which is what I want. When I press the platform button, the user can click the screen to draw a platform, but, after pressing the platform button once, every time they click the screen, a platform gets drawn so I'm having trouble making it so they can only draw one. I thought using removeProcessor() would've worked, but it's not.
InputController inputProcessor;
InputMultiplexer multiplexer;
public GameScreen(FallDown game) {
this.game = game;
GAMESCREEN_STATE = WORLD_STATE_READY;
this.cam = new OrthographicCamera(FRUSTUM_WIDTH, FRUSTUM_HEIGHT);
this.cam.position.set(FRUSTUM_WIDTH / 2, FRUSTUM_HEIGHT / 2, 0);
this.cam.setToOrtho(false, FRUSTUM_WIDTH, FRUSTUM_HEIGHT);
batch = new SpriteBatch();
world = new World();
renderer = new WorldRenderer(batch, world);
cam.position.set(FRUSTUM_WIDTH / 2, 105, 0);
inputProcessor = new InputController(game, world, cam);
multiplexer = new InputMultiplexer();
}
Then, at the end of my render method I have
multiplexer.addProcessor(stage);
Gdx.input.setInputProcessor(multiplexer);
These are the listeners for my buttons and I'm just using the reset button as an alternate way to stop the user from drawing platforms.
reset_button.addListener(new InputListener() {
public boolean touchDown(InputEvent event, float x, float y,
int pointer, int button) {
multiplexer.removeProcessor(inputProcessor);
return true;
}
});
platform_button.addListener(new InputListener() {
public boolean touchDown(InputEvent event, float x, float y,
int pointer, int button) {
if (GAMESCREEN_STATE != WORLD_STATE_RUNNING) {
multiplexer.addProcessor(new InputController(game, world, cam));
}
return true;
}
});
Well, I would advice you not to add and remove processors that many times (especially the one in the render method.. move that to the constructor/create method).
An easy way to achieve what you are looking for is just have a boolean variable:
boolean createplatform = false;
And then set it to true when the button is pressed and to false when the first platform is created.
(So when you touch the screen, that boolean decides if a platform is created).

Categories