Libgdx/ Java cameras for multiple players? - java

I am trying to make a little multiplayer rpg game.
It all worked fine, until I implemented cameras for each player.
Now I got the problem, that if one player joines, he can't walk alone. It seems that he is stuck on the Client players cam. I have created a camera for each of them. Did I miss something?
Here's my "Main" class
public class LauncherScreen implements Screen{
//-----------------------------------------------------------
//-----------------idle Animation----------------------------
//-----------------------------------------------------------
Texture texture;
AnimatedSprite animationForMultiplayer;
SpriteBatch spriteBatch;
Player mySelf;
OrthographicCamera playerCam;
OrthographicCamera mpPlayerCam;
static Client client = new Client();
Launcher launcher = new Launcher();
#Override
public void render(float delta) {
// TODO Auto-generated method stub
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
launcher.update();
for(MPPlayer mpPlayer : launcher.getPlayersValue()){
//System.out.println("mpPlayerXandY : "+mpPlayer.state);
animationForMultiplayer.setState(mpPlayer.state);
animationForMultiplayer.createAnimation();
mpPlayerCam.update();
spriteBatch.setProjectionMatrix(mpPlayerCam.combined);
spriteBatch.begin();
spriteBatch.draw(animationForMultiplayer.convertAnimationTOframes(), mpPlayer.x, mpPlayer.y,Gdx.graphics.getWidth()/25,Gdx.graphics.getHeight()/15); // #6
spriteBatch.end();
mpPlayerCam.position.set(mpPlayer.x,mpPlayer.y,0);
System.out.println("mpPlayer : "+mpPlayer.x+" "+mpPlayer.y);
}
mySelf.update();
mySelf.draw(launcher.getPlayerX(), launcher.getPlayerY(), playerCam);
//System.out.println(Gdx.graphics.getFramesPerSecond());
System.out.println("player : "+launcher.getPlayerX()+" "+launcher.getPlayerY());
}
#Override
public void pause() {
// TODO Auto-generated method stub
//super.pause();
}
#Override
public void resume() {
// TODO Auto-generated method stub
//super.resume();
}
#Override
public void dispose() {
// TODO Auto-generated method stub
//super.dispose();
}
#Override
public void show() {
// TODO Auto-generated method stub
texture = new Texture(Gdx.files.internal("EnemyAnimations/BugIdleStand.png"));
animationForMultiplayer = new AnimatedSprite();
spriteBatch = new SpriteBatch();
mySelf = new Player();
mySelf.doSetup();
playerCam = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
playerCam.setToOrtho(false);
playerCam.position.set(mySelf.getX(), mySelf.getY(), 0);
mpPlayerCam = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
mpPlayerCam.setToOrtho(false);
mpPlayerCam.position.set(0, 0, 0);
}
#Override
public void resize(int width, int height) {
// TODO Auto-generated method stub
}
#Override
public void hide() {
// TODO Auto-generated method stub
}
}
And here's the "Main" player update
public void draw(float f, float g, OrthographicCamera camera){
position.x = f;
position.y = g;
//System.out.println("In beforeSetState : "+currentState);
animatedSprite.setState(state);
//System.out.println("In after : "+currentState);
animatedSprite.createAnimation();
camera.position.set(f,g,0);
camera.update();
batch.setProjectionMatrix(camera.combined);
batch.begin();
batch.draw(animatedSprite.convertAnimationTOframes(),f,g, Gdx.graphics.getWidth()/25,Gdx.graphics.getHeight()/15); // #17
batch.end();
//batch.setProjectionMatrix(null);
//System.out.println(currentState);
}

