1

How should I implement SharedPreferences inside updateRunning(List<TouchEvent> touchEvents, float deltaTime) in 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;
    }

}

Since I'm beginner in Android and Java it would be very helpful if you could write actual code that is required in order for this to work, because comments like "You should do this..." unfortunately doesn't mean a lot to me. Thank you.

Denim Datta
  • 3,740
  • 3
  • 27
  • 53
user2668638
  • 653
  • 1
  • 7
  • 9

3 Answers3

1

you need to pass Context to your GameScreen class ..then define pref as below

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

for more details click here

Community
  • 1
  • 1
Chirag Ghori
  • 4,231
  • 2
  • 20
  • 35
  • Hot to pass context to GameScreen class? In other class I have Context context = this;, but when I try to use it with SharedPreferences settings = OtherClass.context.getSharedPreferences("Prefs", 0);, I got error "non-static field 'context' cannot be referenced from a static context". It's because context is not static. If I make it static I got error "'com..OtherClass.this' cannot be referenced from a static context". – user2668638 Sep 12 '13 at 15:12
  • add context parameter to your GameScreen Constructor like --> public GameScreen(Game game,Context context); and pass Context value from where you called GameScreen... – Chirag Ghori Sep 13 '13 at 06:18
0

Please refer to: How to use SharedPreferences in Android to store, fetch and edit values

The only thing you should modify is "this" object that in your case should be a context you pass to your GameScreen object(you should pass context to GameScreen constructor) and then: mContext.getSharedPreferences(name, mode);

Community
  • 1
  • 1
Evgeni Roitburg
  • 1,958
  • 21
  • 33
0

Since you appear to be using LIBGDX, you should use Gdx.app.getPreferences() to get the preferences object. This usage will allow you to be more platform agnostic with your preferences.

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

Jeremy Scoggins
  • 954
  • 8
  • 18