Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi there,

I'm now trying to mastering OpenGL for mobile gaming, so I'll need to ask a boxes of question about it. Here's my current problem now.

Specification :
- android SDK API 6 (android 2.1)
- using command line to create new project, build, and install
- using emulator (AVD) to run program
- Windows 7 Pro SP 1 32x

I'm trying to rotate triangle in my android on touch (onTouchEvent).
Here's my code snippet :

Java
package .....;
import android.app.Activity;
import android.content.Context;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

public class segitiga extends Activity{
..
..
..
..
}

class HelloOpenGLES10SurfaceView extends GLSurfaceView {
...
...
...
public HelloOpenGLES10SurfaceView(Context context){
...
...
@Override 
    public boolean onTouchEvent(MotionEvent e) {
   
        float x = e.getX();
        float y = e.getY();
        
        switch (e.getAction()) {
            case MotionEvent.ACTION_MOVE:
    
                float dx = x - mPreviousX;
                float dy = y - mPreviousY;
    
                // reverse direction of rotation above the mid-line
                if (y > getHeight() / 2) {
                  dx = dx * -1 ;
                }
    
                // reverse direction of rotation to left of the mid-line
                if (x < getWidth() / 2) {
                  dy = dy * -1 ;
                }
              
                mRenderer.mAngle += (dx + dy) * TOUCH_SCALE_FACTOR;
                requestRender();
        }

        mPreviousX = x;
        mPreviousY = y;
        return true;
    }  

}
}


The porblem is when I tried to compile (install debug / adb installd ) to my emulator it's throw me error like this on the line of :
Java
public boolean onTouchEvent(MotionEvent e)


It need ";" after onTouchEvent and after "e". So it should be this like according to the compiler :
Java
public boolean onTouchEvent;(MotionEvent e;)


And that's wrong things.
How should I fixe it?

Thanks in advances
Posted

1 solution

It looks like you are nesting the functions. onTouchEvent() is placed within the function HelloOpenGLES10SurfaceView(). It needs to be placed in a class directly, either the main class, or a nested class.
 
Share this answer
 
Comments
satrio_budidharmawan 26-Apr-12 5:05am    
But it's should be there I thought.
Because its GLSurfaceView's member, if I moved it out, it wont even know requestRender() and some things.
Niklas L 26-Apr-12 5:11am    
In that case, place it outside of HelloOpenGLES10SurfaceView's constructor.
satrio_budidharmawan 26-Apr-12 5:14am    
So its classless?
satrio_budidharmawan 26-Apr-12 5:16am    
But somehow I tried to separate it onto 3 different files : main, view, and renderer.
And it works like charms, but I still wanted to know what's wrong with my previous version
Niklas L 26-Apr-12 5:34am    
It's not "classless". It should look like:
class HelloOpenGLES10SurfaceView extends GLSurfaceView {
...
public HelloOpenGLES10SurfaceView(Context context){
...
} // end construtor
@Override
public boolean onTouchEvent(MotionEvent e) {
}
} // End class

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900