Found a solution for it, didnt knew that it was so easy ^^ heres my updated code
public class LauncherScreen implements Screen{
//-----------------------------------------------------------
//-----------------idle Animation----------------------------
//-----------------------------------------------------------
Texture texture;
AnimatedSprite animationForMultiplayer;
SpriteBatch spriteBatch;
Player mySelf;
OrthographicCamera playerCam;
OrthographicCamera mpPlayerCam;
OrthographicCamera camera;
ShapeRenderer shapeRenderer;
static Client client = new Client();
Launcher launcher = new Launcher();
int[][] map = {{1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1}};
#Override
public void render(float delta) {
// TODO Auto-generated method stub
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
launcher.update();
for(int i = 0; i < map.length; i++){
for(int j = 0; j < map[0].length; j++){
if(map[i][j] == 1){
shapeRenderer.setProjectionMatrix(camera.combined);
shapeRenderer.begin(ShapeType.Line);
shapeRenderer.setColor(0, 0, 0, 0);
shapeRenderer.rect(i*50, j*50, 50, 50);
shapeRenderer.end();
}
}
}
for(MPPlayer mpPlayer : launcher.getPlayersValue()){
//System.out.println("mpPlayerXandY : "+mpPlayer.state);
animationForMultiplayer.setState(mpPlayer.state);
animationForMultiplayer.createAnimation();
//mpPlayerCam.update();
//spriteBatch.setProjectionMatrix(mpPlayerCam.combined);
camera.position.set(mpPlayer.x,mpPlayer.y,0);
spriteBatch.setProjectionMatrix(camera.combined);
spriteBatch.begin();
spriteBatch.draw(animationForMultiplayer.convertAnimationTOframes(), mpPlayer.x, mpPlayer.y,Gdx.graphics.getWidth()/25,Gdx.graphics.getHeight()/15); // #6
spriteBatch.end();
//mpPlayerCam.position.set(mpPlayer.x,mpPlayer.y,0);
System.out.println("mpPlayer : "+mpPlayer.x+" "+mpPlayer.y);
}
mySelf.update();
mySelf.draw(launcher.getPlayerX(), launcher.getPlayerY(), camera);
//System.out.println(Gdx.graphics.getFramesPerSecond());
camera.update();
System.out.println("player : "+launcher.getPlayerX()+" "+launcher.getPlayerY());
}
#Override
public void pause() {
// TODO Auto-generated method stub
//super.pause();
}
#Override
public void resume() {
// TODO Auto-generated method stub
//super.resume();
}
#Override
public void dispose() {
// TODO Auto-generated method stub
//super.dispose();
}
#Override
public void show() {
// TODO Auto-generated method stub
texture = new Texture(Gdx.files.internal("EnemyAnimations/BugIdleStand.png"));
animationForMultiplayer = new AnimatedSprite();
spriteBatch = new SpriteBatch();
mySelf = new Player();
mySelf.doSetup();
playerCam = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
playerCam.setToOrtho(false);
playerCam.position.set(mySelf.getX(), mySelf.getY(), 0);
mpPlayerCam = new OrthographicCamera(0,0);
mpPlayerCam.setToOrtho(false);
camera = new OrthographicCamera(0, 0);
camera.setToOrtho(false);
shapeRenderer = new ShapeRenderer();
}
#Override
public void resize(int width, int height) {
// TODO Auto-generated method stub
}
#Override
public void hide() {
// TODO Auto-generated method stub
}
}

Related

Libgdx/java Performance crash with box2d light

