
-----------------------------------
goober69
Tue Jun 10, 2008 8:51 pm

Problem using keylistener to move an image
-----------------------------------
Hey there, got a new roadblock in my mario"ish" game that I can't seem to pass. Currently, my game reads a text file, in which a series of numbers are stored, and the numbers correspond to tile images in the program. The code displays these tiles in proper position, and everything works great. Earlier, I had a problem with drawing an ImageIcon of the main character sprite (all images in my program are ImageIcons, probably not the best method im sure but w.e it's the only way I could figure it out) overtop of the tile images, but I fixed this by adding the main character image to a layered pane. Now, here is my current problem :P I have implemented a key listener, and stolen someone elses idea to use boolean values to determin if an arrow key is being held down. The actual Key listener works, but when I try and use it to move the main character's image, it glitches and moves in strange ways(changing the X and Y values of the image). I've spent hours trying to figure this out and I've gotten nowhere :S PLEASE help, i'm sure it's not a complicated problem. Thanks :)

import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.util.*;
import java.awt.event.*;

public class MainFrame extends JFrame implements KeyListener{
    public int MX = 34, MY = 34;
    public JLabel Mario = new JLabel();
    boolean GAME = true;
    boolean key_right,key_left,key_down,key_up;
    public MainFrame(){
       
        super("SUPER MARIO MAZE");       
                        
        //LOAD THE TILES
        Image[] TILE = new Image[225];
        int cntr = 0;
        try {
        BufferedReader in = new BufferedReader(new FileReader("1.txt"));
        String record = null;
        for(int i = 0;i