[英]LibGDX: how do I multiply vector with scalar without modifing original vector? (simple gravity system in Java game)
[英]LibGdx How do i create multiply bullets?
我想在觸摸jetActor時顯示並發射子彈,它可以是無限的子彈。我認為可以以某種方式使用數組來完成,但是我嘗試的次數越多,我的理解就越少。子彈走得更快。
這就是代碼,現在所要做的就是顯示噴氣機和子彈,當觸摸噴氣機時會“發射”子彈。
public class MyGdxGame implements ApplicationListener{
private Texture bulletTexture;
private Texture jetTexture;
private Stage stage;
private BulletActor[] bulletActor;
private JetActor jetActor;
float bulletX = 650, bulletY = 200;
float jetX = 700,jetY = 150;
boolean started;
public class JetActor extends Actor{
public JetActor() {
setBounds(jetX,jetY,jetTexture.getWidth(),jetTexture.getHeight());
this.addListener(new InputListener(){
public boolean touchDown(InputEvent event, float x, float y, int pointer, int buttons){
started = true;
bulletActor[2] = new BulletActor();
System.out.println("Touched");
return true;
}
});
}
@Override
public void draw(Batch batch, float parentAlpha) {
batch.draw(jetTexture, jetX, jetY, jetTexture.getWidth(), jetTexture.getHeight());
}
}
public class BulletActor extends Actor{
@Override
public void act(float delta) {
if(started){
bulletX -=3;
}
}
@Override
public void draw(Batch batch, float parentAlpha) {
batch.draw(bulletTexture,bulletX,bulletY,bulletTexture.getWidth(), bulletTexture.getHeight());
}
}
@Override
public void create() {
bulletTexture = new Texture("C:\\Users\\User\\Documents\\LibGdxMainProjects\\SampleGame1\\android\\assets\\bullet.png");
jetTexture = new Texture("C:\\Users\\User\\Documents\\LibGdxMainProjects\\SampleGame1\\android\\assets\\jet.png");
stage = new Stage();
jetActor = new JetActor();
bulletActor = new BulletActor[10];
stage.addActor(jetActor);
Gdx.input.setInputProcessor(stage);
bulletActor[1] = new BulletActor();
stage.addActor(bulletActor[1]);
}
@Override
public void dispose() {
}
@Override
public void render() {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.act(Gdx.graphics.getDeltaTime());
stage.draw();
}
@Override
public void resize ( int width, int height){
}
@Override
public void pause () {
}
@Override
public void resume () {
}
}
這將增加無限的子彈。 它的問題在於,從0到無限都需要花費很長時間。 所以最好不要有無限的子彈...
List<BulletActor> bulletList = new ArrayList<BulletActor>();
for (int i = 0; i < unlimited; i++)
{
bulletList.add(new bullet();
}
當您要繪制這些項目符號時,只需循環列表並更新每個項目符號。
for (Bullet bullet : bulletList)
{
bullet.act();
bullet.draw();
}
但是由於這些項目符號是演員,您最好將它們添加到舞台上,以便舞台為您處理act()
和draw()
。
//If you want to store them you still need a list or other datastructure to hold them.
List<BulletActor> bulletList = new ArrayList<BulletActor>();
for (int i = 0; i < unlimited; i++)
{
bulletList.add(new bullet();
stage.addActor(new BulletActor); // <-- Just adding them to the stage takes care of the act and draw methods.
}
但是您的代碼還有很多錯誤。 在繼續之前,最好學習一些基本的Java。
bulletActor = new BulletActor[10];
不會為您創建10個項目符號。 它創建了一個數組,該數組可能為您容納10個子彈。 聲明字段時,您可以看到它是一個與您為其創建對象的大小完全相同的框。
BulletActor myBullet // <-- Empty box (Null) where 1 BulletActor can be stored.
new BulletActor() // <-- A Bullet actor object
BulletActor myBullet = new BulletActor() // <-- Box with a BulletActor in it
BulletActor[] bulletArray = new BulletActor[10] // <-- 10 empty boxes
//Now store bullets in these boxes
for (int i = 0; i < bulletArray.length; i++)
{
bulletArray[i] = new BulletActor();
}
這只是您應該了解的基礎知識的一小部分。 當前,您所咬的是更多的東西,然后您可以咀嚼,所以請給自己一個真正的好處,並學習有關面向對象編程的所有基礎知識。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.