Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Problem using keylistener to move an image
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
goober69




PostPosted: Tue Jun 10, 2008 8:51 pm   Post subject: 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 Razz 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 Smile
code:

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<15;i++) {
        String[] temp1 = in.readLine().split("\\s");
        for(int x = 0;x<temp1.length;x++){
            TILE[cntr] = Toolkit.getDefaultToolkit().getImage(temp1[x]+".PNG");
            cntr = cntr + 1;
//             System.out.print(temp1[x]+" ");
        }                                  //BOTH SYSTEM.out LINES ARE FOR DEBUGGING PURPOSES ONLY
//         System.out.println();
        }
        in.close();
        } catch (IOException e) {
        }
       
        //DISPLAY THE TILES
        setLayout(null);
        int TX = 0, TY = 0;
        for(int i = 0;i<225;i++){
            if(TX>448){
                TY=TY+32;
                TX=0;
            }
            add(new JLabel(new ImageIcon(TILE[i]))).setBounds(TX,TY,32,32);
            TX = TX + 32;
        }
       
       
        setSize(488,514);
        setVisible(true);
        validate();
   
        //LOAD AND DISPLAY MAIN CHARACTER
       
        Mario.setBounds(MX,MY,16,16);
        ImageIcon icon = new ImageIcon("TEST_SPRITE.PNG");
        Mario.setIcon(icon);
        validate();               
        getLayeredPane().add(Mario,new Integer(Integer.MAX_VALUE));
        setFocusable(true);
        addKeyListener(this);
        animate();
    }   
        //CONTROL MARIO WITH THE KEYBOARD
        public void animate(){
        setFocusable(true);
        addKeyListener(this);
        if(key_down == true)
              MY=MY+1;
        if(key_up == true)
              MY=MY-1;
        if(key_right == true)
              MX=MX+1;
        if(key_left == true) // Move the player to the left
              MX=MX-1;
        Mario.setBounds(MX,MY,16,16);
     
    }
   
    public void keyPressed(KeyEvent e){System.out.println("KEY PRESSED!");
                    if(e.getKeyCode() == e.VK_DOWN) // If down is pressed...
                         key_down = true;
                    if(e.getKeyCode() == e.VK_UP) // If up is pressed...
                         key_up = true;
                    if(e.getKeyCode() == e.VK_RIGHT)
                         key_right = true;
                    if(e.getKeyCode() == e.VK_LEFT)
                         key_left = true;
                    animate();
                       
    }
    public void keyReleased(KeyEvent e){System.out.println("KEY RELEASED");
                    if(e.getKeyCode() == e.VK_DOWN) // If down is released...
                         key_down = false;
                    if(e.getKeyCode() == e.VK_UP) // If up is released...
                         key_up = false;
                    // Ect...
                    if(e.getKeyCode() == e.VK_RIGHT)
                         key_right = false;
                    if(e.getKeyCode() == e.VK_LEFT)
                         key_left = false;
                   
    }
    public void keyTyped(KeyEvent e){}

}
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 1 Posts ]
Jump to:   


Style:  
Search: