简体   繁体   English

使图像在android中掉落

[英]Making images drop in android

I am making an android game which needs to display some different images. 我正在制作一个需要显示一些不同图像的android游戏。 I need to make a method that keeps on generating images from a resource in the drawable directory and then drop the images from the top of the screen to the bottom. 我需要一种方法,该方法可以继续从可绘制目录中的资源生成图像,然后将图像从屏幕顶部放到底部。 I also need that each image should have an id "image-1" "image-2" in which the index number goes higher once more images are created as an integer. 我还需要每个图像都应该有一个id“ image-1”“ image-2”,一旦将更多图像创建为整数,索引号就会更高。 I also need them to be generated at different locations at the top of the screen and drop with a specified speed. 我还需要在屏幕顶部的不同位置生成它们,并以指定的速度放下它们。 Once they hit the bottom, I want a method to be executed like: 一旦他们触底,我想要一个像这样执行的方法:

public void touchedGround() {
//My code
}

I am using this code so far: 到目前为止,我正在使用此代码:

import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;

public class GameScreenActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.gamescreen);
    generateParachuters(savedInstanceState);
}

private void generateParachuters(Bundle icicle) {
    // TODO Auto-generated method stub
    Toast.makeText(getApplicationContext(), "generating images", 10).show();
}
}

I know one way to do it, but it requires a lot more code then what you're probably looking for. 我知道一种实现方法,但是它需要比您可能正在寻找的代码更多的代码。 But hey, here goes. 但是,嘿,这去了。

  1. Create a class for your falling object, which I'm guessing is a Parachuter . 为您坠落的物体创建一个类,我猜这是一个Parachuter This class will have an X and Y variable to keep track of where it is on the screen. 此类将具有X和Y变量以跟踪其在屏幕上的位置。 You can then have a tick method, which will increase the Y variable by however fast you want it to fall. 然后,您可以使用一个tick方法,该方法将使Y变量的下降速度变快。 You can also have them keep track of a integer ID if you want. 如果需要,还可以让他们跟踪整数ID。

  2. Create a list that will hold the Parachuter objects. 创建一个将保存Parachuter对象的列表。 I use an ArrayList. 我使用一个ArrayList。 This way you create a Parachuter when needed and add it to the list. 这样,您可以在需要时创建Parachuter并将其添加到列表中。 Cycle through the list to draw each one. 循环浏览列表以绘制每个。

  3. You need something to draw the Parachuter s with. 您需要一些东西来绘制Parachuter Personally, I use Canvas from SurfaceView . 我个人使用SurfaceView Canvas

  4. You'll need a looping animation. 您将需要一个循环动画。 Loop through, each time drawing the Parachuter s and calling tick on each one. 遍历,每次绘制Parachuter并在每一个上调用tick

  5. During the loop, you can check the position of each Parachuter . 在循环过程中,您可以检查每个Parachuter的位置。 If its position goes beyond Canvas.getHeight() , remove the image from the list. 如果其位置超出Canvas.getHeight() ,则从列表中删除图像。

Well, that's quite a bit there. 好吧,那儿有很多。 Ask me if you need further clarification. 问我是否需要进一步说明。

UPDATE : Here's a fully-working example . 更新这是一个可以正常工作的示例

UPDATE 2 : 更新2

//Check right edge of screen
for (int i = 0; i < parachuters.size(); i++)
{
    if (parachuters.get(i).getX() > canvas.getWidth())
        //Do whatever it is you want
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM