0
\$\begingroup\$

Let's say that I have a cannon on the bottom of the screen that shoots balls to the click direction. I also have some collisions algorithms and here comes the problem. I am trying to implement an OnHold method that shoots continously and it works, but I think that I need to make shooting on another Thread. I've implemented this in two ways: 1. I was checking the delay and if it passed I shot 2. I was checking previous ball y position and when it floated away I shot

The problem comes here that when I hold the button for some time, for example. 5 balls are okay (they're near each other - not colliding) and then comes a ball that is late O.o

I don't know how to implement a new Thread within these MotionEvents. If is there someone who can point me (if should I make in on Threads or something) a solution - I would be grateful :)

  @Override
public boolean onTouchEvent(MotionEvent event) {
    // TODO Auto-generated method stub

    touched_x = event.getX();
    touched_y = event.getY();
    int i = 100;

    int action = event.getAction();
    if (action == MotionEvent.ACTION_DOWN) {
        startShootTime = new Date().getTime();
        running = true;
    } else if (action == MotionEvent.ACTION_MOVE ) {
        shootTime = new Date().getTime();
        // movement: cancel the touch press
        running = true;
        touched_x = event.getX();
        touched_y = event.getY();
       // && game.gameLoop.current_time - shootTime > 400 &&
              //  game.gameLoop.current_time - startShootTime > 100
        //  while(actionUpflag) {
        int size = game.gameLoop.balls.size();
        if(size == 0){
            game.gameLoop.balls.add(new Ball(metrics.widthPixels, metrics.heightPixels, touched_x, touched_y, game.gameLoop.ball_bmp_width, metrics.heightPixels-game.gameLoop.ball_bmp_width/2));
        }else if(size > 0 && game.gameLoop.balls.get(size-1).image_center_y < metrics.heightPixels - game.gameLoop.ball_bmp_width-50)
        game.gameLoop.balls.add(new Ball(metrics.widthPixels, metrics.heightPixels, touched_x, touched_y, game.gameLoop.ball_bmp_width, metrics.heightPixels-game.gameLoop.ball_bmp_width/2));

        invalidate(); // request draw

    } else if (action == MotionEvent.ACTION_UP) {
        running = false;
    }

    return true;
}
\$\endgroup\$
2
  • \$\begingroup\$ No. You should not use threads for this. \$\endgroup\$ Commented Sep 14, 2015 at 11:40
  • \$\begingroup\$ @msell then what should I use here? Thanks for the response \$\endgroup\$ Commented Sep 14, 2015 at 15:52

1 Answer 1

0
\$\begingroup\$

It looks as if you have some kind of game loop running always? And so the game has a cannon object? If this is the case, you could use the touch event to inform the cannon update.

Please excuse my pseudo code..

in the event

public boolean onTouchEvent(MotionEvent event)
{

    touched_x = event.getX();
    touched_y = event.getY();

    game.gameLoop.cannon.fireAtTarget(x,y);
}

the cannon class

   void cannon::fireAtTarget( x,y )
   {
     firing = true;
     targetx = x;
     targety = y;
   }

the cannon update

void cannon::update( timeNow )
{

    if( firing && (timeNow-lastTimeFired > 0.5) )
    {
        // fire at targetx, targety. 
        lastTimeFired = timeNow;
    }

}

Dont forget to disable firing in the touch up event

\$\endgroup\$
1
  • \$\begingroup\$ Working like a charm - Im adding balls while updating gameloop thread. \$\endgroup\$ Commented Sep 14, 2015 at 17:12

You must log in to answer this question.