updateRunning(List<TouchEvent> touchEvents, float deltaTime)
内部にSharedPreferences を実装するにはどうすればよいGameScreen
ですか?
SharedPreferences settings = getSharedPreferences("Prefs", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("highscore", var);
editor.commit();
GameScreen.java
package com.m.robotgame;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import android.app.Activity;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.Paint;
import com.m.framework.Game;
import com.m.framework.Graphics;
import com.m.framework.Image;
import com.m.framework.Input.TouchEvent;
import com.m.framework.Screen;
public class GameScreen extends Screen {
enum GameState {
Ready, Running, Paused, GameOver
}
GameState state = GameState.Ready;
private static Robot robot;
public static int height = 0, highscore;
public int score;
private Image currentSprite, character;
private ArrayList<Tile> tilearray = new ArrayList<Tile>();
Paint paint;
public GameScreen(Game game) {
super(game);
robot = new Robot();
character = Assets.character;
paint = new Paint();
paint.setTextSize(25);
paint.setTextAlign(Paint.Align.CENTER);
paint.setAntiAlias(true);
paint.setColor(Color.WHITE);
}
@Override
public void update(float deltaTime) {
List<TouchEvent> touchEvents = game.getInput().getTouchEvents();
if (state == GameState.Ready)
updateReady(touchEvents);
if (state == GameState.Running)
updateRunning(touchEvents, deltaTime);
if (state == GameState.Paused)
updatePaused(touchEvents);
if (state == GameState.GameOver)
updateGameOver(touchEvents);
}
private void updateReady(List<TouchEvent> touchEvents) {
if (touchEvents.size() > 0)
state = GameState.Running;
}
private void updateRunning(List<TouchEvent> touchEvents, float deltaTime) {
//Here I would like to have SharedPreferences
int len = touchEvents.size();
for (int i = 0; i < len; i++) {
TouchEvent event = touchEvents.get(i);
if (event.type == TouchEvent.TOUCH_DOWN) {
if (event.x > 80) {
...
}
if (event.x < 20) {
...
}
}
if (event.type == TouchEvent.TOUCH_UP) {
...
if (inBounds(event, 220, 0, 40, 40)) {
...
}
}
}
if (robot.getCenterY() > 200) {
...
}
}
private void updatePaused(List<TouchEvent> touchEvents) {
...
}
private void updateGameOver(List<TouchEvent> touchEvents) {
...
}
private void updateTiles() {
...
}
@Override
public void paint(float deltaTime) {
...
}
private void nullify() {
...
}
private void drawReadyUI() {
...
}
private void drawRunningUI() {
...
}
private void drawPausedUI() {
...
}
private void drawGameOverUI() {
...
}
@Override
public void pause() {
if (state == GameState.Running)
state = GameState.Paused;
}
@Override
public void resume() {
if (state == GameState.Paused)
state = GameState.Running;
}
@Override
public void dispose() {
}
private void goToMenu() {
game.setScreen(new MainMenuScreen(game));
}
public static Robot getRobot() {
return robot;
}
}
私は Android と Java の初心者なので、これを機能させるために必要な実際のコードを書いていただければ非常に助かります。自分。ありがとうございました。