public class MoveableImage
{
private float x, y;
private Image image;
public MoveableImage(Image i, float startX, float startY)
{
x = startX;
y = startY;
image = i;
}
public void draw(Graphics g)
{
g.drawImage(image, (int)x, (int)y);
}
public void moveTo(float newX, float newY)
{
x = newX;
y = newY;
}
public void moveBy(float deltaX, float deltaY)
{
x += deltaX;
y += deltaY;
}
}
MoveableImage image;
public void keyReleased(KeyEvent e)
{
int c = e.getKeyCode();
if (c == KeyEvent.VK_LEFT)
{
image.moveBy(-1.0f, 0.0f);
}
if (c == KeyEvent.VK_DOWN)
{
image.moveBy(0.0f, 1.0f);
}
//etc.
}
Clear enough? That’s not optimal, but sounds like you’re really far from optimal. When you’re a lot more comfortable coding I’d put the moveBy’s into a game loop instead of being called directly by the keyReleased function.
Thank you. But if I keep the directional pad down the sprite will move quickly or slowly? to be clear I would like to create a super mario type platform
I studied several books and supported tests in java. My problem is that I want an equal movement in case you press or hold a button. For this reason I asked
If this is real code then it has some grammatical mistakes and compiler shouldn’t compile or run.
Looks like you are missing some ; and break in some switch cases. Also there maybe some other problems that I don’t see.
Sorry but to me it always compiled this code, maybe you can give problems only because I do not find images why don’t you have.
There is some optimization that I can do for this code?
I haven’t fully read his code, but I see no syntatic problems here. You don’t need to have a break for each case.
@zazza:
I highly recomend you checking some 4k skeletons. They’re simple at most but you’ll have a nice skeleton on drawing things in screen and treating keypresses, in the best ways available (and not using AWT, JLabels, ImageIcon which are definetely not meant to be used with games).
Well looks like my eyes deceive me. Because I only look at code but don’t compiled it. But I think that you should put break after each case or don’t make that case, because switch without breaks could execute unwanted code which don’t belong to that case (or belong to other case). And I must admire to teletubo that awt, swing isn’t made for games, but you can use it if you don’t have anything better. (But mostly there is)
Guys I have modified my code through a 4k skeleton but I have the problem that my image moves too fast. Now place the code
package Gioco;
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Gioco extends Applet implements Runnable {
private boolean[] keyPress = new boolean[32768];
private final int SPRITE_DIM = 32;
private final int COLONNE = 20;
private final int RIGHE = 20;
private final int APPLET_WIDTH = RIGHE*SPRITE_DIM;
private final int APPLET_HEIGHT = COLONNE*SPRITE_DIM;
private final int JUMP = SPRITE_DIM;
private JLabel[] arraylabel;
private ImageIcon imgTexture;
private JPanel jp;
private Image image;
//altre inizializzazioni
public void start(){
enableEvents(8);
new Thread(this).start();
}
public void run(){
//variabili di istanza
int playerX = 0;
int playerY=0;
final int VK_LEFT = 0x25;
final int VK_RIGHT = 0x27;
final int VK_UP = 0x26;
final int VK_DOWN = 0x28;
final int VK_ATTACK = 0x42;
final int MILLISECONDI = 100;
ImageIcon imgPers;
JLabel labelPers;
boolean isControlled=true;
int x=0;
int y=0;
int inc_x;
int inc_y;
ImageIcon immagine;
try {
image = ImageIO.read(new File("C:/Users/vincenzo/Desktop/img/grass09.jpg"));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
/*getContentPane().*/setLayout(null); //tolgo il LayoutManager predefinito
/*getContentPane().*/setPreferredSize(new Dimension(APPLET_WIDTH, APPLET_HEIGHT));
imgPers = new ImageIcon("C:/Users/vincenzo/Desktop/img/persT.PNG");
labelPers = new JLabel(imgPers);
labelPers.setBounds(0, 0, 32, 32); // Imposto posizione e dimensione del mio JTextPane
labelPers.setOpaque(false);
jp = new BackgroundPanel(image);
jp.setLayout(null);
jp.setBounds(0, 0, APPLET_WIDTH, APPLET_HEIGHT); // Imposto posizione e dimensione del mio JPanel
//getContentPane().add(labelPers);
jp.add(labelPers);//
createImageIcon();
createPanelImage();
/*getContentPane().*/add(jp);
//inizializzazione
long nextFrameStartTime = System.nanoTime();
//while true
while(true){
do{
nextFrameStartTime += 16666667;
//inizio aggiornamenti
x = playerX;
y = playerY;
if (keyPress[VK_UP]) {
y -= JUMP;
} else if (keyPress[VK_DOWN]) {
y += JUMP;
} else if (keyPress[VK_LEFT]) {
x -= JUMP;
} else if (keyPress[VK_RIGHT]) {
x += JUMP;
}
if ((x>=0) && (x<APPLET_WIDTH))
playerX=x;
if ((y>=0) && (y<APPLET_HEIGHT))
playerY=y;
// fine aggiornamenti
}while(nextFrameStartTime < System.nanoTime());
//render starts
labelPers.setBounds(playerX, playerY,32,32);
//render end
// burn off extra cycles
while(nextFrameStartTime - System.nanoTime() > 0) {
Thread.yield();
}
}
//
}
public void processKeyEvent(KeyEvent keyEvent) {
final int VK_LEFT = 0x25;
final int VK_RIGHT = 0x27;
final int VK_UP = 0x26;
final int VK_DOWN = 0x28;
final int VK_ATTACK = 0x42;
final int VK_W = 0x57;
final int VK_S = 0x53;
final int VK_A = 0x41;
final int VK_D = 0x44;
int k = keyEvent.getKeyCode();
if (k > 0) {
k = k == VK_W ? VK_UP : k == VK_D ? VK_RIGHT : k == VK_A ? VK_LEFT
: k == VK_S ? VK_DOWN : k;
keyPress[(k >= VK_LEFT && k <= VK_DOWN) ? k : VK_ATTACK]
= keyEvent.getID() != 402; //=key released
}
}
private void createImageIcon() {
int i;
int j;
arraylabel = new JLabel[RIGHE];
imgTexture = new ImageIcon("C:/Users/vincenzo/Desktop/img/erbaTR.PNG");
for(i=0;i<RIGHE;i++){
arraylabel[i]= new JLabel(imgTexture);
arraylabel[i].setSize(32,32);
//arraylabel[i].setBounds(i*JUMP, JUMP*4, 32, 32);
}
}
private void createPanelImage(){
//for(int i=0;i<RIGHE;i++)
// jp.add(arraylabel[i]);
for(int i=0;i<RIGHE;i++){
arraylabel[i].setBounds(i*JUMP, JUMP*5, 32, 32);
jp.add(arraylabel[i]);
}
}
class BackgroundPanel extends JPanel
{
private Image img;
public BackgroundPanel (Image img)
{
super (new BorderLayout ());
this.img = img;
}
public void paintComponent (Graphics g)
{
super.paintComponent (g);
g.drawImage (img, 0, 0, this);
}
}
}
Well looks who’s here
But i think that if you think that my post is wrong you are wrong!
Switch without break is just a lines of code and no switch.
Case without break is damned to execute all other following cases and code;
P.S. No offence.
Will +1 what others have said, a drop-through switch case is totally fine. Perhaps what you are alluding to is a dropthrough case that has additional logic, which some view as bad practice (and it’s not even valid in C#). But a flat dropthrough like that is fine everywhere.
Also I will +1 not using the 4k example as 4k does a lot of bad practices to reduce memory usage. Instead check out Basic Game tutorial and Game Loops tutorial. But since you’ve already adapted the 4k skeleton just give it a go I suppose.