Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9205873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:03:48+00:00 2026-06-18T00:03:48+00:00

I’m making a simple Breakout game, and so far I have created the ball

  • 0

I’m making a simple Breakout game, and so far I have created the ball and the player. I made the ball so that it moves to the position you touch, but I’d rather have it moving towards the direction I swipe. As in, the player moves along with my finger. Here’s the code I have in my MainActivity:

package com.example.breakout;

import android.os.Bundle;
import android.app.Activity;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

public class MainActivity extends Activity implements OnTouchListener, OnGestureListener{

    BreakoutView bv;
    GestureDetector gs;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        bv = new BreakoutView(this);
        setContentView(bv);
        bv.setOnTouchListener(this);


        gs = new GestureDetector(MainActivity.this, this);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);

        return true;
    }

    @Override
    public boolean onTouch(View arg0, MotionEvent arg1) {
        /*if(arg1.getY()<600){
            bv.p1.x = arg1.getX();
            bv.p1.y = 600;
        }*/
        return false;
    }

    @Override
    public boolean onTouchEvent(MotionEvent me){
        return gs.onTouchEvent(me);
    }

    @Override
    public boolean onDown(MotionEvent arg0) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY) {
        /*if(e2.getX()>e1.getX()){
            //Move right
            bv.p1.x = e2.getX();

        }*/

        return false;
    }

    @Override
    public void onLongPress(MotionEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean onScroll(MotionEvent arg0, MotionEvent arg1, float arg2,
            float arg3) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void onShowPress(MotionEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean onSingleTapUp(MotionEvent arg0) {
        // TODO Auto-generated method stub
        return false;
    }

}

So, the two methods I have tried are the onTouch and the onFling ones. The onTouch “teleports” my player where I touch, which isn’t what I want. The onFling only moves my player when I’m done swiping, so that doesnt work either. My question is, how can I make it move as I swipe my finger? Do I use onDown? Any help is appreciated.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T00:03:49+00:00Added an answer on June 18, 2026 at 12:03 am

    Assuming you have some sort of game loop set up where the UI is updated on intervals:

    /**
     * Onclick event for Touch Screen
     */
    @Override
    public boolean onTouchEvent(MotionEvent input) {
        if (input.getAction() == MotionEvent.ACTION_MOVE) {
            float currX = input.getX();
            float currY = input.getY();
    
            if (!swiping) {
                swiping = true;
                swipeStartY = currY;
                swipeStartX = currX;
            }
    
            if (swiping) {          
                double newDirection = Math.atan2((currY-ball.getY()),currX-ball.getX());
                ball.setDirection(newDirection);
            }
    
        } else if (input.getAction() == MotionEvent.ACTION_UP) {
            if (swiping) {
                swiping = false;
            }
        }
    }
    

    This way the ball’s direction is updated on each tick of the game thread and moves towards your finger (the touch event X/Y location). I’m not sure how you have set up your drawing/physics/etc. so I can’t provide meaningful function calls here. If you need to modify it so it swipes in a straight line after you lift your finger then store the initial X/Y point (swipeStartX/swipeStartY in this case) and only set the new direction after you stop swiping.

    EDIT:

    I should mention, my class definition that contains this code is as follows:

    public class GameView extends SurfaceView implements SurfaceHolder.Callback {
      ....
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm making a simple page using Google Maps API 3. My first. One marker
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.