2014-09-09 2 views
0

안녕하세요 저는 "가상 드럼 키트"를 만드는 앱에 문제가 있습니다. libgdx로 환상적이지는 않겠지 만 솔직하게 "무엇을 가르쳤습니까? 나는 더 이상 접촉하지 않는 누군가에 의해 "알 필요가있었습니다.libgdx 및 android를 통해 여러 프레스 추적

앱이 모두 실행되어 멋지지만 Android 기기에서 사용할 때 두 개의 드럼/심벌을 동시에 칠 수 없으며 동시에 드럼의 소리가 반복됩니다. 아마 언론을 추적하는 데 가장 좋은 방법을 사용하지는 않지만 libgdx에 익숙하지 않다고 말했듯이 내가 코드를 붙여 넣으면 사람들로부터 도움이나 조언을 얻는 것이 좋을 것입니다!

package gamebulk.drumkit; 

import java.util.Random; 

import gamebulk.drumkit.DrumKit.DialogListener; 

import com.badlogic.gdx.ApplicationListener; 
import com.badlogic.gdx.Game; 
import com.badlogic.gdx.Gdx; 
import com.badlogic.gdx.Input; 
import com.badlogic.gdx.Screen; 
import com.badlogic.gdx.graphics.GL10; 
import com.badlogic.gdx.graphics.GLCommon; 
import com.badlogic.gdx.graphics.OrthographicCamera; 
import com.badlogic.gdx.graphics.g2d.SpriteBatch; 
import com.badlogic.gdx.math.Rectangle; 
import com.badlogic.gdx.math.Vector3; 

public class DrumScreen implements Screen, ApplicationListener { 

Game game; 

OrthographicCamera guiCam; 
SpriteBatch batcher; 
Rectangle snareBounds; 
Rectangle hihatBounds; 
Rectangle crashBounds; 
Rectangle rideBounds; 
Rectangle bassBounds; 
Rectangle hitBounds; 
Rectangle lowtBounds; 
Rectangle floortBounds; 
Rectangle exitBounds; 
Vector3 touchPoint; 
DialogListener callback; 

float time = 0; 

public final Random rand; 

public DrumScreen (Game game, DialogListener callback) { 
    this.game = game; 

    guiCam = new OrthographicCamera(800, 480); 
    guiCam.position.set(800/2, 480/2, 0); 
    batcher = new SpriteBatch(); 
    snareBounds = new Rectangle (180, 150, 147, 80); 
    hihatBounds = new Rectangle (28, 184, 160, 90); 
    crashBounds = new Rectangle(36, 310, 246, 160);  
    rideBounds = new Rectangle (550, 250, 222, 140); 
    bassBounds = new Rectangle (278, 38, 250, 211); 
    hitBounds = new Rectangle (256, 242, 132, 96); 
    lowtBounds = new Rectangle (410, 240, 130, 92); 
    floortBounds = new Rectangle (494, 128, 178, 118); 
    exitBounds = new Rectangle (704, 384, 64, 64); 
    touchPoint = new Vector3(); 
    this.callback = callback; 
    Gdx.input.setCatchBackKey(true); 
    rand = new Random(); 

} 

public void update (float deltaTime) { 

    handleInput(deltaTime); 

    if (Gdx.input.justTouched()) { 
     guiCam.unproject(touchPoint.set(Gdx.input.getX(), Gdx.input.getY(), 0)); 


     if (OverlapTester.pointInRectangle(snareBounds, touchPoint.x, touchPoint.y)) { 
      if (rand.nextFloat() < 0.5){ 
       Assets.playSound(Assets.snare1sound);} 
      else{ 
       Assets.playSound(Assets.snare2sound); 
      } 
     } 
     if (OverlapTester.pointInRectangle(hihatBounds, touchPoint.x, touchPoint.y)) { 
      if (rand.nextFloat() < 0.5){ 
       Assets.playSound(Assets.hihat1sound);} 
      else{ 
       Assets.playSound(Assets.hihat2sound); 
      } 
     }    
     if (OverlapTester.pointInRectangle(crashBounds, touchPoint.x, touchPoint.y)) { 
      if (rand.nextFloat() < 0.5){ 
       Assets.playSound(Assets.crash1sound);} 
      else{ 
       Assets.playSound(Assets.crash1sound); 
      } 
     } 
     if (OverlapTester.pointInRectangle(rideBounds, touchPoint.x, touchPoint.y)) { 
      if (rand.nextFloat() < 0.5){ 
       Assets.playSound(Assets.ride1sound);} 
      else{ 
       Assets.playSound(Assets.ride2sound); 
      } 
     } 
     if (OverlapTester.pointInRectangle(bassBounds, touchPoint.x, touchPoint.y)) { 
      if (rand.nextFloat() < 0.5){ 
       Assets.playSound(Assets.bass1sound);} 
      else{ 
       Assets.playSound(Assets.bass2sound); 
      } 
     } 
     if (OverlapTester.pointInRectangle(hitBounds, touchPoint.x, touchPoint.y)) { 
      Assets.playSound(Assets.hitom1sound); 
     } 
     if (OverlapTester.pointInRectangle(lowtBounds, touchPoint.x, touchPoint.y)) { 
      Assets.playSound(Assets.lowtom1sound); 
     } 
     if (OverlapTester.pointInRectangle(floortBounds, touchPoint.x, touchPoint.y)) { 
      Assets.playSound(Assets.floortom1sound); 
     } 

     if (OverlapTester.pointInRectangle(exitBounds, touchPoint.x, touchPoint.y)) { 
      if (callback != null){ 
       callback.showOfferwall2();} 
       Gdx.app.exit(); 
     } 
    } 
} 

private void handleInput(float delta) { 

time += delta; 
if (time > 0.5) { 
if(Gdx.input.isKeyPressed(Input.Keys.BACK)) {     
callback.showOfferwall2(); 
Gdx.app.exit();} 
}  
} 

public void draw (float delta) { 
GLCommon gl = Gdx.gl; 
time += delta; 
gl.glClearColor(1, 1, 1, 1); 
gl.glClear(GL10.GL_COLOR_BUFFER_BIT); 
guiCam.update(); 
batcher.setProjectionMatrix(guiCam.combined); 

batcher.enableBlending(); 
batcher.begin(); 
batcher.draw(Assets.drumbackground, 0, 0); 

batcher.draw(Assets.exitbutton, 704, 384); 

batcher.end();  

} 

@Override 
public void render (float delta) { 
update(delta); 
draw(delta); 
} 

@Override 
public void resize (int width, int height) { 
} 

@Override 
public void show() { 
} 

@Override 
public void hide() { 
} 

@Override 
public void resume() { 
} 

@Override 
public void dispose() { 
} 

@Override 
public void pause() { 
// TODO Auto-generated method stub 

} 

@Override 
public void create() { 
// TODO Auto-generated method stub 

} 

@Override 
public void render() { 
// TODO Auto-generated method stub 

} 
} 

