我希望当用户的生命达到0时,会出现对话框。因此,任何与障碍物的碰撞都意味着生命会减少并变为0,即游戏停止时。没关系,但我怎么能打破'在视图之外,进入启动View的Activity,创建一个Dialog,它要求用户输入一个名称,并从View中获取分数,然后将其保存到文件中?
保存到文件部分我认为我可以解决,它是使用得分int打破View并创建对话我感到困惑。
这是我的GameActivity:
public class GameActivity extends Activity implements SensorEventListener
{
FileStore fs;
GameView gv;
private SensorManager sensorManager;
boolean continueMusic = true;
private final int DIALOG_TEXT_ENTRY = 1;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
continueMusic = true;
fs = new FileStore();
//full screen
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_splash);
Intent i = getIntent();
int speed = i.getIntExtra("s", 0);
gv = new GameView(this, speed);
setContentView(gv);
setVolumeControlStream(AudioManager.STREAM_MUSIC); //makes the audio controllable
sensorManager=(SensorManager)getSystemService(SENSOR_SERVICE);
sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
public void onSensorChanged(SensorEvent event)
{
if(event.sensor.getType()==Sensor.TYPE_ACCELEROMETER)
{
float x=event.values[0];
if (x < 0)
{
gv.setSpriteState(2);
}
if (x > 0)
{
gv.setSpriteState(1);
}
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy)
{
// TODO Auto-generated method stub
}
public void onPause()
{
super.onPause();
if(!continueMusic)
{
MusicManager.pause();
}
}
public void onResume()
{
super.onResume();
continueMusic = false;
MusicManager.start(this,R.raw.europa);
}
protected Dialog onCreateDialog(int id) {
switch (id) {
case DIALOG_TEXT_ENTRY:
LayoutInflater factory = LayoutInflater.from(this);
final View textEntryView = factory.inflate(R.layout.save, null);
final EditText t = (EditText) textEntryView
.findViewById(R.id.save_edit);
return new AlertDialog.Builder(GameActivity.this)
.setIcon(R.drawable.smalllogo)
.setTitle(R.string.save2)
.setView(textEntryView)
.setPositiveButton(R.string.save3,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
Player p= new Player(t.getText().toString(), gv.getScore());
ArrayList<Player> players= fs.getScores();
if(players==null)
players= new ArrayList<Player>();
players.add(p);
fs.saveScores(players);
}
})
.setNegativeButton(R.string.save4,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
}
}).create();
}
return null;
}
}
这就是GameView(修剪过,因为它很大):
public class GameView extends SurfaceView implements Runnable
{
//user stuff
private PlayerSprite player; //creates the player
private int score = 0;
//graphics
private int backgroundSpeed = 0; //sets speed the background scrolls at
private Bitmap bmp, bg, spaceships,
a1, a2, a3, a4, a5, a6,
a7, a8, a9, a10, a11, a12; //creates empty objects for all the sprites
//tech/threads
private SurfaceHolder holder; //creates the placement view
private Thread gameThread = null;
private boolean isPaused = false;
static final long FPS = 30;
private boolean running = false;
private int speed = 0;
//sound
private SoundPool boom;
boolean loaded = false;
private int kaboom;
//asteroid belts
private int numOfAsteroids = 12;
private Bitmap[] asteroids;
private ArrayList<Asteroid> activeAsteroids;
private ArrayList<Asteroid> deadAsteroids;
public GameView(Context context, int s)
{
super(context);
speed = s;
initialise();
holder = getHolder();
gameThread = new Thread(this);
holder.addCallback(new SurfaceHolder.Callback()
{
@Override
public void surfaceDestroyed(SurfaceHolder holder)
{
boolean retry = true;
running = false;
while (retry)
{
try
{
gameThread.join();
retry = false;
}
catch (InterruptedException e)
{
}
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
running = true;
gameThread.start();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format,
int width, int height) {
}
});
setFocusable(true);
}
public void initialise()
{
//load graphics
//initialises player
player = new PlayerSprite(spaceships, this, 2, 5, 500, 800, speed);
//initialises blocks
activeAsteroids = new ArrayList<Asteroid>();
deadAsteroids = new ArrayList<Asteroid>();
//initialises sounds
boom = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
boom.setOnLoadCompleteListener(new OnLoadCompleteListener()
{
@Override
public void onLoadComplete(SoundPool soundPool, int sampleId, int status)
{
loaded = true;
}
});
kaboom = boom.load(this.getContext(), R.raw.boom, 1);
}
public Bitmap getBmp()
{
return bmp;
}
public void setBmp(Bitmap bmp)
{
this.bmp = bmp;
}
public void setSpriteState(int val)
{
player.setState(val);
}
public int getScore()
{
return score;
}
public void addAsteroid(Canvas c)
{
activeAsteroids.add(new Asteroid(getAsteroid(), 1, 1, createAsteroidX(c), -200));
}
public Bitmap getAsteroid()
{
Random r = new Random();
int i = r.nextInt(12);
return asteroids[i];
}
public int createAsteroidX(Canvas c)
{
Random r = new Random();
int i = r.nextInt((c.getWidth() - a1.getWidth()));
return i;
}
@Override
public void onDraw(Canvas canvas)
{
backgroundSpeed += 10;
if (backgroundSpeed > bg.getHeight() - getHeight())
{
backgroundSpeed = 0;
}
Rect src = new Rect(0, bg.getHeight() - getHeight() - backgroundSpeed, getWidth(), bg.getHeight() - backgroundSpeed);
Rect dst = new Rect(0, 0, getWidth(), getHeight());
canvas.drawBitmap(bg, src, dst, null);
player.update(canvas);
if (activeAsteroids.size() == 0) //adds asteroids periodically
{
addAsteroid(canvas);
}
for (Asteroid a : activeAsteroids)
{
//a.move(canvas, 0, 10); //moves asteroids downward slowly
a.move(canvas, 0, speed);
if (player.collidesWith(a)) //ends game
{
boom.play(kaboom, 3, 3, 1, 0, 1f);
running = false;
Paint pp = new Paint();
pp.setTextAlign(Paint.Align.CENTER);
pp.setTextSize(75);
pp.setColor(Color.WHITE);
canvas.drawText("You crashed.", getWidth() / 2, getHeight() / 2, pp);
}
if (!a.getScored()) //adds score once when asteroid passes player
{
if ((a.getY() / 2) > (player.getY() / 2))
{
score++;
a.setScored(true);
}
}
if (a.getY() > 1200) //removes asteroid from lists when passed bottom of screen
{
a.move(canvas, 10000, 10000);
deadAsteroids.add(a);
activeAsteroids.remove(a);
}
}
for (Asteroid a : deadAsteroids) //removes asteroids in the dead list
{
deadAsteroids.remove(a);
}
Paint p = new Paint();
p.setARGB(255, 255, 255, 255);
p.setTextSize(100);
canvas.drawText("" + score, (canvas.getWidth()/2) - 40, 1100, p);
canvas.drawText("" + activeAsteroids.size(), (canvas.getWidth()/2) - 40, 100, p);
canvas.drawText("" + speed, (canvas.getWidth()/2) - 40, 600, p);
}
@Override
public void run()
{
long ticksPS = 1000 / FPS;
long startTime;
long sleepTime;
while (running) {
Canvas c = null;
startTime = System.currentTimeMillis();
try {
c = getHolder().lockCanvas();
synchronized (getHolder()) {
onDraw(c);
}
} finally {
if (c != null) {
getHolder().unlockCanvasAndPost(c);
}
}
sleepTime = ticksPS - (System.currentTimeMillis() - startTime);
try {
if (sleepTime > 0)
Thread.sleep(sleepTime);
else
Thread.sleep(10);
} catch (Exception e) {
}
}
}
}
答案 0 :(得分:0)
首先,您应该使用Open GL ES之类的东西来执行此操作。
无论如何,我会使用新视图为您的GameOver屏幕创建一个新活动。然后,您可以在使用新视图创建新活动时完成()当前活动。
您可以使用intent来实现这一点,这样可以轻松地在两个活动之间切换。
void checkLives(Context mContext)
{
If(Lives == 0)
{
Intent intent = new Intent(mContext, SaveScore.class);
((Activity)mContext).startActivity(intent);
finish();
}
}
确保你完成()活动,否则你可能会遇到多个实例。
然后,您可以从SaveScore活动开始新游戏,创建orignal活动的新实例。