Jump to content
  • Advertisement
Sign in to follow this  
LAURENT*

method inside boolean onTouchEvent(MotionEvent event) is not being called

This topic is 592 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello:

 

for some reason it appears a method inside my public boolean onTouchEvent(MotionEvent event) isn't being called. I put break point on it and in other various places I expected it to be but had no luck. I simply want to update a special global variable to match the status of the screen touch.  I wonder why the game won't freeze and why my special variable won't update?

 

Below is the code of my overwritten ontouchevent method.

@Override
public boolean onTouchEvent(MotionEvent event){

globalFunctions.setScreenTouch(event.getAction());


return super.onTouchEvent(event); 
}

Share this post


Link to post
Share on other sites
Advertisement

Where is your function at?
 
If it is inside your activity:
 
Does your activity class say it implements OnTouchListener? Do you set the view or layout to use the activity with .setOnTouchListener(this)?
 
 
If it is inside a view:
 
Is the class properly derived from View, or from a child of View? Is the view added to the hierarchy of displayed views or widgets? Are the other views displaying?  Is the touch event within the view's area?

Share this post


Link to post
Share on other sites

Where is this function called? The path from the start of the program isn't too complex. Take a look at this. I never call the onTouchEvent method inside my mainthread class but I didn't need to in my older build. Anyways the on touchevent method is at the very bottom.

 

This is where my program starts

import android.app.Activity;
import android.os.Bundle;
//import android.support.design.widget.FloatingActionButton;
//import android.support.design.widget.Snackbar;
//import android.support.v7.widget.Toolbar;
//import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.view.WindowManager;

public class Game extends Activity {



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //Turn off title
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        //Set screen to full screen
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(new GamePanel(this));
    }
}

Then this class called gamepanel was made


package com.vinnstar.myfirstgame;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import java.util.ArrayList;

/**
* Created by Laurent on 6/30/2016.
*/
public class GamePanel extends SurfaceView implements SurfaceHolder.Callback {




// Unchanging variables of the GamePanel class
public static final int WIDTH = 856;
public static final int HEIGHT = 480;
public static final int MOVESPEED = -5;

//Tier One Classes
private MainThread thread;
private GlobalFunctions globalFunctions = new GlobalFunctions();
private ArrayList<GameObject> gameObject;


//Tier Two Classes
private Physics physics = new Physics(gameObject,globalFunctions);
private Timer timer = new Timer();

//Tier three
//private Scenery bg;
//private ObjectPlayer objectPlayer;
// private ObjectFireBall objectFireBall;

//private CystalPoint cystalPoint;
//private ArrayList<Smokepuff> smoke ; //smoke



public GamePanel(Context context){

super(context);

// add callbacks surfaceholder to intercept events
getHolder().addCallback(this);

thread = new MainThread(getHolder(), this,
gameObject,
physics,
timer,
globalFunctions);

//Make gamepanel focusable to perform task
setFocusable(true);
}


@Override
public void surfaceCreated(SurfaceHolder holder) {

// gameObject = new ArrayList<GameObject>();
// gameObject.add(new ObjectButton(BitmapFactory.decodeResource(getResources(), R.drawable.redcyrstal), globalFunctions, 0));
gameObject = new ArrayList<GameObject>();


//Scenery and ObjectFireBall renders but the player does not.
gameObject.add(new Scenery(BitmapFactory.decodeResource(getResources(), R.drawable.grassbg1)));
gameObject.add(new ObjectPlayer(BitmapFactory.decodeResource(getResources(), R.drawable.helicopter) ,globalFunctions ));
gameObject.add(new ObjectFireBall(BitmapFactory.decodeResource(getResources(), R.drawable.redcyrstal)));



thread = new MainThread(getHolder(), this, gameObject, physics, timer, globalFunctions);
//Safely start game loop
thread.setRunning(true);
thread.start();
}

?//SKIPPING PASS SURFACEONDESTROY
?//SKIPPING PASS SURFACEONCHANGE






//YOU HAVE ARRIVE AT THE FINAL DESTINATION. THE METHOD I'M HAVING PROBLEMS WITH.
@Override
public boolean onTouchEvent(MotionEvent event){

globalFunctions.setScreenTouch(event.getAction());

return super.onTouchEvent(event);
}





Edited by LAURENT*

Share this post


Link to post
Share on other sites

Hello; I've come to reveal that I have figured out the problem.

 

First thing I did was plant a few log cat messages so I could see what code was being executed. The method known as public boolean onTouchEvent(MotionEvent event) wouldn't freeze the program even if it hit a break point in it. This lead to a lot of confusion from me but I did realized the code was being executed. With a few logcat messages spread over the place I finally saw the flow of my program. Apparently the method I thought was given me problems did executed but only when I tapped the screen which is why in an older build I only saw my first logcat messages appear for motion down. I decided to set it to be always true and got my second logcat message to reveal when I wasn't tapping the screen. This also allow for my special variable to be updated with the correct value to represent the screen being touched because when you return false of onTouchEvent(MotionEvent event) nothing happens, not even the methods inside the overwritten method get called.

 

Problem solved.

@Override
        public boolean onTouchEvent(MotionEvent event){


            if(event.getAction() == MotionEvent.ACTION_DOWN) {

                globalFunctions.setScreenTouch(event.getAction());
                Log.d(TAG,"Laurent Key is down");
            }

            else if(event.getAction() == MotionEvent.ACTION_UP) {
                globalFunctions.setScreenTouch(event.getAction());
                Log.d(TAG,"Laurent Key is up");
                return true;
            }
      
            return true;//super.onTouchEvent(event);
        }

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!