이 관련된 어떤 도움이 사전에 환상적인 감사 것 : 여기

내 가상 드럼을 실행하는 클래스입니다.

+0

첫 번째 결과 : 이 http://stackoverflow.com/questions/16876877/how-to-track-multiple-touch-events-in-libgdx – lxknvlk

답변

0

Libgdx 입력 getX() 메서드는 첫 번째 터치 점 (첫 번째 손가락이라고도 함)의 x 좌표를 반환합니다. justTouched() 메서드는 모든 새 터치 이벤트 (모든 손가락)에 대해 true를 반환합니다.

터치 다운 이벤트가 화면에서 나타날 때마다 현재 코드가 실행됩니다. 그러나 첫 번째 손가락 터치 포인트의 좌표 만 봅니다.

Libgdx에는 다른 포인터에 대한 justTouched() 메서드가 없습니다 (다른 포인터의 좌표를 확인하는 방법은와 getY(int)입니다). 따라서, 입력 체크에서 이벤트 기반 입력으로 전환하고 싶지 않다고 가정 할 때, 그냥 직접 체크하는 것이 좋을 것입니다.

먼저 화면에서 지원할 손가락 수를 결정하십시오. 그것의 많은 지원하므로 (40)가 새로운 클래스 변수를 추가하려고 저렴 :

boolean sawTouch[maxFingers]; 

을하고 생성자에서 초기화 :

private static final int maxFingers = 40; 

가 방금 감동을 체크하기위한 새로운 인스턴스 변수를 추가

sawTouch = new boolean[40]; 

지금, 당신은 감동되어 있는지 확인하기 위해 모든 포인터를 확인하고, 만약 그렇다면 어디에서, 다음 소리 실행

for (int i = 0; i < maxFingers; i++) { 
    if (Gdx.input.isTouched(i)) { 
     if (!sawTouch[i]) { 
     sawTouch[i] = true; // Mark this finger as "seen" for future checks 

     // Figure out where the i'th touch was 
     guiCam.unproject(touchPoint.set(Gdx.input.getX(i), Gdx.input.getY(i), 0)); 

     // XXX put all the code that does OverlapTester.calls here (or put that in a separate function and call it here) 
     } 
     // else already saw the touch .. 
    } else { 
     sawTouch[i] = false; // Finger is gone, any future touch is "new" 
    } 
} 

또는 그와 비슷한 것. 필자는이 코드를 컴파일하려고하지 않았으므로 오타가있을 수 있지만 잘하면 아이디어를 얻을 수 있습니다. 구글에

+0

안녕하세요 덕분에 응답을, 나는 몇 가지 문제가 있어요 이 구현을 얻으려고하면 부울을 배치 할 때마다 오류가 발생합니다. 토큰 "maxFingers"에 대한 구문 오류,이 토큰을 삭제하십시오. 몇 가지 다른 위치에서 시도해 보았습니다. 또한 입력을 처리하기위한 libgdx 튜토리얼을 살펴 보았지만 실제로는 이해하지 못했습니다. – user3618087

+0

'maxFingers' 선언은 코드의 "class scope"(즉,'rand'를 선언하는'public final Random rand;'줄 옆)에 있어야합니다. ('sawTouch' 선언과 동일합니다.) –