问题描述
我想要得到子弹的起点,但我不能让它决赛,以及bullet.x和bullet.y总是在变化,所以我不知道如何保存每个的起始坐标子弹。它生成一个随机的地方,然后移动。我试着拯救bullet.x和bullet.y给一个变量启动时,但改变子弹移动时以及
下面是我的code: -
公共类MyGame扩展ApplicationAdapter {
SpriteBatch批次;
纹理ballImage,bulletImage;
OrthographicCamera凸轮;
圈球,子弹;
阵列<&圈GT;子弹;
//长lastShot;@覆盖
公共无效创建()
{
的System.out.println(游戏创造);
ballImage =新的纹理(Gdx.files.internal(ball.png));
bulletImage =新的纹理(Gdx.files.internal(bullet.png)); 凸轮=新OrthographicCamera();
cam.setToOrtho(真,320480); //真正开始左上出师不利左下方 一批=新SpriteBatch(); 球=新圈();
ball.radius = 20;
ball.x = 320 /双ball.radius; //半屏幕尺寸 - 图像的一半
ball.y = 480 /双ball.radius; 子弹=新的Array<&圈GT;();
spawnBullet();}公共无效spawnBullet()
{
圆形子弹=新圈();
bullet.radius = 8;
bullet.x = bullet.radius; //半屏幕尺寸 - 图像的一半
bullet.y = MathUtils.random(0,480-bullet.radius);
bullets.add(子弹);
的System.out.println(×:+ bullet.x +Y:+ bullet.y);}@覆盖
公共无效渲染()
{
Gdx.gl.glClearColor(0,0,0,1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
cam.update();
batch.setProjectionMatrix(cam.combined);
batch.begin();
batch.draw(ballImage,ball.x-ball.radius,ball.y-ball.radius);
对于(圈子弹:子弹)
{
batch.draw(bulletImage,bullet.x-bullet.radius,bullet.y-bullet.radius);
}
batch.end(); 如果(Gdx.input.isTouched())
{
POS的Vector3 =新的Vector3();
pos.set(Gdx.input.getX(),Gdx.input.getY(),0);
cam.unproject(POS)
ball.y = pos.y;
}
如果(ball.y℃,+ ball.radius)ball.y = ball.radius;
如果(ball.y> 480 ball.radius)ball.y = 480 ball.radius; 迭代器<&圈GT; I = bullets.iterator(); 而(i.hasNext())
{ 圆形子弹= i.next();
//System.out.println(\"x2:+ bullet.x +Y2:+ bullet.y);
如果(bullet.y> 240){
bullet.x ++;
bullet.y--;} bullet.x ++; //右边框的碰撞
如果(bullet.x> 320)
{
i.remove();
spawnBullet();
}
//圈碰撞
如果(ball.overlaps(子弹))
{
i.remove();
spawnBullet();
}
}
}
}
我的libgdx是有点生疏。这就是说,这看起来像一个相当普通的设计问题。从本质上讲,你MyGame类是试图做太多,这意味着解决问题中很困难的。
现在,你想只使用一个圈为你的子弹,这意味着你只能得到什么Circle类提供。你想存储额外的信息和行为添加到这个圈子,所以你应该创造的东西做的。我建议创建一个子弹类,像这样:
公共类子弹{
私人圈圈;
私人诠释startx的;
私人诠释startY; 公共子弹(INT运行startx,诠释startY){
圈= //从运行startx和startY创建圈子
this.startX = startx的;
this.startY = startY;
}
// getter和setter这里...
}
您可以选择性地具有子弹延长圈,虽然这将使更改子弹像一个矩形稍微更困难,如果你决定。
然后就可以存储更多的信息。它也可以让你移动的一些行为纳入这个类,而不是您MyGame类中做的一切。这是一件好事,因为它使code更容易理解。
I want to get the starting point of the bullet but i can't make it final, and the bullet.x and bullet.y is always changing so i'm not sure how to save the starting co-ordinates of each bullet. It spawns in a random place and then moves. I've tried saving the bullet.x and bullet.y to a variable when it starts but that changes when the bullet moves as well
Here is my code:-
public class MyGame extends ApplicationAdapter {
SpriteBatch batch;
Texture ballImage, bulletImage;
OrthographicCamera cam;
Circle ball, bullet;
Array <Circle> bullets;
//long lastShot;
@Override
public void create ()
{
System.out.println("game created");
ballImage = new Texture(Gdx.files.internal("ball.png"));
bulletImage = new Texture(Gdx.files.internal("bullet.png"));
cam = new OrthographicCamera();
cam.setToOrtho(true,320,480);//true starts top left false starts bottom left
batch = new SpriteBatch();
ball = new Circle();
ball.radius=20;
ball.x=320/2-ball.radius; // half screen size - half image
ball.y=480/2-ball.radius;
bullets = new Array<Circle>();
spawnBullet();
}
public void spawnBullet()
{
Circle bullet = new Circle();
bullet.radius=8;
bullet.x=bullet.radius; // half screen size - half image
bullet.y=MathUtils.random(0, 480-bullet.radius);
bullets.add(bullet);
System.out.println("x: "+bullet.x+" Y: "+bullet.y);
}
@Override
public void render ()
{
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
cam.update();
batch.setProjectionMatrix(cam.combined);
batch.begin();
batch.draw(ballImage,ball.x-ball.radius,ball.y-ball.radius);
for(Circle bullet: bullets)
{
batch.draw(bulletImage, bullet.x-bullet.radius, bullet.y-bullet.radius);
}
batch.end();
if(Gdx.input.isTouched())
{
Vector3 pos = new Vector3();
pos.set(Gdx.input.getX(), Gdx.input.getY(),0);
cam.unproject(pos);
ball.y = pos.y ;
}
if(ball.y<0+ball.radius)ball.y=ball.radius;
if(ball.y>480-ball.radius)ball.y=480-ball.radius;
Iterator<Circle> i = bullets.iterator();
while(i.hasNext())
{
Circle bullet = i.next();
//System.out.println("x2: "+bullet.x+" Y2: "+bullet.y);
if(bullet.y>240){
bullet.x++;
bullet.y--;}
bullet.x++;
//right border collision
if(bullet.x>320)
{
i.remove();
spawnBullet();
}
//circle collision
if(ball.overlaps(bullet))
{
i.remove();
spawnBullet();
}
}
}
}
My libgdx is a little rusty. That said, this looks like a fairly regular design problem. Essentially, your MyGame class is trying to do too much, which means solving problems within it is difficult.
Right now, you're trying to just use a circle as your bullet, which means you only get what the Circle class provides. You want to store extra information and add behavior to the circle, so you should create something to do that that. I'd suggest creating a Bullet class, something like this:
public class Bullet {
private Circle circle;
private int startX;
private int startY;
public Bullet(int startX, int startY){
circle = //create circle from startX and startY
this.startX = startX;
this.startY = startY;
}
//getters and setters here...
}
You could alternatively have Bullet extend Circle, though that would make changing your bullet to something like a rectangle slightly more difficult if you ever decide to.
Then you can store additional information. It also lets you move some behaviour into that class, instead of doing everything inside your MyGame class. This is always good, as it makes code easier to understand.
这篇关于如何让每一颗子弹的起点? Libgdx简单的游戏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!