NEW IMAGE NO SPRITE WITH :
if(q>=15){
screen.clear(0);
screen.render(Art.sprites[i][0], 20 , cx);
i++;
movement();
cx+=1;
if(i>=3)i=0;
q=0;
}
This sprite leaves a trail and I can't find a way to fix it. I spent close to a good 2 hours trying to look online for it and could not fix it if someone could help that would be great. <3
Note: The sprite is drawn in the "render" method. (I hope that there is enough information. Also 1st time working with sprites)
import java.awt.Canvas;
import java.awt.Color;
//import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import javax.swing.JFrame;
import me.matt.tower.gfx.Art;
import me.matt.tower.gfx.Screen;
public class TowerComponent extends Canvas implements Runnable{
/**
*
*/
private static final long serialVersionUID = 1L;
public static final int WIDTH = 640, HEIGHT = 480;
private boolean running =false;
public static final String NAME = "Tower Of Death";
private Screen screen;
public int q=0;
public int i=0;
public int cx= 20;
public int cy= 20;
public TowerComponent (){
screen = new Screen (WIDTH, HEIGHT);
}
public void stop(){
running = false;
}
public void start(){
running = true;
new Thread(this).start();
}
@Override
public void run() {
int fps = 0, tick = 0;
double fpsTimer = System.currentTimeMillis();
double nsPerTick = 1000000000.0d / 60;
double then = System.nanoTime();
double unprocessed = 0;
while(running){
boolean canRender = false;
double now = System.nanoTime();
unprocessed += (now-then) / nsPerTick;
then = now;
while (unprocessed >=1){
//Updates game objects
tick++;
tick();
canRender = true;
--unprocessed;
}
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
//draws game objects
if(canRender){
fps++;
render();
}
//FPS Counter Report
if(System.currentTimeMillis()- fpsTimer >1000){
System.out.printf("%d fps, %d tick%n",fps,tick);
fps = 0; tick =0;
fpsTimer +=1000;
}
}
}
private void render() {
BufferStrategy bs = getBufferStrategy();
if(bs == null){
createBufferStrategy(2);
requestFocus();
return;
}
Graphics g = bs.getDrawGraphics();
//Rendering
q++;// counts frames.... use fps count later?
if(q>=15){
screen.clear(0);
screen.render(Art.sprites[i][0], 20 , cx);
i++;
movement();
cx+=1;
if(i>=3)i=0;
q=0;
}
//done rendering
g.drawImage(screen.image, 0,0, WIDTH,HEIGHT,null);
g.dispose();
bs.show();
}
public void movement(){
cx+=10;
cy+=10;
screen.clear(0);
repaint();
}
private void tick() {
}
public static void main(String [] args){
TowerComponent game = new TowerComponent();
Dimension dimension = new Dimension(WIDTH,HEIGHT);
game.setMaximumSize(dimension);
game.setMaximumSize(dimension);
game.setPreferredSize(dimension);
game.setSize(dimension);
JFrame frame = new JFrame();
frame.setTitle(NAME);
frame.add(game);
frame.pack();
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
game.start();
}
}
Bitmap class
package me.matt.tower.gfx;
import java.util.Arrays;
public class Bitmap {
public int w,h;
public int [] pixels;
public Bitmap(int w , int h){
this.w = w;
this.h = h;
pixels = new int [w*h];
}
//public void clear(int color){
// Arrays.fill(pixels, color);
//}
public void render(Bitmap bitmap, int x, int y){
int x0= x;
int x1 = x + bitmap.w;
int y0=y;
int y1 = y + bitmap.h;
if(x0 <0)x0=0;
if(x1>w) x1 = w;
if(y0 < 0)y0=0;
if(y1 >h) y1=h;
int ww = x1 - x0;
for (int yy= y0; yy < y1; yy++){
int tp = yy *w +x0;
int sp = (yy - y )* bitmap.w + (x0 -x);
tp -= sp;
for(int xx = sp; xx < sp + ww; xx++){
int col = bitmap.pixels[xx];
if (col < 0) pixels [tp+xx] = col;
}
}
}
}
Screen class
package me.matt.tower.gfx;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
public class Screen extends Bitmap{
public BufferedImage image;
public Screen(int w, int h) {
super(w, h);
image = new BufferedImage(w ,h , BufferedImage.TYPE_INT_ARGB);
pixels = ((DataBufferInt)image.getRaster().getDataBuffer()).getData();
}
}
Art class
import java.io.IOException;
import javax.imageio.ImageIO;
import me.matt.tower.TowerComponent;
public class Art {
public static final Bitmap [][] sprites = cut("/SpriteSheet.png", 32 ,32);
private static Bitmap[][] cut(String string, int w,int h){
return cut(string ,w,h,0,0);
}
private static Bitmap[][] cut(String string, int w, int h, int sx ,int sy){
try {
BufferedImage image = ImageIO.read(TowerComponent.class.getResourceAsStream(string));
int xTiles = (image.getWidth()- sx)/w;
int yTiles = (image.getHeight()-sy)/h;
Bitmap[][] result = new Bitmap[xTiles][yTiles];
for(int x= 0; x< xTiles;x++){
for(int y=0;y< yTiles;y++){
result[x][y]=new Bitmap(w,h);
image.getRGB( sx + x * w, sy + y * h,w,h,result[x][y].pixels,0,w);
}
}
return result;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
screen.clear();
call must be just beforescreen.render(...)
insidepublic void render()
function. Could you post the contents of thescreen.render(...)
function? \$\endgroup\$