0
我正在爲我的大學項目製作一個2D遊戲,而我在這一點上被卡住了。基本上是 。我有一個背景,我想從右向左移動(按住手機按鈕)。問題是,無論我嘗試它從上到下。我在OpenGL中創建引擎,並且擁有保存三角紋理座標的類。不知何故,我強迫背景倒退,但我無法改變方向。 這裏是源代碼(scrollBackground方法):Android - 移動我的遊戲背景horizontaly(OpenGL)
package com.example.spaceimpact_remake;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.opengl.GLSurfaceView.Renderer;
public class GameRenderer implements Renderer{
private Background background = new Background();
private Background background2 = new Background();
private float bgScroll1;
private float bgScroll2;
@Override
public void onDrawFrame(GL10 gl) {
try{
Thread.sleep(SFEngine.GAME_THREAD_FPS_SLEEP);
} catch (InterruptedException e){
e.printStackTrace();
}
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
scrollBackground1(gl);
scrollBackground2(gl);
gl.glEnable(GL10.GL_BLEND);
gl.glBlendFunc(GL10.GL_ONE, GL10.GL_ONE);
}
private void scrollBackground1(GL10 gl){
if (bgScroll1 == Float.MAX_VALUE){
bgScroll1 = 0f;
}
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glPushMatrix();
gl.glScalef(1f, 1f, 1f);
gl.glTranslatef(0f, 0f, 0f);
gl.glMatrixMode(GL10.GL_TEXTURE);
gl.glLoadIdentity();
gl.glTranslatef(0.0f, bgScroll1, -0.0f); //przewijanie tekstury
background.draw(gl);
gl.glPopMatrix();
bgScroll1 += SFEngine.SCROLL_BACKGROUND_1;
gl.glLoadIdentity();
}
private void scrollBackground2(GL10 gl){
if (bgScroll1 == Float.MAX_VALUE){
bgScroll1 = 0f;
}
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glPushMatrix();
gl.glScalef(1f, 1f, 1f);
gl.glTranslatef(0f, 0f, 0f);
gl.glMatrixMode(GL10.GL_TEXTURE);
gl.glLoadIdentity();
gl.glTranslatef(0.0f, bgScroll2, 0.0f);
background2.draw(gl);
gl.glPopMatrix();
bgScroll2 += SFEngine.SCROLL_BACKGROUND_2;
gl.glLoadIdentity();
}
@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(0f, 1f, 0f, 1f, -1f, 1f);
}
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
//włączenie w OpenGL mapowania tekstur 2D
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glClearDepthf(1.0f);
gl.glEnable(GL10.GL_DEPTH_TEST);
gl.glDepthFunc(GL10.GL_LEQUAL);
//przeźroczystość
gl.glEnable(GL10.GL_BLEND);
gl.glBlendFunc(GL10.GL_ONE, GL10.GL_ONE);
background.loadTexture(gl, SFEngine.BACKGROUND_LAYER_ONE, SFEngine.context);
background2.loadTexture(gl, SFEngine.BACKGROUND_LAYER_TWO, SFEngine.context);
}
}
謝謝。它是這樣工作的: (bgScroll1,0.0f,0.0f); =(x,y,z); bgScroll放置在第一個變量上將滾動X軸,第二個var - > Y軸。等等。 – user3213145