如何实现SharedPreferences?

时间:2013-09-11 11:13:29

标签: java android

我应该如何在updateRunning(List<TouchEvent> touchEvents, float deltaTime)中的GameScreen内实现SharedPreferences?

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的初学者,所以如果你能编写为了使它能够工作所需的实际代码将会非常有用,因为像“你应该这样做......”这样的评论并不意味着对我很重要。谢谢。

3 个答案:

答案 0 :(得分:1)

你需要将Context传递给你的GameScreen类..然后将pref定义如下

SharedPreferences settings = context.getSharedPreferences("Prefs", 0);

了解更多详情click here

答案 1 :(得分:0)

请参阅: How to use SharedPreferences in Android to store, fetch and edit values

你应该修改的唯一内容是“this”对象,在你的情况下应该是你传递给GameScreen对象的上下文(你应该将上下文传递给GameScreen构造函数),然后: mContext.getSharedPreferences(name,mode);

答案 2 :(得分:0)

由于您似乎正在使用LIBGDX,因此应使用Gdx.app.getPreferences()来获取首选项对象。这种用法将使您能够根据自己的喜好更加平台化。

http://code.google.com/p/libgdx/wiki/Preferences