Background Info
Hey guys i am currently working on a little rpg :) and today i tried to implement some easy light ....
Main Question
When i use Point light in my Project the fps will get slower and slower ... i have got a very good pc, so it cant be my gpu or cpu ... So what did i miss?
Heres an Screenshot
You can see in the left bottom corner the fps : 6.
By the way, i disabled vsync and my gpu is an gtx 960 ... so i dont really know why i have so low fps ...
Player class :
package Mobs;
public class Player {
AnimatedSprite animatedSprite;
SpriteBatch batch;
Light licht = new Light(Color.WHITE,500);
public int state = 0;
public int netState = 1;
float speed = 2f;
public Vector2 position = new Vector2(256,256);
public Vector2 networkPosition = new Vector2(0,0);
public Player(){
}
public void update(){
state = 0;
if(Gdx.input.isKeyPressed(Keys.A)){
position.x -= Gdx.graphics.getDeltaTime() * 100f;
state = 1;
//System.out.println(currentState);
}
if(Gdx.input.isKeyPressed(Keys.D)){
position.x += Gdx.graphics.getDeltaTime() * 100f;
state = 2;
//System.out.println(currentState);
}
if(Gdx.input.isKeyPressed(Keys.W)){
position.y += Gdx.graphics.getDeltaTime() * 100f;
state = 3;
//System.out.println(currentState);
}
if(Gdx.input.isKeyPressed(Keys.S)){
position.y -= Gdx.graphics.getDeltaTime() * 100f;
state = 4;
//System.out.println(currentState);
}
}
public void setX(float x){
position.x = x;
}
public void setY(float y){
position.y = y;
}
public void draw(float f, float g, OrthographicCamera camera){
position.x = f;
position.y = g;
//System.out.println("In beforeSetState : "+currentState);
animatedSprite.setState(state);
//System.out.println("In after : "+currentState);
animatedSprite.createAnimation();
camera.position.set(f,g,0);
camera.update();
licht.drawLight(camera, f+25 , g+25);
batch.setProjectionMatrix(camera.combined);
batch.begin();
batch.draw(animatedSprite.convertAnimationTOframes(),f,g, Gdx.graphics.getWidth()/25,Gdx.graphics.getHeight()/15);
batch.end();
}
public void doSetup(){
batch = new SpriteBatch();
animatedSprite = new AnimatedSprite();
}
public float getX(){
return position.x;
}
public float getY(){
return position.y;
}
}
And my "Light" class :
package Screen;
public class Light {
World world;
RayHandler rayHandler;
PointLight pointLight;
Body player;
Box2DDebugRenderer debugRenderer;
public Light(Color farbe, int radius) {
world = new World(new Vector2(0,0),false);
rayHandler = new RayHandler(world);
pointLight = new PointLight(rayHandler, 500 , farbe , radius, 0, 0);
pointLight.setSoftnessLength(0f);
debugRenderer = new Box2DDebugRenderer();
}
public void drawLight(OrthographicCamera playerCam, float x, float y){
world.step(1 / 60f, 8, 3);
debugRenderer.render(world, playerCam.combined);
pointLight.setPosition(x,y);
rayHandler.setCombinedMatrix(playerCam.combined);
rayHandler.updateAndRender();
}
public void removeLights(){
rayHandler.removeAll();
pointLight.remove();
}
}
Because i still got laggs, heres my MainClass :
public class LauncherScreen implements Screen{
//-----------------------------------------------------------
//-----------------idle Animation----------------------------
//-----------------------------------------------------------
Map duengon;
AnimatedSprite animationForMultiplayer;
SpriteBatch spriteBatch;
Player mySelf;
OrthographicCamera mpPlayerCam;
OrthographicCamera camera;
static Client client = new Client();
Launcher launcher = new Launcher();
int[][] map = {{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}};
#Override
public void render(float delta) {
// TODO Auto-generated method stub
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
launcher.update();
//------------------------------------------------------------------------
//------------------------Draws the Map-----------------------------------
//------------------------------------------------------------------------
duengon.ubdate(map, camera);
//------------------------------------------------------------------------
//------------------------Draws the Players-------------------------------
//------------------------------------------------------------------------
for(MPPlayer mpPlayer : launcher.getPlayersValue()){
animationForMultiplayer.setState(mpPlayer.state);
animationForMultiplayer.createAnimation();
camera.position.set(mpPlayer.x,mpPlayer.y,0);
spriteBatch.setProjectionMatrix(camera.combined);
spriteBatch.begin();
spriteBatch.draw(animationForMultiplayer.convertAnimationTOframes(), mpPlayer.x, mpPlayer.y,Gdx.graphics.getWidth()/25,Gdx.graphics.getHeight()/15); // #6
spriteBatch.end();
System.out.println("mpPlayer : "+mpPlayer.x+" "+mpPlayer.y);
}
mySelf.update();
mySelf.draw(launcher.getPlayerX(), launcher.getPlayerY(), camera);
camera.update();
System.out.println(Gdx.graphics.getFramesPerSecond());
System.out.println("player : "+launcher.getPlayerX()+" "+launcher.getPlayerY());
}
#Override
public void show() {
// TODO Auto-generated method stub
animationForMultiplayer = new AnimatedSprite();
spriteBatch = new SpriteBatch();
mySelf = new Player();
mySelf.doSetup();
mpPlayerCam = new OrthographicCamera(0,0);
mpPlayerCam.setToOrtho(false);
camera = new OrthographicCamera(0, 0);
camera.setToOrtho(false);
duengon = new Map();
}
#Override
public void resize(int width, int height) {
// TODO Auto-generated method stub
}
#Override
public void hide() {
// TODO Auto-generated method stub
}
#Override
public void pause() {
// TODO Auto-generated method stub
}
#Override
public void resume() {
// TODO Auto-generated method stub
}
#Override
public void dispose() {
// TODO Auto-generated method stub
spriteBatch.dispose();
mySelf.doDispose();
animationForMultiplayer.doDispose();
duengon.doDispose();
}
}
The .doDispose() in my mainClass are methods which disposes the resource from the classes is use
Thanks for your help and your time :)
You are not disposing anything, you might want to look into the dispose() function of LibGDX
Here is the link
https://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/utils/Disposable.html
if thats not the problem please let us know.

