Results 1 to 8 of 8
  1. #1
    Join Date
    Apr 2013
    Location
    Your face.
    Posts
    92
    Rep Power
    0

    Exclamation Movement not working...

    Hi! RiokuTheSlayer here with ANOTHER bugy thing.

    My character wont move when i use "A" and "D".

    Listening.java code:
    Java Code:
    package net.ulixava.RetroHunter;
    
    import java.awt.event.*;
    
    public class Listening implements KeyListener {
    
    	public void keyPressed(KeyEvent e) {
    		int key = e.getKeyCode();
    		
    		switch(key) {
    		case KeyEvent.VK_H:
    		Component.isMoving = true;
    		Component.dir = Character.movementSpeed;
    			break;
    		case KeyEvent.VK_A:
    			Component.isMoving = true;
    			Component.dir = -Character.movementSpeed;
    			break;
    	    
    		}
    	}
    
    	public void keyReleased(KeyEvent e) {
    		int key = e.getKeyCode();
    		
    		switch(key) {
    		case KeyEvent.VK_D:
    		    if(Component.dir == Character.movementSpeed) {
    		    	Component.isMoving = false;
    		    }
    			break;
    		case KeyEvent.VK_A:
    			if (Component.dir == -Character.movementSpeed) {
    		    	Component.isMoving = false;
    		    }
    			break;
    		}
    		
    	}
    
    	public void keyTyped(KeyEvent e) {
    		
    	}
    	
    
    }
    Character code:
    Java Code:
    package net.ulixava.RetroHunter;
    
    import java.awt.*;
    
    public class Character extends DoubleRectangle {
    	public static double fallingSpeed = 2.5;
    	public static double movementSpeed = 15;
    	
    	public Character(int width,int height) {
    		setBounds((Component.pixel.width / 2) - (width / 2) , (Component.pixel.height / 2) - (height / 2), width, height);
    		
    	}
    	
    	public static void tick(){
    		if(!IsCollidingWithBlock(new Point ((int) x, (int) (y + height)), new Point((int) (x + width), (int) (y + height)))){
    			y += fallingSpeed;
    			Component.sY += fallingSpeed;
    		}
    		
    		if(Component.isMoving == true) {
    			x += movementSpeed;
    		}
    	}
    	
    
    	public static boolean IsCollidingWithBlock(Point pt1, Point pt2) {
    		for(int x=(int) (DoubleRectangle.x/Tile.tileSize);x<(int) (DoubleRectangle.x/Tile.tileSize) + 3;x++) {
    			for(int y=(int) (DoubleRectangle.y/Tile.tileSize);y<(int) (DoubleRectangle.y/Tile.tileSize) + 3;y++) {
    				if(Level.block[x][y].id != Tile.air) {
    					if(Level.block[x][y].contains(pt1) || Level.block[x][y].contains(pt2)) {
    						return true;
    					}
    				}
    			}
    		}
    		return false;
    	}	
    
    	public static void render(Graphics g){
    		g.drawImage(Tile.tileset_terrain, (int) x - (int) Component.sX, (int) y - (int) Component.sY, (int) (x + width) - (int) Component.sX, (int) (y + height) - (int) Component.sY, Tile.character[0] * Tile.tileSize, Tile.character[1] * Tile.tileSize, Tile.character[0] * Tile.tileSize + (int)width, Tile.character[1] * Tile.tileSize + (int)height,null);
    	}
    }
    And the place i Add a new Listening is i the Component class:
    Java Code:
       public void start() {
        	//Defining Objects etc.
        	new Tile(); //Loading Images
        	level = new Level();
        	character = new Character(Tile.tileSize,Tile.tileSize * 2);
        	
        	//Start game loop.
        	new Thread(this).start();
        	isRunning = true;
    		addKeyListener(new Listening());
        }
    >.< In case some of you are wondering,i'm sorta new to Java.I've been developing a new game,so i'm encountering alot of bugs here and there.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,963
    Rep Power
    8

    Default Re: Movement not working...

    Time to sharpen your debugging skills. Does the KeyListener ever get triggered? Do you enter the keyPressed() method at all? What happens after that? Step through this with a debugger, or at least add some print statements, to figure it out.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Join Date
    Apr 2013
    Location
    Your face.
    Posts
    92
    Rep Power
    0

    Default Re: Movement not working...

    Yeah,i sorta posted this without thinking >.< I need more team members for this game.... Its just me right now.(with a texture artist,but still.)I didn't even trigger it!But,how DO i? because every time i add Listening.keyPressed(); into the Tick method,it says that keyPressed() has to be static,but THEN it says that keyPressed isn't supposed to have a static modifier.

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,963
    Rep Power
    8

    Default Re: Movement not working...

    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Join Date
    Apr 2013
    Location
    Your face.
    Posts
    92
    Rep Power
    0

    Default Re: Movement not working...

    Well,all i need to know is how to call the keyPressed and keyReleased methods.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default Re: Movement not working...

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default Re: Movement not working...

    Quote Originally Posted by RiokuTheSlayer View Post
    every time i add Listening.keyPressed(); into the Tick method,
    As Kevin has already implied, that's not the way a KeyListener (or any event listener) is used.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  8. #8
    Join Date
    Apr 2013
    Location
    Your face.
    Posts
    92
    Rep Power
    0

    Default Re: Movement not working...

    Turns out,the movement speed made it WAY to fast

    Bug Fixed.(the code is all correct.)

Similar Threads

  1. Movement
    By ChaoticXknight in forum New To Java
    Replies: 2
    Last Post: 03-06-2013, 08:38 PM
  2. Replies: 2
    Last Post: 03-10-2012, 04:26 AM
  3. Movement
    By elamre in forum Java Applets
    Replies: 24
    Last Post: 01-18-2012, 08:44 PM
  4. Key/mouse movement
    By falkon114 in forum New To Java
    Replies: 3
    Last Post: 02-21-2011, 02:12 AM
  5. Movement of balls
    By BlitzA in forum New To Java
    Replies: 8
    Last Post: 01-09-2008, 03:30 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •