我的项目是在Android中创建一个翻转卡片游戏,如http://partyhatmy.blogspot.kr/2012/08/angry-bird-matching-card-game.html。不同之处在于屏幕上总共有3 x 4个卡片,游戏将有一个计时器。因此,如果计时器到期或者用户找到所有对,则新阶段开始。
我的问题是我确实知道如何使用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();
}
}
}
});
}
}
答案 0 :(得分:0)
看到此链接,它将帮助您翻转动画
Displaying card flip animation on old android
或
使用此coode在ui线程中进行更改
MainActivity.this.runOnUiThread(new Runnable() {
public void run() {
//do ui changes here
}
});