LibGDX - Trying to animate a sprite, crashes my whole game

I'm working on an Android game and so far the way I draw game objects is I initialise them in Game then put them in an array list of GameObject (every object extends this abstract class; player, flag, coin). This array list gets passed into Renderer which then draws the objects using a for loop (that iterates over the array list).
I am trying to add a new GameObject called Coin. Unlike other objects I want this one to be animated, and already have 8 pictures representing each frame of the animation. Here's my code (using the libgdx Animation class):
public class Coin extends GameObject implements Screen {
private SpriteBatch batch;
private Animation animation;
private float time;
public Coin(Sprite spr, float xPos, float yPos,
float radius) {
super(spr, xPos, yPos, radius);
setxPos(xPos);
setyPos(yPos);
batch = new SpriteBatch();
time = 0;
}
public void render(float delta) {
// TODO Auto-generated method stub
batch.begin();
batch.draw(animation.getKeyFrame(time += delta), getxPos(), getyPos());
batch.end();
}
#Override
public void resize(int width, int height) {
// TODO Auto-generated method stub
}
#Override
public void show() {
// TODO Auto-generated method stub
//batch = new SpriteBatch();
animation = new Animation(1 / 3f,
new TextureRegion(new Texture("coin1.png")),
new TextureRegion(new Texture("coin2.png")),
new TextureRegion(new Texture("coin3.png")),
new TextureRegion(new Texture("coin4.png")),
new TextureRegion(new Texture("coin5.png")),
new TextureRegion(new Texture("coin6.png")),
new TextureRegion(new Texture("coin7.png")),
new TextureRegion(new Texture("coin8.png")));
animation.setPlayMode(Animation.PlayMode.LOOP);
}
#Override
public void hide() {
// TODO Auto-generated method stub
}
#Override
public void pause() {
// TODO Auto-generated method stub
}
#Override
public void resume() {
// TODO Auto-generated method stub
}
#Override
public void dispose() {
// TODO Auto-generated method stub
batch.dispose();
}
}
The error I get from the LogCAT is a NullPointerException # batch.draw(animation.getKeyFrame(time += delta), getxPos(), getyPos());
Does anyone know how to fix this? Any insight would be highly appreciated
Write this code:
public Coin(Sprite spr, float xPos, float yPos,
float radius) {
super(spr, xPos, yPos, radius);
setxPos(xPos);
setyPos(yPos);
batch = new SpriteBatch();
time = 0;
loadAnimation();
}
public void loadAnimation() {
// TODO Auto-generated method stub
//batch = new SpriteBatch();
animation = new Animation(1 / 3f,
new TextureRegion(new Texture("coin1.png")),
new TextureRegion(new Texture("coin2.png")),
new TextureRegion(new Texture("coin3.png")),
new TextureRegion(new Texture("coin4.png")),
new TextureRegion(new Texture("coin5.png")),
new TextureRegion(new Texture("coin6.png")),
new TextureRegion(new Texture("coin7.png")),
new TextureRegion(new Texture("coin8.png")));
animation.setPlayMode(Animation.PlayMode.LOOP);
}

