So I've got my sprite moving, turning, and now I need to slow down the rate at which my sprite transitions through its subimages (there're currently 2 subimages per direction). Could someone show me where to look to solve this, or should I just increase his speed and call him the holder of the gold medal for the 100 meter dash?
How do I slow down getSubImage()?
Page 1 of 13 Replies - 227 Views - Last Post: 06 October 2012 - 12:17 PM
Replies To: How do I slow down getSubImage()?
#2
Re: How do I slow down getSubImage()?
Posted 06 October 2012 - 03:45 AM
Lagaam, on 05 October 2012 - 10:32 PM, said:
So I've got my sprite moving, turning, and now I need to slow down the rate at which my sprite transitions through its subimages (there're currently 2 subimages per direction). Could someone show me where to look to solve this, or should I just increase his speed and call him the holder of the gold medal for the 100 meter dash?
What is getSubImage()? / What are you using to create this game?
Sprinkle that answer with some of your code and you'll be more likely to have someone answer it
#3
Re: How do I slow down getSubImage()?
Posted 06 October 2012 - 12:11 PM
Sorry, I'll post my code up here... This is the Java forums right?
engine <- (2D game engine that I am working on. Still learning so point anything out that could be done better)
gameTest <-(a test game I'm working on until I have my 2D game engine set up to how I want it.)
engine <- (2D game engine that I am working on. Still learning so point anything out that could be done better)
package rand.LearningJava.Lagaan;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class engine {
public static void start(Game game) {
JFrame frame = new JFrame(game.getTitle());
frame.setSize(game.width(), game.height());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GameCanvas canvas = new GameCanvas(game);
frame.add(canvas);
frame.setVisible(true);
GameLoop loop = new GameLoop(game, canvas);
loop.start();
}
}
abstract class Game implements KeyListener, MouseListener, MouseMotionListener {
protected String title = "GameEngine";
protected int width = 640, height = 480, delay = 30;
protected boolean over;
public abstract void init();
public abstract void update();
public abstract void draw(Graphics2D g);
public String getTitle() {
return title;
}
public int width() {
return width;
}
public int height() {
return height;
}
public int getDelay() {
return delay;
}
public boolean isOver() {
return over;
}
@Override
public void keyPressed(KeyEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
@Override
public void mouseDragged(MouseEvent e) {
}
@Override
public void mouseMoved(MouseEvent e) {
}
}
class GameCanvas extends JComponent {
private static final long serialVersionUID = 1L;
private final Game game;
public GameCanvas(Game game) {
this.game = game;
addKeyListener(this.game);
addMouseListener(this.game);
addMouseMotionListener(this.game);
requestFocus();
setFocusable(true);
}
public void paintComponent(Graphics g) {
game.draw((Graphics2D) g);
}
}
class GameLoop extends Thread {
private final Game game;
private final GameCanvas canvas;
public GameLoop(Game game, GameCanvas canvas) {
this.game = game;
this.canvas = canvas;
}
public void run() {
game.init();
while (!game.isOver()) {
game.update();
canvas.repaint();
try {
Thread.sleep(game.getDelay());
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
gameTest <-(a test game I'm working on until I have my 2D game engine set up to how I want it.)
package rand.LearningJava.Lagaan;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class gameTest extends Game {
public static final void main(String[] args) {
engine.start(new gameTest());
}
public BufferedImage player;
int frame, frameCount, dir, dirx, diry, STEP, x = 100, y = 100;
boolean locked;
public gameTest() {
title = "gameTest";
frame = 0;
dir = KeyEvent.KEY_RELEASED;
STEP = 0;
locked = false;
try {
player = ImageIO.read(new File("images/FinalFantasy3BlueKnight.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void init() {
}
@Override
public void keyPressed(KeyEvent e) {
if (!locked) {
dir = e.getKeyCode();
locked = true;
}
}
@Override
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == dir) {
locked = false;
}
}
@Override
public void update() {
frame++;
if (frame > 50) {
frame = 0;
}
if (frame > 25) {
frameCount = 1;
}
else {
frameCount = 0;
}
if (dir - 37 == 0) {
dirx = 0;
diry = 1;
}
else if (dir - 37 == 1) {
dirx = 0;
diry = 0;
}
else if (dir - 37 == 2) {
dirx = 1;
diry = 1;
}
else if (dir - 37 == 3) {
dirx = 0;
diry = 2;
}
if (locked == false) {
STEP = 0;
}
else {
STEP = 1;
}
switch (dir) {
case KeyEvent.VK_LEFT:
x -= STEP;
break;
case KeyEvent.VK_RIGHT:
x += STEP;
break;
case KeyEvent.VK_UP:
y -= STEP;
break;
case KeyEvent.VK_DOWN:
y += STEP;
break;
}
}
@Override
public void draw(Graphics2D g) {
g.drawImage(player.getSubimage(dirx*32 + frameCount*16, diry*16, 16, 16), x, y, null);
}
}
#4
Re: How do I slow down getSubImage()?
Posted 06 October 2012 - 12:17 PM
Also, I got my sprite to slow down ing the above code by increasing how high "frame" can go and making another variable set to alternate between 0 and 1 based on whether frame is greater than half its max.
Used to be this
frame++;
if (frame > 50) {
frame = 0;
}
if (frame > 25) {
frameCount = 1;
}
else {
frameCount = 0;
}
Used to be this
frame++;
if (frame > 1) {
frame = 0;
}
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote



|