【发布时间】:2012-02-17 21:18:28
【问题描述】:
我是 Android 开发的初学者。
我创建了一个游戏。一个位图 (1) 是可控的,另一个位图 (2) 与第一个位图发生碰撞。工作正常。问题 ;我将如何创建 (2) 的多个实例,首先,它们都以相同的方式响应与 (1) 的冲突。
到目前为止我已经构建了什么;
在 MainGamePanel 中,我创建了两个位图:
basket = new basket(BitmapFactory.decodeResource(getResources(), R.drawable.basket01), 50, 50);
apple = new apple(BitmapFactory.decodeResource(getResources(), R.drawable.apple_red01));
MainThread 执行更新并在面板上绘制画布
在更新中(在 MainGamePanel 中),我检查 (1) 和 (2) 之间的碰撞,检查坐标。
如果检测到碰撞,我在苹果 (2) 上设置坐标,它变成“开槽”。
然后,如果苹果 (2) 被开槽并触摸,我将它移动到屏幕上的随机位置并将开槽布尔值设置为 false。
...这是我卡住的地方,2 个问题(我应该把它们分开吗?)
- 如何创建位图的多个实例 (2)?
- 如何获取画布的 X 和 Y 最大值,或者不是在类本身中创建的视图?
谢谢!
位图的当前 Code-sn-ps (2) "apple" :
public void draw(Canvas canvas) {
canvas.drawBitmap(bitmap, X - width/2, Y - height/2, null);
}
关于问题二(复习随机生成器,我需要设置最大值);
if (slotted){
if (eventX >= (X - width/ 2) && (eventX <= (X + width/2))) {
if (eventY >= (Y - height/ 2) && (eventY <= (Y + height/ 2))) {
// basket touch
Random Rnd = new Random();
float nX=Rnd.nextInt(HOWTOMAXOFVIEWORCANVAS);
float nY=Rnd.nextInt(HOWTOMAXOFVIEWORCANVAS)+80;
// the +80 is to prevent the apple from returning in the 'slotted' area (the basket can't get there ;)
setX(nX);
setY(nY);
slotted = false;
【问题讨论】:
标签: android random bitmap android-canvas