Search code examples
androidandroid-layouttouch-eventmotionevent

onTouchEvent() in a Game


I am developing a game application using android.

The Object(a box) slides up and down. And it has to hit the objects(orange and pink balls) coming towards it from the right end of the screen that would increase his score.

There will be black balls as well(shot from the right end of the screen) which he should avoid hitting.


enter image description here

I am having problem with

onTouchEvent(MotionEvent me)

function while implementing the code.

I am following this tutorial in this series of tutorials.


My Questions:

  1. To use the onTouchEvent(MotionEvent me) function do I need to import any class?

  2. The tutorial has declared theonTouchEvent(MotionEvent me) outside the onCreate method. Which is okay. But the program has not called it anywhere. How does it work then?

  3. After writing the code as mentioned in the tutorial, the program is not working as intended. The box appears when the activity starts. However, it disappears as soon as I click on the screen. What could be the problem?

ActivityMain.XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/scoreLabel"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text=" : 300"
        android:paddingLeft="10dp"
        android:gravity="center_vertical"  />

    <FrameLayout
        android:id="@+id/frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/startLabel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="30sp"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="130dp"/>

        <ImageView

            android:id="@+id/box"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:src="@drawable/box"
            android:layout_gravity="center_vertical" />

        <ImageView
            android:id="@+id/orange"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:src="@drawable/orange" />

        <ImageView
            android:id="@+id/black"
            android:layout_width="24dp"
            android:layout_height="24dp"
            android:src="@drawable/black" />

        <ImageView
            android:id="@+id/pink"
            android:layout_width="16dp"
            android:layout_height="16dp"
            android:src="@drawable/pink" />



    </FrameLayout>

</RelativeLayout>

MainActivity.java

package com.example.catcheggs1;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private TextView scoreLabel;
    private TextView startLabel;
    private ImageView box;
    private ImageView orange;
    private ImageView black;
    private ImageView pink;

    //Position
    private int boxY;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        scoreLabel=(TextView)findViewById(R.id.scoreLabel);
        startLabel=(TextView)findViewById(R.id.startLabel);
        box=(ImageView)findViewById(R.id.box);
        orange=(ImageView)findViewById(R.id.orange);
        pink=(ImageView)findViewById(R.id.pink);
        black=(ImageView)findViewById(R.id.black);

        //Move To Out of Screen
        orange.setX(-80);
        orange.setY(-80);
        pink.setX(-80);
        pink.setY(-80);
        black.setX(-80);
        black.setY(-80);


        //Temporary
        startLabel.setVisibility(View.INVISIBLE);
        boxY=500;


    }

    public boolean onTouchEvent(MotionEvent me)
    {
       if(me.getAction()==MotionEvent.ACTION_DOWN)
       {
           boxY -= 1 ;
       }
       box.setY(boxY);

       return true;
    }
}

Solution

  • 1.) No, you do not need to import anything.

    2.) For detailed information you can see the link. It is an event method, it is automatically called, you do not need to call it. https://developer.android.com/guide/topics/ui/ui-events#java

    3.) you use boxY variable when touched and it is assigned an arbitrary number. Your problem is much likely to be caused by that. Rather than that, you should first get the current position than tweak it. You can get current position with this method.

    int[] location = new int[2];
    imageView.getLocationOnScreen(location);
    

    https://developer.android.com/reference/android/view/View.html#getLocationOnScreen(int[])

    Bonus.) onTouchEvent is an activity method, so you should use view's own event listeners for specific tasks rather than onTouchEvent.

    imageView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {   
            return true;
        }
    });