【发布时间】:2014-06-05 00:52:38
【问题描述】:
我是 java 和 libgdx 的菜鸟。在某人的帮助下,我得到了归位子弹。现在我正在思考如何让它旋转,以便当它绕过它或接近它时它面向球(这是主角)。子弹面向
另外我如何使它成为一个最好的数组/数组列表,这样我就可以在随机或放置的地方拥有多个子弹。我尝试了很多东西都没有用:(
感谢您的帮助。
//如果你想调用它,下面是子弹或敌人。
public class Bullet extends Sprite {
public static final float BULLET_HOMING = 6000;
public static final float BULLET_SPEED = 300;
private Vector2 velocity;
private float lifetime;
public Bullet(float x, float y) {
velocity = new Vector2(0, 0);
setPosition(x, y);
}
public void update(float delta) {
float targetX = GameWorld.getBall().getX();
float targetY = GameWorld.getBall().getY();
float dx = targetX - getX();
float dy = targetY - getY();
float distToTarget = (float) Math.sqrt(dx * dx + dy * dy);
dx /= distToTarget;
dy /= distToTarget;
dx *= BULLET_HOMING;
dy *= BULLET_HOMING;
velocity.x += dx * delta;
velocity.y += dy * delta;
float vMag = (float) Math.sqrt(velocity.x * velocity.x + velocity.y * velocity.y);
velocity.x /= vMag;
velocity.y /= vMag;
velocity.x *= BULLET_SPEED;
velocity.y *= BULLET_SPEED;
Vector2 v = velocity.cpy().scl(delta);
setPosition(getX() + v.x, getY() + v.y);
setOriginCenter();
setRotation(velocity.angle());
lifetime += delta;
setRegion(AssetLoader.bulletAnimation.getKeyFrame(lifetime));
}
}
// 这是我加载图像的地方。
public class AssetLoader {
public static Animation bulletAnimation;
public static Sprite bullet1, bullet2;
public static void load() {
texture = new Texture(Gdx.files.internal("SpriteN1.png"));
texture.setFilter(TextureFilter.Nearest, TextureFilter.Nearest);
bullet1 = new Sprite(texture, 380, 350, 45, 20);
bullet1.flip(false, true);
bullet2 = new Sprite(texture, 425, 350, 45, 20);
bullet2.flip(false, true);
Sprite[] bullets = { bullet1, bullet2 };
bulletAnimation = new Animation(0.06f, aims);
bulletAnimation.setPlayMode(Animation.PlayMode.LOOP);
}
public static void dispose() {
// We must dispose of the texture when we are finished.
texture.dispose();
}
//这是用于渲染图像等
public class GameRenderer {
private Bullet bullet;
private Ball ball;
public GameRenderer(GameWorld world) {
myWorld = world;
cam = new OrthographicCamera();
cam.setToOrtho(true, 480, 320);
batcher = new SpriteBatch();
// Attach batcher to camera
batcher.setProjectionMatrix(cam.combined);
shapeRenderer = new ShapeRenderer();
shapeRenderer.setProjectionMatrix(cam.combined);
// Call helper methods to initialize instance variables
initGameObjects();
initAssets();
}
private void initGameObjects() {
ball = GameWorld.getBall();
bullet = myWorld.getBullet();
scroller = myWorld.getScroller();
}
private void initAssets() {
ballAnimation = AssetLoader.ballAnimation;
bulletAnimation = AssetLoader.bulletAnimation;
}
public void render(float runTime) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);
batcher.begin();
// Disable transparency
// This is good for performance when drawing images that do not require
// transparency.
batcher.disableBlending();
// The ball needs transparency, so we enable that again.
batcher.enableBlending();
batcher.draw(AssetLoader.ballAnimation.getKeyFrame(runTime), ball.getX(), ball.getY(), ball.getWidth(), ball.getHeight());
batcher.draw(AssetLoader.bulletAnimation.getKeyFrame(runTime), bullet.getX(), bullet.getY());
// End SpriteBatch
batcher.end();
}
}
// 我猜这是在屏幕上加载图像等
public class GameWorld {
public static Ball ball;
private Bullet bullet;
private ScrollHandler scroller;
public GameWorld() {
ball = new Ball(480, 273, 32, 32);
bullet = new Bullet(10, 10);
scroller = new ScrollHandler(0);
}
public void update(float delta) {
ball.update(delta);
bullet.update(delta);
scroller.update(delta);
}
public static Ball getBall() {
return ball;
}
public ScrollHandler getScroller() {
return scroller;
}
public Bullet getBullet() {
return bullet;
}
}
所以这就是全部。图像通过 AssetLoader 加载,然后通过 Bullet 类加载到 GameRenderer 和 GameWorld。我猜是这样的。抱歉新手,还在学习中。
感谢您的帮助或任何建议。
ps 我在 gamedev 上发布了这个,如果它是一个重复的帖子,请告诉我,我会删除它。 请不要因为我是新手而惩罚我 谢谢
【问题讨论】:
标签: arrays animation arraylist libgdx