How to resize a sprite in Libgdx?

I have a problem with the method sprite.setSize(float x, float y) in Libgdx. It does not affect the size or the dimensions of the sprite. They remains fixed whatever I pass to the setSize() method.
here is my code:
public class GameScreen implements Screen {
OrthographicCamera camera;
SpriteBatch batch;
Texture carTexture;
Sprite carSprite;
public GameScreen()
{
}
#Override
public void render(float delta) {
// TODO Auto-generated method stub
Gdx.gl.glClearColor(0,0,0,0);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
batch.begin();
carSprite.setSize(16, 32);
batch.draw(carSprite, 0 , 0);
batch.end();
camera.update();
}
#Override
public void resize(int width, int height) {
// TODO Auto-generated method stub
camera.viewportWidth=width;
camera.viewportHeight=height;
camera.update();
}
#Override
public void show() {
// TODO Auto-generated method stub
camera = new OrthographicCamera();
batch = new SpriteBatch();
carTexture = new Texture(Gdx.files.internal("NetRace.png"));
carTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
carSprite = new Sprite(carTexture);
}
#Override
public void hide() {
// TODO Auto-generated method stub
}
#Override
public void pause() {
// TODO Auto-generated method stub
}
#Override
public void resume() {
// TODO Auto-generated method stub
}
#Override
public void dispose() {
// TODO Auto-generated method stub
}
}
could you please find my mistake?
The problem was solved.
I had to use sprite.draw(batch); instead of using Batch.draw(Sprite sp, float x, float y); since the Batch.draw(...) method takes the texture from the passed sprite and uses the texture in the drawing process which has a fixed width and a fixed height.
Another way to solve this problem is to use the batch.draw(Sprite, float x, float y, float width, float height); method in the SpriteBatch class.

Why is my Triangle not displaying in opengl es?

I'm trying to display a triangle in opengl es and I have been over the tutorial several times but i cant figure out why this is happening. It displays the background but not the triangle and I've looked over the code in close detail but I couldn't find anything wrong.
Here is my MainActivity:
public class MainActivity extends Activity {
GLSurfaceView ourSurface;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ourSurface = new GLSurfaceView(this);
ourSurface.setRenderer(new GLRenderer());
setContentView(ourSurface);
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
ourSurface.onPause();
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
ourSurface.onResume();
}
}
Here is my Renderer:
public class GLRenderer implements Renderer{
private GLTriangle tri;
public GLRenderer(){
tri = new GLTriangle();
}
#Override
public void onSurfaceCreated(GL10 gl, EGLConfig eglConfig) {
// TODO Auto-generated method stub
gl.glDisable(GL10.GL_DITHER);
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
gl.glClearColor(.8f, 0f, .2f, 1f);
gl.glClearDepthf(1f);
}
#Override
public void onDrawFrame(GL10 gl) {
// TODO Auto-generated method stub
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
GLU.gluLookAt(gl, 0, 0, -10, 0, 0, 0, 0, 2, 0);
tri.draw(gl);
}
#Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
// TODO Auto-generated method stub
gl.glViewport(0, 0, width, height);
float ratio = (float)width/height;
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glFrustumf(-ratio, ratio, -1, 1f, 1, 25);
}
}
Here is my Triangle:
public class GLTriangle {
private float vertices[] = {
0f,1f,
1f,-1f,
-1f,-1f
};
private FloatBuffer vertBuff;
private short pIndex[]= {0,1,2};
private ShortBuffer pBuff;
public GLTriangle(){
ByteBuffer bBuff = ByteBuffer.allocateDirect(vertices.length * 4);
bBuff.order(ByteOrder.nativeOrder());
vertBuff = bBuff.asFloatBuffer();
vertBuff.put(vertices);
vertBuff.position(0);
ByteBuffer pbBuff = ByteBuffer.allocateDirect(pIndex.length * 2);
bBuff.order(ByteOrder.nativeOrder());
pBuff = pbBuff.asShortBuffer();
pBuff.put(pIndex);
pBuff.position(0);
}
public void draw(GL10 gl){
gl.glFrontFace(GL10.GL_CW);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glVertexPointer(2, GL10.GL_FLOAT, 0, vertBuff);
gl.glDrawElements(GL10.GL_TRIANGLES, pIndex.length, GL10.GL_UNSIGNED_SHORT, pBuff);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
}
}
you have to set the color of your triangle!
so have to add
gl.glColorf(0.5f, 0.9f, 0.2f, 1.0f); //r g b + opacity
in draw method, first than vertexPointer() method. Then
remove
gl.glSetFrontFace(GL10l.GL_CW);

