我是android developpement的新手,这是我的问题,我怎样才能从MainActivity onTouchEvent中的类“Ship”中更改Vertex内部类“MainRenderer”?
这是我的代码: MainActivity.java:
package com.example.galaga2d;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.app.Activity;
import android.view.MotionEvent;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;
public class MainActivity extends Activity {
//private boolean isTouch = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Убираем тайтл приложения, тобишь делаем его FullScreen
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
// Создаём новый Surface и устанавливаем MainRenderer
GLSurfaceView view = new GLSurfaceView(this);
view.setRenderer(new MainRenderer());
view.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
setContentView(view);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int X = (int) event.getX();
int Y = (int) event.getY();
int upX = (int) event.getX();
int upY = (int) event.getY();
int downX = (int) event.getX();
int downY = (int) event.getY();
int eventaction = event.getAction();
switch (eventaction) {
case MotionEvent.ACTION_DOWN:
Toast.makeText(this, "ВНИЗ "+"X: "+X+" Y: "+Y, Toast.LENGTH_SHORT).show();
break;
case MotionEvent.ACTION_MOVE:
Toast.makeText(this, "ДВИЖЕНИЕ "+"X: "+X+" Y: "+Y, Toast.LENGTH_SHORT).show();
break;
case MotionEvent.ACTION_UP:
Toast.makeText(this, "ВВЕРХ "+"X: "+X+" Y: "+Y, Toast.LENGTH_SHORT).show();
break;
}
return true;
}
}
和MainRenderer.java:
package com.example.galaga2d;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
import java.nio.ByteOrder;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.opengl.GLSurfaceView.Renderer;
public class MainRenderer implements Renderer {
public int playerSize = 0;
private Ship playerShip = new Ship();
private Astedoid enemyAstedoid = new Astedoid();
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
gl.glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
}
@Override
public void onDrawFrame(GL10 gl) {
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
playerShip.draw(gl);
enemyAstedoid.draw(gl);
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glOrthof(0, width, height, 0, 1, -1);
gl.glMatrixMode(GL10.GL_MODELVIEW);
}
// --------------------------------------------------------------------------------
class Ship {
public int health = 100;
public int life = 3;
public FloatBuffer ShipVertexBuffer;
public float ShipVerticles[] = {
5, 5, // лево низ
5, 10, // лево вверх
10, 5, // право низ
10, 10 // право вверх
};
public Ship() {
ByteBuffer bb = ByteBuffer.allocateDirect(36);
bb.order(ByteOrder.nativeOrder());
ShipVertexBuffer = bb.asFloatBuffer();
ShipVertexBuffer.put(ShipVerticles);
ShipVertexBuffer.position(0);
}
public void draw(GL10 gl) {
gl.glColor4f(0.0f, 1.0f, 0.0f, 1.0f);
gl.glVertexPointer(2, GL10.GL_FLOAT, 0, ShipVertexBuffer);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
}
}
class Astedoid {
public int health = 100;
public int life = 3;
public FloatBuffer AsteroidColorBuffer;
public FloatBuffer AsteroidVertexBuffer;
public float AsteroidVerticles[] = {
25, 25, // лево низ
25, 30, // лево вверх
30, 25, // право низ
30, 30 // право вверх
};
public Astedoid() {
ByteBuffer bb = ByteBuffer.allocateDirect(36);
bb.order(ByteOrder.nativeOrder());
AsteroidVertexBuffer = bb.asFloatBuffer();
AsteroidVertexBuffer.put(AsteroidVerticles);
AsteroidVertexBuffer.position(0);
}
public void draw(GL10 gl) {
gl.glColor4f(0.0f, 0.0f, 1.0f, 1.0f);
gl.glVertexPointer(2, GL10.GL_FLOAT, 0, AsteroidVertexBuffer);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
}
}
// --------------------------------------------------------------------------------
}
我想这样做,创建一些像“objectSize”这样的变量,并在“case MotionEvent.ACTION_DOWN”上的主要活动onTouchEvent中给它一些值5,例如:
case MotionEvent.ACTION_DOWN:
objectSize = 5;
break;
我希望使用此变量来更改Ship类中MainRenderer中绘图对象的大小,例如:
public float ShipVerticles[] = {
5*objectSize , 5*objectSize,
5*objectSize, 10*objectSize,
10*objectSize, 5*objectSize,
10*objectSize, 10*objectSize
};
我怎么能这样做? THX!