주 활동이 있고 해당 XML 레이아웃에는 게임 객체 (내 탱크 및 10 명의 적), 내 탱크 및 총알을 제어하는 몇 개의 버튼 및 내 점수를 표시하는 TextView를 그리는 사용자 정의보기가 있습니다. 내 사용자 정의보기는 절반 화면 게임 보드 GameSurfaceView 자바 클래스입니다. 여기 내 코드의 일부는 다음과 같습니다GameSurfaceView에서 컨텍스트를 얻는 방법은 무엇입니까?
public class GameSurfaceView extends SurfaceView implements Runnable {
private static Context gContext;
public GameSurfaceView(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
holder = getHolder();
holder.addCallback(new SurfaceHolder.Callback() {
resume();
gContext = context;
}
public void resume() {
isRunning = true;
gameThread = new Thread(this);
gameThread.start();
}
public void pause() {
isRunning = false;
boolean retry = true;
while (retry) {
try {
gameThread.join();
retry = false;
} catch (InterruptedException e) {
// try again shutting down the thread
}
}
}
@Override
public void run() {
while (isRunning) {
// We need to make sure that the surface is ready
if (!holder.getSurface().isValid()) {
continue;
}
long started = System.currentTimeMillis();
// update
step();
// draw
Canvas canvas = holder.lockCanvas();
if (canvas != null) {
render(canvas);
holder.unlockCanvasAndPost(canvas);
}
//detect all possible collisions
detectCollisions();
float deltaTime = (System.currentTimeMillis() - started);
int sleepTime = (int) (FRAME_PERIOD - deltaTime);
if (sleepTime > 0) {
try {
gameThread.sleep(sleepTime);
} catch (InterruptedException e) {
}
}
while (sleepTime < 0) {
step();
sleepTime += FRAME_PERIOD;
}
}
}
//Called from MainActivity
public void dispatchKey(int tDirection) {
Toast.makeText(gContext, "Hi", Toast.LENGTH_LONG).show();
gameStarted = true;
if (tDirection == FIRE)
Fire();
else if (tDirection != tank.Direction)
turnTankDirection = tDirection;
}
private void detectCollisions() {
//Collision Detection between tank and enemy
Toast.makeText(gContext, "Collision", Toast.LENGTH_LONG).show();
}
}
내 질문 : dispatchKey에서 토스트()를 실행 올바르게하지만 토스트 detectCollisions (에) 힘 가까이를 만드는 이유는 1? 2 detectCollisions() 메서드에서 TextView를 업데이트하는 방법은 무엇입니까? 3- detectCollisions() 메서드에서 충돌이 감지되었을 때 DialogAlert를 표시하는 방법은 무엇입니까? 내 문제는 주로 gContext 변수와 관련이 있습니다. 감사합니다. .
오류 로그/스택 트레이스를 기입하십시오 추락. – ishmaelMakitla
그러나 일부 하드웨어 제한으로 인해 실제 장치에서만 프로젝트를 테스트 할 수 있습니다. – Ahmad