-1
使用此链接 http://developer.android.com/guide/topics/data/data-storage.html#filesInternal试图使用Android内部存储来创建高分数组。
我想为应用程序游戏构建高分数组。我想在内部存储它。我刚刚开始,我得到的错误我无法弄清楚。我只是想测试以确保它确实创建了该文件。我使用Eclipse和Java代码和andEngine。请帮忙!!
我的错误是: 方法openFileOutput(字符串,整数)是未定义令牌 “写” 类型GameScene 语法错误,标识预计在此之后令牌
package com.example.game;
import java.io.FileOutputStream;
import java.util.Timer;
import org.andengine.engine.camera.Camera;
import org.andengine.engine.camera.hud.HUD;
import org.andengine.engine.handler.timer.ITimerCallback;
import org.andengine.engine.handler.timer.TimerHandler;
import org.andengine.entity.Entity;
import org.andengine.entity.IEntity;
import org.andengine.entity.modifier.DelayModifier;
import org.andengine.entity.primitive.Rectangle;
import org.andengine.entity.scene.IOnSceneTouchListener;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.scene.background.ParallaxBackground;
import org.andengine.entity.scene.background.ParallaxBackground.ParallaxEntity;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.text.Text;
import org.andengine.extension.physics.box2d.FixedStepPhysicsWorld;
import org.andengine.extension.physics.box2d.PhysicsFactory;
import org.andengine.extension.physics.box2d.PhysicsWorld;
import org.andengine.extension.physics.box2d.util.Vector2Pool;
import org.andengine.input.sensor.acceleration.AccelerationData;
import org.andengine.input.sensor.acceleration.IAccelerationListener;
import org.andengine.input.sensor.orientation.IOrientationListener;
import org.andengine.input.sensor.orientation.OrientationSensorOptions;
import org.andengine.input.touch.TouchEvent;
import org.andengine.util.color.Color;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.example.game.SceneManager.SceneType;
public class GameScene extends BaseScene implements IAccelerationListener
{
\t
public IAccelerationListener yo;
\t public static PhysicsWorld physicsWorld;
private float tiltSpeedX;
private float tiltSpeedY;
private int collisionCheck=0;
\t
\t private HUD gameHUD;
\t private Text scoreText;
\t private Text enemyLife;
\t
\t float accelerometerSpeedX;
\t
\t private int lives=3;
\t private int captainLives=3;
\t
\t private Player player;
\t private Captain captain;
\t private Crab crab;
\t private Bomb bomb;
\t private Princess princess;
\t
\t Sprite explosion;
\t Sprite heart;
\t Sprite explosion2;
\t int flag = 1;
\t int flag2 = 1;
\t int flag3 = 1;
\t
\t private void createHUD()
\t {
\t gameHUD = new HUD();
\t engine.enableAccelerationSensor(activity, this);
\t // CREATE SCORE TEXT
\t scoreText = new Text(20, 420, resourcesManager.font, "Lives:", vbom);
\t scoreText.setSkewCenter(0, 0);
\t scoreText.setText("Lives: "+ lives);
\t gameHUD.attachChild(scoreText);
\t
\t enemyLife = new Text(600, 20, resourcesManager.font, "Enemy:", vbom);
\t enemyLife.setSkewCenter(0, 0);
\t enemyLife.setText("Score: "+ captainLives);
\t gameHUD.attachChild(enemyLife);
\t
\t
\t camera.setHUD(gameHUD);
\t }
\t
\t
\t private void subtractFromLives()
\t {
\t lives = lives -1;
\t scoreText.setText("Lives: "+ lives);
\t if (lives ==0)
\t {
\t \t SceneManager.getInstance().loadLossScene(engine);
\t \t int finalScore = captainLives; //saves score when you run out of lives to be used in the comparator for the high scores.
\t }
\t }
\t private void subtractFromCaptainLives()
\t {
\t captainLives = captainLives +1;
\t enemyLife.setText("Score: "+captainLives);
\t if (captainLives ==0)
\t {
\t \t SceneManager.getInstance().loadWinScene(engine);
\t \t
\t }
\t }
\t
\t private void createPhysics()
\t {
\t physicsWorld = new FixedStepPhysicsWorld(20, new Vector2(0, -17), false);
\t registerUpdateHandler(physicsWorld);
\t }
\t
\t @Override
\t public void createScene()
\t {
\t \t resourcesManager.gMusic.play();
\t \t
\t \t //captainLives=3;
\t \t lives = resourcesManager.number_lives;
\t \t captainLives = resourcesManager.enemy_lives;
\t createBackground();
\t createHUD();
\t createPhysics();
\t createPlayer();
\t createCaptain();
\t createAETimeHandler(2);
\t \t
\t }
\t
\t private void createPlayer()
\t {
\t \t player = new Player(400,300, vbom,camera, physicsWorld)
\t \t {
\t \t \t @Override
\t \t \t public void onDie() {
\t \t \t \t // TODO Auto-generated method stub
\t \t \t \t
\t \t \t }
\t \t \t
\t \t };
\t \t final long[] PLAYER_ANIMATE = new long[] { 100, 100, 100 };
\t \t player.animate(PLAYER_ANIMATE);
\t \t this.attachChild(player);
\t }
\t
\t private void createCaptain()
\t {
\t \t captain = new Captain(480,150, vbom,camera, physicsWorld)
\t \t {
\t \t \t @Override
\t \t \t public void onDie() {
\t \t \t \t // TODO Auto-generated method stub
\t \t \t \t
\t \t \t }
\t \t \t
\t \t \t
\t \t \t
\t \t \t
\t \t };
\t \t final long[] PLAYER_ANIMATE = new long[] { 100, 100, 100 };
\t \t captain.animate(PLAYER_ANIMATE);
\t \t this.attachChild(captain);
\t }
\t
\t private void createCrab()
\t {
\t \t int xpos = (int)(695*Math.random());
\t \t crab = new Crab(xpos,0, vbom,camera, physicsWorld)
\t \t {
\t \t \t @Override
\t \t \t protected void onManagedUpdate(float pSecondsElapsed)
\t \t \t {
\t \t \t \t if (player.collidesWith(this))
\t \t \t \t {
\t \t \t \t \t subtractFromLives();
\t \t \t \t \t float x = player.getX();
\t \t \t \t \t float y = player.getY();
\t \t \t \t \t crab.detachSelf();
\t \t \t \t \t briefExplosion(x,y);
\t \t \t \t \t collisionCheck=1;
\t \t \t \t \t
\t \t \t \t }
\t \t \t }
\t \t \t @Override
\t \t \t public void onDie() {
\t \t \t \t // TODO Auto-generated method stub
\t \t \t \t if(collisionCheck==0)
\t \t \t \t {
\t \t \t \t \t crab.detachSelf();
\t \t \t \t }
\t \t \t \t
\t \t \t }
\t \t \t
\t \t };
\t \t this.attachChild(crab);
\t \t collisionCheck=0;
\t \t
\t }
\t GameScene scene = this;
\t
\t private void briefExplosion(float x, float y)
\t {
\t \t explosion = new Sprite(x, y, resourcesManager.explosion_region, vbom);
\t \t scene.attachChild(explosion);
\t \t flag = 0;
\t \t
\t }
\t private void briefExplosion2(float x, float y)
\t {
\t \t explosion2 = new Sprite(x, y, resourcesManager.explosion2_region, vbom);
\t \t scene.attachChild(explosion2);
\t \t flag3 = 0;
\t \t
\t }
\t
\t private void briefHeart(float x, float y)
\t {
\t \t heart = new Sprite(x, y, resourcesManager.heart_region, vbom);
\t \t scene.attachChild(heart);
\t \t flag2 = 0;
\t \t
\t }
\t
\t private void createBomb()
\t {
\t \t int xpos = (int)(750*Math.random());
\t \t bomb = new Bomb(xpos,0, vbom,camera, physicsWorld)
\t \t {
\t \t \t @Override
\t \t \t protected void onManagedUpdate(float pSecondsElapsed)
\t \t \t {
\t \t \t \t if (player.collidesWith(this))
\t \t \t \t {
\t \t \t \t \t subtractFromCaptainLives();
\t \t \t \t \t float x = captain.getX();
\t \t \t \t \t float y = captain.getY();
\t \t \t \t \t bomb.detachSelf();
\t \t \t \t \t briefExplosion2(x,y);
\t \t \t \t \t collisionCheck=1;
\t \t \t \t }
\t \t \t }
\t \t \t @Override
\t \t \t public void onDie() {
\t \t \t \t if(collisionCheck==0)
\t \t \t \t {
\t \t \t \t \t subtractFromLives();
\t \t \t \t \t float x = (float) (Math.random()*800);
\t \t \t \t \t float y = 425;
\t \t \t \t \t bomb.detachSelf();
\t \t \t \t \t briefExplosion2(x,y);
\t \t \t \t }
\t \t \t \t
\t \t \t }
\t \t \t
\t \t };
\t \t this.attachChild(bomb);
\t \t collisionCheck=0;
\t \t
\t }
\t
\t private void createPrincess()
\t {
\t \t int xpos = (int)(730*Math.random());
\t \t princess = new Princess(xpos,0, vbom,camera, physicsWorld)
\t \t {
\t \t \t @Override
\t \t \t protected void onManagedUpdate(float pSecondsElapsed)
\t \t \t {
\t \t \t \t if (player.collidesWith(this))
\t \t \t \t {
\t \t \t \t \t float x = player.getX();
\t \t \t \t \t float y = player.getY();
\t \t \t \t \t princess.detachSelf();
\t \t \t \t \t briefHeart(x,y);
\t \t \t \t \t collisionCheck=1;
\t \t \t \t }
\t \t \t }
\t \t \t @Override
\t \t \t public void onDie() {
\t \t \t \t if(collisionCheck==0)
\t \t \t \t {
\t \t \t \t \t subtractFromLives();
\t \t \t \t \t princess.detachSelf();
\t \t \t \t }
\t \t \t \t
\t \t \t }
\t \t \t
\t \t };
\t \t this.attachChild(princess);
\t \t collisionCheck=0;
\t }
\t
private void createBackground()
{
\t //setBackground(new Background(Color.BLUE));
ParallaxBackground background = new ParallaxBackground(0, 0, 0);
background.attachParallaxEntity(new ParallaxEntity(0, new Sprite(0, 0, resourcesManager.game_background_region, vbom)));
setBackground(background);
}
@Override
public void onBackKeyPressed()
{
\t SceneManager.getInstance().loadMenuScene(engine);
}
@Override
public SceneType getSceneType()
{
return SceneType.SCENE_GAME;
}
@Override
public void disposeScene()
{
camera.setHUD(null);
camera.setCenter(400, 240);
resourcesManager.gMusic.stop();
// TODO code responsible for disposing scene
// removing all game scene objects.
}
\t @Override
\t public void onAccelerationAccuracyChanged(AccelerationData pAccelerationData)
\t {
\t \t
\t }
\t @Override
\t public void onAccelerationChanged(AccelerationData pAccelerationData) {
\t \t // TODO Auto-generated method stub
\t \t player.setPosition(player.getX() + pAccelerationData.getX()*2,300);
\t \t
\t if(player.getX() < 0)
\t {
\t \t player.setPosition(0,300);
\t \t
\t }
\t if(player.getX() > 550)
\t {
\t \t player.setPosition(550,300);
\t \t
\t }
\t }
\t private boolean firstTouch = false;
\t
\t private void createAETimeHandler(float mEffectSpawnDelay)
{
TimerHandler spriteTimerHandler2;
\t \t this.engine.registerUpdateHandler(spriteTimerHandler2 = new TimerHandler(mEffectSpawnDelay,true, new ITimerCallback()
{
@Override
public void onTimePassed(final TimerHandler pTimerHandler)
{
\t int i = (int)(Math.random()*10);
\t if(flag == 0)
\t {
\t explosion.detachSelf();
\t flag = 1;
\t }
\t if (flag2 == 0)
\t {
\t \t heart.detachSelf();
\t \t flag2 = 1;
\t }
\t if (flag3 == 0)
\t {
\t \t explosion2.detachSelf();
\t \t flag3 = 1;
\t }
\t if(i < 3)
\t {
\t \t createPrincess();
\t }
\t else if(i>3 && i<7)
\t {
\t \t createBomb();
\t }
\t else
\t {
\t \t createCrab();
\t }
\t }
\t }));
\t }
\t String FILENAME = "High_Scores";
\t String string = "Arrggg";
\t FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
\t fos.write(string.getBytes());
\t fos.close();
}