Sprite not drawing?

Hi,

I cannot for the life of me see why my sprite isn’t being rendered, this is the code:


public class Bullet {

	private Texture bullet = null;
	private Sprite spriteBullet = null;
	private boolean firing;
	
	public Sprite getBullet() { return spriteBullet; }
			
	private double x1,y1,distance,angleToPlayer;
	
	public Bullet()
	{
		firing = false;
		if(bullet==null) {
		     bullet = new Texture(Gdx.files.internal("assets/data/bullet.png"));
		}
	    spriteBullet = new Sprite(bullet);
	
	}
	
	public void fireBullet(float x, float y, float tx, float ty, SpriteBatch batch)
	{
		if(!firing)
		{
			x1 = Math.pow(Math.abs(x) - Math.abs(tx),2);
			y1 = Math.pow(Math.abs(y) - Math.abs(ty),2);
			distance = Math.sqrt(x1 + y1);			
			angleToPlayer = Math.atan2(Math.abs(y-ty), Math.abs(x-tx));
			firing = true;
		}
		spriteBullet.setPosition(x, y);
		spriteBullet.draw(batch);
	}

}

fireBullet is called from within main game loop where the projection matrix is fine (set to camera), batch is fine and the x,y positions above are correct. All my other sprites etc are drawn, just this one refuses and the method is called every frame?

Another second pair of eyes may help?!

Thanks

Well I don’t understand most of the code as you’re using a framework I don’t know and you don’t show much code.
Anyway as improvement I suggest rethinking the null-check on line 14. I would say that is a tautology? (= Something that will always be true).

Also if everything is fine as you’re saying, check if the Sprite is fine and valid. Write yourself a small and easy program to draw exactly that sprite onto your screen, will it work? Also your method draws the sprite at x and y. So the parameters that are passed through the method directly, not being modified. Is that correct?

Hi,

Sorry, the Texture bullet variable is actually set to static, thus the check for null (only want the texture loaded once - I must have changed it when pasting the code).

Sprite is fine, x and y will get updated later, they basically are the current sprites location which is going to fire a bullet in the direction of the player whose
position is at tx,ty hence the bit of math in that method.

I have a manager class which basically renders all the creatures/aliens in the game and also if any creatures need to fire bullets:


public void renderBadGuys(Camera camera, SpriteBatch batch) {
		for (int i = 0; i < badGuys.size(); i++) {
			BadGuy badguy = badGuys.get(i);
			if(badguy!=null)
			{
				if (badguy.health <= 0) // if bad guy dead, remove them from the
										// list
					removeBadGuy(badguy);
				else {
	
					badguy.move(badguy.x, badguy.y, camera);
	
					// Only draw outside baddies that are in the camera's view
					if ((badguy.x >= camera.position.x - (width + 30) && badguy.x <= camera.position.x + width + 10)
							&& (badguy.y >= camera.position.y - height && badguy.y <= camera.position.y + height)) {
	
						badguy.render(batch);

						if(badguy instanceof SentryCop)
						{
							((SentryCop) badguy).fireBullet(batch);
						}
						
						
					}
				}
			}
		}
	}

Then in my BadGuy class the render method:


public void render(SpriteBatch batch) {
		if(this.inVehicle)
		{
			return; // if in a vehicle we do not want to draw the bad guy/creature - we need to draw the vehicle instead
		}
		
		Color c = batch.getColor();
		if (badGuySprites != null) {
			float dt = Gdx.graphics.getDeltaTime();
			animationElapsed += dt;
			while (animationElapsed > frameLength) {
				animationElapsed -= frameLength;
				spriteCurrentFrame = (spriteCurrentFrame == badGuySprites.size - 1) ? 0 : ++spriteCurrentFrame;
			}
				Sprite s = badGuySprites.get(spriteCurrentFrame);
			if (s != null) {
				float colour = worldMap.getBlockColor(new Vector3(x,y,0));
				if(colour==-1) colour=1;
				batch.setColor(colour,colour,colour,1);
				batch.draw(s, bRight ? x + 16 : x, y, bRight ? -16 : 16, 16);
				
				batch.draw(healthTexture2, x, y + 22, 32, 2); // red
				batch.draw(healthTexture, x, y + 22, health, 2);
			}
		}
		batch.setColor(c);
	}

All this code works, it is around 6 months old since I coded it. The framework/graphics engine I’m using is LibGdx.

I just cannot see anything wrong, this is very, very simple stuff.

Where are your batch.begin() and batch.end() calls?