实现位图游戏对象的多个实例

发布于 2025-01-06 17:27:00 字数 1459 浏览 0 评论 0原文

我是 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) 上设置坐标,它会变成“有槽”。

然后,如果 apple (2) 被插入并被触摸,我将其移动到屏幕上的随机位置并将布尔值 slotted 设置为 false。

...这就是我陷入困境的地方,有两个问题(我应该在这里将它们分开吗?)

  1. 我将如何创建位图(2)的多个实例?
  2. 如何获取画布或未在类本身内创建的视图的 X 和 Y 最大值?

谢谢!

位图 (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;

I'm a beginner at Android Development.

I've created a game. One Bitmap (1) is controllable, the other bitmap (2) acts on collision with the first. Works fine. Question ; How would I create multiple instances of (2) that would, to start with, all respond to a collision with (1) in the same way.

What I've built so far;

In the MainGamePanel, I create the two Bitmaps :

  basket = new basket(BitmapFactory.decodeResource(getResources(), R.drawable.basket01), 50, 50);
  apple = new apple(BitmapFactory.decodeResource(getResources(), R.drawable.apple_red01));

The MainThread performs update and draws the canvas on the panel

In the update (in MainGamePanel), I check for collisions between (1) and (2), checking the coordinates.

If collision detected, I set coordinates on apple (2), it becomes 'slotted'.

Then, if apple (2) is slotted AND touched, I move it to a random position on the screen and set the boolean slotted to false.

... this is where I'm stuck, 2 questions (should I split them up here on SO?)

  1. How would I create multiple instances of the bitmap (2)?
  2. How would I get the X and Y max values of the canvas, or view that is not created within the class itself?

Thanks!

Current Code-snippets for the bitmap (2) "apple" :

     public void draw(Canvas canvas) {
  canvas.drawBitmap(bitmap, X - width/2, Y - height/2, null);
 }

With regards to question two (review random generator, I need to set max values);

    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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

网名女生简单气质 2025-01-13 17:27:00

一种非常简单的方法是创建一个苹果列表,而不是单独的苹果实例:

ArrayList<apple> appleList = new ArrayList<apple>();
int nApples = 5;
for (int i=0; i<5; i++)
      appleList.add(new apple();

不想想要创建位图的多个实例。位图每个像素会消耗大量内存(4 字节)。只需创建一次位图,然后让您的苹果对象直接引用该位图,效果会好很多倍。

画布的最大 X 和 Y 值由您要绘制到的 View 的尺寸决定。一旦您的 View 已膨胀并绘制,您应该能够从视图中获取这些值。

A very simple way would be to create a list of apples instead of separate apple instances:

ArrayList<apple> appleList = new ArrayList<apple>();
int nApples = 5;
for (int i=0; i<5; i++)
      appleList.add(new apple();

You DO NOT WANT want to create multiple instances of a bitmap. Bitmaps can consume a large amount of memory 4 bytes per pixel. Its would be many times better to simply create the bitmap once and then just have your apple objects reference that bitmap directly.

The max X and Y values of the canvas are determined by the dimensions of the View you are drawing to. Once your View has been inflated and drawn you should be able to get those values from the view.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文