LibGDX onTouch() method Array and flip method

Posted by johnny-b on Game Development See other posts from Game Development or by johnny-b
Published on 2014-06-08T20:18:21Z Indexed on 2014/06/10 3:45 UTC
Read the original article Hit count: 251

Filed under:
|
|

How can I add this on my application. i want to use the onTouch() method from the implementation of the InputProcessor to kill the enemies on screen. how do i do that? do i have to do anything to the enemy class? also i am trying to add a Array of enemies and it keeps throwing exceptions or the bullet now is facing LEFT <--- again after I used the flip method in the bullet class.

All the code is below so please anyone feel free to have a look thanks. please help Thank you

M

// This is the bullet class.

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;
private Rectangle bul;

public Bullet(float x, float y) {
 velocity = new Vector2(0, 0);
 setPosition(x, y);
AssetLoader.bullet1.flip(true, false);
AssetLoader.bullet2.flip(true, false);
setSize(AssetLoader.bullet1.getWidth(), AssetLoader.bullet1.getHeight());
bul = new Rectangle();
}

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;

 bul.set(getX(), getY(), getOriginX(), getOriginY());

 Vector2 v = velocity.cpy().scl(delta);
 setPosition(getX() + v.x, getY() + v.y);
 setOriginCenter(); 
 setRotation(velocity.angle());
 }

public Rectangle getBounds() {
    return bul;
}
public Rectangle getBounds1() {
    return this.getBoundingRectangle();
}
}

// This is the class where i load all the images from

 public class AssetLoader {
 public static Texture texture;
 public static TextureRegion bg, ball1, ball2;
 public static Animation bulletAnimation, ballAnimation;
 public static Sprite bullet1, bullet2;

 public static void load() {

 texture = new Texture(Gdx.files.internal("SpriteN1.png"));
 texture.setFilter(TextureFilter.Nearest, TextureFilter.Nearest);

 bg = new TextureRegion(texture, 80, 421, 395, 30);
 bg.flip(false, true);

 ball1 = new TextureRegion(texture, 0, 321, 32, 32);
 ball1.flip(false, true);

 ball2 = new TextureRegion(texture, 32, 321, 32, 32);
 ball2.flip(false, true);

 bullet1 = new Sprite(texture, 380, 350, 45, 20);
 bullet1.flip(false, true);

 bullet2 = new Sprite(texture, 425, 350, 45, 20);
 bullet2.flip(false, true);

 TextureRegion[] balls = { ball1, ball2 };
 ballAnimation = new Animation(0.16f, balls);
 ballAnimation.setPlayMode(Animation.PlayMode.LOOP);
 }

 Sprite[] bullets = { bullet1, bullet2 };
 bulletAnimation = new Animation(0.06f, aims);
 bulletAnimation.setPlayMode(Animation.PlayMode.LOOP);
 }

 public static void dispose() {
 texture.dispose();
 }

// This is for the rendering or drawing onto the screen/canvas.

 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();

 batcher.disableBlending();


 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(), bullet.getOriginX(), bullet.getOriginY(), bullet.getWidth(), bullet.getHeight(), 1.0f, 1.0f, bullet.getRotation());

 // End SpriteBatch
 batcher.end();
 }
 }

// this is to load the image etc on the screen i guess

 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;
 }
 }

//This is the input handler class

public class InputHandler implements InputProcessor {

private Ball myBall;
private Bullet bullet;
private GameRenderer aims;

// Ask for a reference to the Soldier when InputHandler is created.
public InputHandler(Ball ball) {

    myBall = ball;

}

@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {

 return false;
}

@Override
public boolean keyDown(int keycode) {
    return false;
}

@Override
public boolean keyUp(int keycode) {
    return false;
}

@Override
public boolean keyTyped(char character) {
    return false;
}

@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
    return false;
}

@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
    return false;
}

@Override
public boolean mouseMoved(int screenX, int screenY) {
    return false;
}

@Override
public boolean scrolled(int amount) {
    return false;
}
}

i am rendering all graphics in a GameRender class and a gameworld class if you need more info please let me know

I am trying to make the array work but keep finding that when an array is initialized then the bullet fips back to the original and ends up being backwards???? and if I create an array I keep getting Exceptions throw??? Thank you for any help given.

© Game Development or respective owner

Related posts about java

Related posts about android