我想在屏幕底部做一个关于一个男孩的简单游戏,试图抓住掉落的苹果。苹果在随机x坐标处直线下降,底部的男孩可以向右或向左滚动。
到目前为止,苹果现在可以随机生成并正确动画,男孩可以正常滚动。
然而,我在游戏核心条件方面遇到困难:当苹果处于男孩的宽度时,苹果被认为是成功捕获的。
private final Queue<Button> spots = new ConcurrentLinkedQueue<Button>();
并在视图中生成新的苹果时:
spots.add(spot);
当苹果从视图中消失时:
spots.remove(spot);
我使用以下代码检查条件:
(BOY_X
是男孩的x坐标)
public void Check_all_Spot(int BOY_X)
{
int [] apple_x;
int [] apple_y;
int apple_shown_length =spots.size();
Button [] apples = null;
String text_x ="";
String text_y = "";
if (!spots.isEmpty())
{
apples = spots.toArray(new Button[apple_shown_length]);
}
apple_x = new int [apple_shown_length];
apple_y = new int [apple_shown_length];
for (int w = 0; w < apple_shown_length ; ++w)
{
apple_x[w] = (int) apples[w].getX();
apple_y[w] = (int) apples[w].getY();
text_x = text_x+ "\n" +apple_x[w];
text_y = text_y+ "\n" +apple_y[w];
if ( (apple_x[w]+SPOT_DIAMETER)/2 >= BOY_X ||
(apple_x[w]+SPOT_DIAMETER)/2 <= (BOY_X+BOY_WIDTH) ||
(apple_y[w]+SPOT_DIAMETER) >= (viewHeight - BOY_HEIGHT) ||
(apple_y[w]+SPOT_DIAMETER) <= (viewHeight - (BOY_HEIGHT)/2) )
// check text
{
}
}
Toast.makeText(getContext(), "X=" +text_x+"\nY=" +text_y, Toast.LENGTH_LONG).show();
}
通过这种将斑点队列转换为按钮阵列apple
的方式,toast报告视图中所有苹果的x和y坐标。
我的问题是,虽然现在我知道哪个苹果满足检查条件,怎么能指示到spots queue
删除苹果??
或者我应该使用游戏线程计算时间吗?
或者我不应该将ConcurrentLinkedQueue
转换为按钮数组?
我想获得你的一些建议。非常感谢!