gpt4 book ai didi

java - 以编程方式创建可独立控制的相同 ImageView

转载 作者:行者123 更新时间:2023-12-01 09:25:00 26 4
gpt4 key购买 nike

我正在创建一个应用程序,其中气泡在屏幕上弹跳,您可以通过弹出它们来获得积分,然后它们重新生成。

除了用于控制其在屏幕上运动的速度和方向整数之外,每个气泡都完全相同。

到目前为止,我有一个名为“bubble”的 ImageView。然后我运行我创建的 randoms() 方法。

randoms() 的代码:

    public void randoms(){

ySpeedRand = new Random();
xSpeedRand = new Random();
yPolarityRand = new Random();
xPolarityRand = new Random();

ySpeed = ySpeedRand.nextInt(5) + 4;
xSpeed = xSpeedRand.nextInt(5) + 4;
yPolarity = yPolarityRand.nextInt(3) + 1;
xPolarity = xPolarityRand.nextInt(3) + 1;

if (xPolarity == 1){
xSpeed*=-1;
}
if (yPolarity == 2){
ySpeed*=-1;
}

}

然后我有一个监听器来检查气泡何时被点击,然后将其设置为不可见,重新运行 randoms() block ,然后将其设置为可见。

这是控制气泡位置的处理程序:

final Handler handler = new Handler();
Runnable run = new Runnable() {
@Override
public void run() {

bubble.setX(bubble.getX()+xSpeed);
bubble.setY(bubble.getY()+ySpeed);
handler.postDelayed(this, 5);

}
};handler.post(run);

我使用此代码来检查气泡是否仍在屏幕上:

        final Handler checkHandler = new Handler();
Runnable check = new Runnable() {
@Override
public void run() {

if (bubble.getX()>SCREEN_WIDTH-bubble.getWidth()/2){xSpeed*=-1;}
if (bubble.getX()<0){xSpeed*=-1;}
if (bubble.getY()>SCREEN_HEIGHT-bubble.getHeight()){ySpeed*=-1;}
if (bubble.getY()<bubble.getHeight()/2+barHeight+actionBorder.getHeight()){ySpeed*=-1;}

handler.postDelayed(this, 50);

}
};checkHandler.post(check);

有没有一种简单的方法可以简单地扩展这个系统,以便我可以调用方法 createbubble() ,然后让新气泡具有与旧气泡完全相同的属性,但具有新生成的随机数?

我 100% 被卡住了,找不到任何东西。非常感谢任何帮助!

最佳答案

读你的问题我有点困惑。不确定您为什么要尝试使用 ImageView 。以此为目标:

  • 创建一个能够产生可以弹出的气泡的游戏
  • 弹出时,气泡应移动到屏幕上的随机空间
  • 当气泡移出屏幕时,反转其方向。

我至少会上以下类(class):

  • 启动器,存储 main() 方法并运行游戏循环的位置
  • 窗口,其中 extends Canvas其中您将设置 JFrame
  • 控制,每次启动器完成循环时都会计算物理
  • Entity,一个抽象的java对象
  • 气泡

这是制作游戏循环的一种方法:http://www.java-gaming.org/index.php?topic=24220.0

以下是如何制作 JFrame 的示例:

import java.awt.Canvas;
import javax.swing.JFrame;

public class Window extends Canvas{
public Window(Launcher launcher){
JFrame frame = new JFrame("Title");
frame.setSize(700,500);
frame.setResizable(false);
frame.setLocationRelativeTo(null);;
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(launcher);
frame.setVisible(true);
launcher.setFrame(frame);
}
}

为了跟踪气泡,我将声明并初始化 LinkedList<>在控制类中。要创建气泡,请在 Control 中运行如下命令:

public class Control{
public LinkedList<Entity> entity = new LinkedList<Entity>();
Random rand = new Random();

public Control(){
//You will have to define these constants in the Launcher class
entity.add(
rand.nextInt(Launcher.WINDOW_WIDTH),
rand.nextInt(Launcher.WINDOW_HEIGHT)
);
}

//Called every time Launcher completes a game loop
tick(){
for(int i=0; i<entity.size(); i++){
entity.get(i).tick();
}
}
}

在实体类中:

public abstract class Entity{
protected double x;
protected double y;

public Entity(int x, int y){
this.x = x;
this.y = y;
}

public abstract void tick();
public abstract void render(Graphics g);
//Getters and setters here ...
}

在 Bubble 类中:

public class Bubble extends Entity{
public double x = 0;
public double y = 0;

public Bubble(int x, int y){
super(x,y);
}

public void tick(){
//Physics go here
}
public void render(){
//Graphics functions go here
}
}

如果其中任何一个没有意义,请告诉我,我将更详细地解释,甚至考虑到任务的简单性为您制作整个程序。

关于java - 以编程方式创建可独立控制的相同 ImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39907890/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com