AndEngine how to remove bullets and shoot one bullets at a time?

I'm using Andengine for android and I created a code where my player will shoot a bullet when I touch the screen.
Now I want to do 2 things
first: I want the bullet to be removed when it collides with the enemy
Second: I want to be able to shoot only bullet at a time. So as long as the bullets haven't hit the enemy I don't want the method of firing a bullet to be called.
Here's the code I created
I minimized all the codes that are not really important
public class ShooterActivity extends BaseGameActivity implements IOnSceneTouchListener,IAccelerometerListener{
//Variables
private static final int CAMERA_WIDTH = 720;
private static final int CAMERA_HEIGHT = 480;
private Camera mCamera;
private BitmapTextureAtlas mBitmapTextureAtlas;
private TiledTextureRegion mTiledTextureRegion;
private TextureRegion mBulletTextureRegion;
private AnimatedSprite facebox;
private AnimatedSprite enemy;
private Sprite bulletsprite;
private Scene mScene;
private PhysicsWorld mPhysicsWorld;
private Shape ground, roof, right, left;
private Body body, bulletbody, enemybody;
private FixtureDef mFixtureDef = PhysicsFactory.createFixtureDef(1, 0.5f, 0.5f);
private TiledTextureRegion enemyTiled;
private boolean pFlippedHorizontal = true;
#Override
public Engine onLoadEngine() {
// TODO Auto-generated method stub
this.mCamera = new Camera(0,0,CAMERA_WIDTH,CAMERA_HEIGHT);
return new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(CAMERA_WIDTH,CAMERA_HEIGHT),this.mCamera));
}
#Override
public void onLoadResources() {
// TODO Auto-generated method stub
this.mBitmapTextureAtlas = new BitmapTextureAtlas(1024,1024, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
this.mTiledTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mBitmapTextureAtlas, this, "gfx/player.png", 0, 0, 8, 1);
this.enemyTiled = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mBitmapTextureAtlas, this, "gfx/enemy.png", 200, 500, 8, 1);
this.mBulletTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(mBitmapTextureAtlas, this, "gfx/badge.png", 200, 200);
this.mEngine.getTextureManager().loadTexture(mBitmapTextureAtlas);
}
#Override
public Scene onLoadScene() {
// TODO Auto-generated method stub
this.mEngine.registerUpdateHandler(new FPSLogger());
mScene = new Scene();
mScene.setBackground(new ColorBackground(0,0,0));
this.mPhysicsWorld = new PhysicsWorld(new Vector2(0,SensorManager.GRAVITY_EARTH), false);
//Walls
final FixtureDef wallFixture = PhysicsFactory.createFixtureDef(1, 0.5f, 0.5f);
roof = new Rectangle(0, 0, CAMERA_WIDTH, 2);
ground = new Rectangle(0,CAMERA_HEIGHT ,CAMERA_WIDTH,2);
left = new Rectangle(0,0,2,CAMERA_HEIGHT);
right = new Rectangle(CAMERA_WIDTH -2, 0,2,CAMERA_HEIGHT);
PhysicsFactory.createBoxBody(this.mPhysicsWorld, ground, BodyType.StaticBody, wallFixture);
PhysicsFactory.createBoxBody(this.mPhysicsWorld, roof, BodyType.StaticBody, wallFixture);
PhysicsFactory.createBoxBody(this.mPhysicsWorld, left, BodyType.StaticBody, wallFixture);
PhysicsFactory.createBoxBody(this.mPhysicsWorld, right, BodyType.StaticBody, wallFixture);
this.mScene.attachChild(roof);
this.mScene.attachChild(ground);
this.mScene.attachChild(left);
this.mScene.attachChild(right);
//facebox
facebox = new AnimatedSprite(150,150, this.mTiledTextureRegion);
facebox.setScale(.75f);
facebox.animate(200);
body = PhysicsFactory.createBoxBody(this.mPhysicsWorld, facebox, BodyType.DynamicBody, mFixtureDef);
this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(facebox,body,true,false));
this.mScene.attachChild(facebox);
//enemy
enemy = new AnimatedSprite(500,150,this.enemyTiled);
enemy.animate(200);
enemy.setScale(.75f);
enemy.setFlippedHorizontal(pFlippedHorizontal);
this.mScene.attachChild(enemy);
enemybody = PhysicsFactory.createBoxBody(this.mPhysicsWorld, enemy, BodyType.DynamicBody, mFixtureDef);
this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(enemy,enemybody,true,false));
//scene
this.mScene.setOnSceneTouchListener(this);
this.mScene.registerUpdateHandler(mPhysicsWorld);
return mScene;
}
#Override
public void onLoadComplete() {
// TODO Auto-generated method stub
}
//touch the screen to create bullets
#Override
public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {
// TODO Auto-generated method stub
if(pSceneTouchEvent.getAction() == TouchEvent.ACTION_DOWN){
runOnUpdateThread(new Runnable(){
#Override
public void run() {
// TODO Auto-generated method stub
fire();
}
});
//here I want to be able to remove the bullets when it hits the enemy but not sure what method to use
this.mScene.registerUpdateHandler(new IUpdateHandler(){
#Override
public void onUpdate(float pSecondsElapsed) {
// TODO Auto-generated method stub
if(bulletsprite.collidesWith(enemy)){
}
}
#Override
public void reset() {
// TODO Auto-generated method stub
}
});
}
return false;
}
//method to create bullets
public void fire(){
bulletsprite = new Sprite(this.facebox.getX() + 15, this.facebox.getY() -5, this.mBulletTextureRegion);
bulletsprite.setScale(.5f);
bulletbody = PhysicsFactory.createCircleBody(this.mPhysicsWorld, bulletsprite, BodyType.DynamicBody, mFixtureDef);
this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(bulletsprite, bulletbody, true, true));
final Vector2 speed = Vector2Pool.obtain(50, 0);
bulletbody.setLinearVelocity(speed);
Vector2Pool.recycle(speed);
this.mScene.attachChild(bulletsprite);
}
//nothing here just accelerometer
#Override
public void onAccelerometerChanged(AccelerometerData pAccelerometerData) {
// TODO Auto-generated method stub
final Vector2 gravity = Vector2Pool.obtain(pAccelerometerData.getX() *3, 10);
this.mPhysicsWorld.setGravity(gravity);
Vector2Pool.recycle(gravity);
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
this.enableAccelerometerSensor(this);
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
this.disableAccelerometerSensor();
}
}
First, google collision detection - that will help you solve the first problem.
Second, only keep 1 instance of the bullet object, and when it either (a) collides with the enemy or another object, or (b) goes off the screen, then you make the bullet able to be shot again.

Categories

Resources