如何解决为什么激光线只有一条,什么时候应该是4条,为什么仅来自PlayerShip而没有来自EnemyShip?
我制作了一款名为《太空射击》的游戏。我在两艘飞船上都添加了激光,并将激光放到LinkedList中,但是当我运行游戏时,激光从玩家飞船的一侧发射。其他3个激光器在哪里?您可以在下面的照片中看到我的意思。我已经检查过两次代码,但是找不到问题所在。
class GameScreen implements Screen {
//screen
private Camera camera;
private Viewport viewport;
//graphics
private SpriteBatch batch;
private TextureAtlas textureAtlas;
private TextureRegion[] backgrounds;
private float backgroundHeight; //height of background in World units
private TextureRegion playerShipTextureRegion,playerShieldTextureRegion,enemyShipTextureRegion,enemyShieldTextureRegion,playerLaserTextureRegion,enemyLaserTextureRegion;
//timing
private float[] backgroundOffsets = {0,0};
private float backgroundMaxScrollingSpeed;
//world parameters
private final int WORLD_WIDTH = 72;
private final int WORLD_HEIGHT = 128;
//game objects
private Ship playerShip;
private Ship enemyShip;
private LinkedList<Laser>PlayerLaserList;
private LinkedList<Laser>EnemyLaserList;
GameScreen() {
camera = new OrthographicCamera();
viewport = new StretchViewport(WORLD_WIDTH,WORLD_HEIGHT,camera);
//set up the texture atlas
textureAtlas = new TextureAtlas("images.atlas");
//setting up the background
backgrounds = new TextureRegion[4];
backgrounds[0] = textureAtlas.findRegion("Starscape00");
backgrounds[1] = textureAtlas.findRegion("Starscape01");
backgrounds[2] = textureAtlas.findRegion("Starscape02");
backgrounds[3] = textureAtlas.findRegion("Starscape03");
backgroundHeight = WORLD_HEIGHT * 2;
backgroundMaxScrollingSpeed = (float) (WORLD_HEIGHT) / 4;
//initialize texture regions
playerShipTextureRegion = textureAtlas.findRegion("playerShip2_red");
enemyShipTextureRegion = textureAtlas.findRegion("enemyBlack2");
playerShieldTextureRegion = textureAtlas.findRegion("shield2");
enemyShieldTextureRegion = textureAtlas.findRegion("shield1");
enemyShieldTextureRegion.flip(false,true);
playerLaserTextureRegion= textureAtlas.findRegion("laserGreen15");
enemyLaserTextureRegion= textureAtlas.findRegion("laserRed03");
//set up game objects
playerShip = new PlayerShip(2,3,10,WORLD_WIDTH/2,WORLD_HEIGHT/4,2f,1,0.05f,playerShipTextureRegion,playerLaserTextureRegion);
enemyShip = new EnemyShip(2,WORLD_HEIGHT*4/5,0.3f,0.8f,enemyLaserTextureRegion);
PlayerLaserList = new LinkedList<>();
EnemyLaserList = new LinkedList<>();
batch = new SpriteBatch();
}
@Override
public void render(float deltaTime) {
batch.begin();
playerShip.update(deltaTime);
enemyShip.update(deltaTime);
//scrolling background
renderBackground(deltaTime);
//enemy ships
enemyShip.draw(batch);
//player ship
playerShip.draw(batch);
//lasers
//create new Laser
//player lasr
if (playerShip.canFireLaser()) {
Laser[] lasers = playerShip.fireLaser();
for (Laser laser: lasers) {
PlayerLaserList.add(laser);
}
}
//enemy lasers
if (enemyShip.canFireLaser()) {
Laser[] lasers = enemyShip.fireLaser();
for (Laser laser: lasers) {
EnemyLaserList.add(laser);
}
}
//draw the Lasers
//remove old Laser
ListIterator<Laser> iterator = PlayerLaserList.listIterator();
while(iterator.hasNext()) {
Laser laser = iterator.next();
laser.draw(batch);
laser.yPosition += laser.movementSpeed*deltaTime;
if (laser.yPosition > WORLD_HEIGHT) {
iterator.remove();
}
}
//enemy lSAEr list
iterator = EnemyLaserList.listIterator();
while(iterator.hasNext())
{
Laser laser = iterator.next();
laser.draw(batch);
laser.yPosition -= laser.movementSpeed*deltaTime;
if(laser.yPosition + laser.height < 0)
{
iterator.remove();
}
}
//explosions
batch.end();
}
package com.mygdx.game;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
class EnemyShip extends Ship
{
public EnemyShip(float movementSpeed,int shield,float width,float height,float xCentre,float yCentre,float laserWidth,float laserHeight,float laserMovementSpeed,float timeBetweenShoots,TextureRegion shipTexture,TextureRegion shieldTexture,TextureRegion laserTextureRegion) {
super(movementSpeed,shield,width,height,xCentre,yCentre,laserWidth,laserHeight,laserMovementSpeed,timeBetweenShoots,shipTexture,shieldTexture,laserTextureRegion);
}
@Override
public Laser[] fireLaser() {
Laser[] laser = new Laser[2];
laser[0] = new Laser(xPosition + width * 0.18f,yPosition - laserHeight,laserTextureRegion);
laser[1] = new Laser(xPosition + width * 0.82f,yPosition - laserHeight,laserTextureRegion);
timeSinceLastShoot = 0;
return laser;
}
@Override
public void draw(Batch batch) {
batch.draw(shipTexture,xPosition,yPosition,height);
if (shield > 0) {
batch.draw(shieldTexture,yPosition-height*0.2f,height);
}
}
package com.mygdx.game;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
public class Laser
{
float movementSpeed;
//position and dimension
float xPosition,yPosition;
float width,height;
//graphics
TextureRegion textureRegion;
public Laser(float movementSpeed,float xPosition,float yPosition,TextureRegion textureRegion) {
this.movementSpeed = movementSpeed;
this.xPosition = xPosition;
this.yPosition = yPosition+33;
this.width = width;
this.height = height;
this.textureRegion = textureRegion;
}
public void draw(Batch batch)
{
batch.draw(textureRegion,height);
}
}package com.mygdx.game;
package com.mygdx.game;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
class PlayerShip extends Ship {
public PlayerShip(float movementSpeed,TextureRegion laserTextureRegion) {
super(movementSpeed,laserTextureRegion);
}
public Laser[] fireLaser()
{
Laser[] laser = new Laser[2];
laser[0] = new Laser(xPosition + width * 0.07f,yPosition + height * 0.45f,laserTextureRegion);
laser[1] = new Laser(xPosition + width * 0.93f,laserTextureRegion);
timeSinceLastShoot = 0;
return laser;
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。