0

Make it face the other way Pull from this sheet after taking from sheet

I have a sprite sheet that I can change the little slimey's picture from but it only changes like the actual image I want to make them so when I press the right key it takes the pictures and gives it the one where it's facing right. I tried to get it so that it takes a int from the slime's class and uses it when I press right but it only prints the number 2 in the console but isn't changing the picture....... the stuff for the slime....

package com.maprildoll.main;

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.Random;

public class Slimey extends DollObject{
    
    Random r = new Random();
    
    private int x;
    private int y;
    public int sprites = 0;
    
    private BufferedImage slimey;
    public SpriteSheet slimes;

    public Slimey(ID id, int x, int y, Slimeball slimeball) {
        super(x, y, id);
        
        this.x = x;
        this.y = y;
        
        SpriteSheet slimes = new SpriteSheet(slimeball.getSpriteSheet());
        slimey = slimes.grabImage(1, 1, 69, 49);
        if(sprites == 2) {
            slimey = slimes.grabImage(2, 1, 69, 49);
        }
    }   

    public void tick() {
        x += speedX;
        y += speedY;
        
        x = Slimeball.clamp(x, 0, Slimeball.WIDTH - 82);
        y = Slimeball.clamp(y, 88, Slimeball.HEIGHT - 278);
    }

    public void render(Graphics g) {
        g.drawImage(slimey, (int)x, (int)y, null);
    }
}

the main window thingy....

package com.maprildoll.main;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

import javax.swing.ImageIcon;

public class Slimeball extends Canvas implements Runnable{

    private static final long serialVersionUID = -2882786666891858852L;
    public static final int WIDTH = 1700, HEIGHT = 900;
    
    private Thread thread;
    private boolean running = false;
    
    private Random r;
    private Handler handler;
    
    private BufferedImage spriteSheetSlimes;
    private BufferedImage spriteSheetItem = null;
    private BufferedImage items;
    
    private Slimey slimey;

    
    public Slimeball (Slimeball slimeball){
        
        handler = new Handler();
        this.addMouseListener(new Mousey(handler));
        new Windows(WIDTH, HEIGHT, "Slimeballin' >.<!!", this);
        
        r = new Random();
        
            handler.addObject(new Slimeyball(WIDTH/2-32, HEIGHT/2+186, ID.Slimeyball));
        }
    
    public void init() {
        requestFocus();
            ImageLoader loader = new ImageLoader();
            try {
                spriteSheetItem = loader.loadImage("C:\\Users\\SPooKykun\\eclipse-workspace\\MaprilDolly\\src\\com\\maprildoll\\main\\Slimeball\\itemsheet.png");
                spriteSheetSlimes = loader.loadImage("C:\\Users\\SPooKykun\\eclipse-workspace\\MaprilDolly\\src\\com\\maprildoll\\main\\Slimeball\\slimebox.png");
            }catch(IOException e) {
                e.printStackTrace();
            }
            
            addKeyListener(new Slimekey(this));
            SpriteSheetTew itemsheet = new SpriteSheetTew(spriteSheetItem);
            items = itemsheet.grabImage(1, 1, 32, 32);
            slimey = new Slimey(ID.Slime, 1040, 620, this);
    }
    
    

    public synchronized void start() {
        thread = new Thread(this);
        thread.start();
        running = true;
    }
    
    public synchronized void stop() {
        try{
            thread.join();
            running = false;
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    
    public void run(){
        init();
        long lastTime = System.nanoTime();
        double amountOfTicks = 60.0;
        double ns = 1000000000 / amountOfTicks;
        double delta = 0;
        long timer = System.currentTimeMillis();
        int frames = 0;
        while(running){
            long now = System.nanoTime();
            delta += (now - lastTime) / ns;
            lastTime = now;
            while(delta >=1) {
                tick();
                delta--;
            }
            if(running)
                render();
            frames++;
            
            if(System.currentTimeMillis() - timer > 1000) {
                timer += 1000;
                System.out.println("FPS: " + frames);
                frames = 0;
            }
        }
        stop();
    }
    
    private void tick() {
        handler.tick();
        slimey.tick();
    }
    
    private void render() {
        BufferStrategy bs = this.getBufferStrategy();
        if(bs == null){
            this.createBufferStrategy(3);
            return; 
        }
    
    Graphics g = bs.getDrawGraphics();
    
    Color cyanie = new Color (231, 255, 254);
    g.setColor(cyanie);
    g.fillRect(0, 0, WIDTH, HEIGHT);
     final ImageIcon background = new ImageIcon("C:\\Users\\SPooKykun\\eclipse-workspace\\MaprilDolly\\src\\com\\maprildoll\\main\\Slimeball\\testbackground.gif");
     Image imgback = background.getImage();
      g.drawImage(imgback, 0, 0, this);
      g.drawImage(items, 865, 636, this);
      slimey.render(g);
    
      handler.render(g);
    
      g.dispose();
      bs.show();
  }
    
    public void keyPressed(KeyEvent e) {
        int key = e.getKeyCode();
        
        if (key == KeyEvent.VK_LEFT) {
            slimey.setSpeedX(-5);
        } else if (key == KeyEvent.VK_RIGHT) {
            slimey.setSpeedX(+5);
            slimey.sprites = 2;
            System.out.println(slimey.sprites);
        } else if (key == KeyEvent.VK_ALT){
            slimey.setSpeedY(-8);
        }
    }
    
    public void keyReleased(KeyEvent e) {
        int key = e.getKeyCode();
        
        if (key == KeyEvent.VK_LEFT) {
            slimey.setSpeedX(0);
        } else if (key == KeyEvent.VK_RIGHT) {
            slimey.setSpeedX(0);
        } else if (key == KeyEvent.VK_ALT);{
            slimey.setSpeedY(+4);
        }
    }
    
    
    public static int clamp(double x, int min, int max){
        if(x >= max)
            return (int) (x = max);
        else if(x <= min)
            return (int) (x = min);
        else
            return (int) x;
    }
    
    
    public BufferedImage getSpriteSheet() {
            return spriteSheetSlimes;
    }
    
}

I tried to put a keylistener to the Slimey but for some reason the keyboard would never work when it was in there..... soo how would I take a image from the sheet and add it to the keypressed stuff I want to put the right facing one when it presses right.....

Locke
  • 7,626
  • 2
  • 21
  • 41
Zwei
  • 1
  • 3
  • Since I don't see `SpriteSheet` or `SpriteSheetTew` in your imports list, I assume you must have written them and they are defined in package `com.maprildoll.main`. What do they look like? – Locke Mar 17 '22 at 08:29
  • SpriteSheet and SpriteSheetTew "two" are the same thing but just one has the sizes for items and one for the slimes sense when i had only the one only the slimes were reading fine after the first time it was messing it up because size differences of the slimes and items.... this is the sprite sheet – Zwei Mar 17 '22 at 12:33
  • i guess i will have to put images instead sense it doesn't really work in the comments https://ibb.co/s5shjMR – Zwei Mar 17 '22 at 12:34

0 Answers0