【发布时间】:2014-07-23 08:14:55
【问题描述】:
我的项目是在 Android 中创建一个翻转纸牌游戏,例如 http://partyhatmy.blogspot.kr/2012/08/angry-bird-matching-card-game.html。不同之处在于屏幕上会出现 3 x 4 总共 12 张牌,并且游戏会有计时器。因此,如果计时器到期或用户找到所有对,则新阶段开始。
我的问题是我确实知道如何使用 SurfaceView 实现这一点,但由于所有卡片都在固定位置,我认为可以使用 xml 中的布局来实现游戏,但我不知道如何。网络上有没有可用的起点资源?
第 1 版
我的代码是这样的:我只想先将一个 TextView 的剩余时间打印到屏幕上。问题是屏幕全黑(没有 runOnUiThread() 调用,活动完美地绘制了给定的布局 activity_game。
public class GameActivity extends Activity {
private TextView mTimerTextView;
private int mRemainingTime = 30;
@Override
protected void onCreate(Bundled savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_game);
mTimerTextView = (TextView)findViewById(R.id.remaining_time);
this.runOnUiThread(new Runnable() {
public void run() {
long lastSystemTime = 0;
mTimeTextView.setText(String.valueOf(mRemainingTime));
while (mRemainingTime > 0) {
if (lastSystemTime == 0) { // initial run
lastSystemTime = System.currentTimeMillis();
continue;
}
long currentTime = System.currentTimeMillis();
long elapsedTime = currentTime - lastSystemTime;
lastSystemTime = currentTime;
if (elapsedTime > 1000) {
mRemainingTime--;
mTimeTextView.setText(String.valueOf(mRemainingTime));
}
// To avoid excessive loop
try {
Thread.sleep(100);
} catch (Exception e) {
e.printStackTrace();
}
}
}
});
}
}
【问题讨论】:
标签: android layout surfaceview