Results 1 to 6 of 6

Thread: character movement in Java not working

  1. #1
    Join Date
    Jul 2010
    Posts
    38

    character movement in Java not working

    I'm making my first game in java, now I managed to paint the levels and move the character using the keyboard. At the moment my character moves in any direction (not diagonal) but you can wander around the map, and therefore my next step is to move the logic that has the game. I will present my idea for it and wanted to give me opinions ...

    My levels are on file .Txt and I thought that according to the current level progress along the file at that level and be copied to an array. Later I have to check if the character can move in different positions, for it traverses the array with a for loop to find the character and then calculate the next position and according to the number you have saved the file to go because I can set the character motion. I enter the code below:
    Code:
    public void copyboard () {// create array from the current level 
    
    File file = null; 
    FileReader fr = null; 
    BufferedReader br = null; 
    
    try { 
    
    file = new File (getClass (). getResource ("/ levels /" + / * getLevel () + * / "1.txt"). toString ().substring (6)); 
    fr = new FileReader (file); 
    br = new BufferedReader (fr) 
    
    String line; 
    
    while ((line = br.readLine ())! = null) { 
    char [] character = linea.toCharArray (); 
    // OFF = 0, WHITE = 1, SKIP = 2, 
    // WALL = 3, POSFINAL = 4, BLOCK = 5, CHARACTER = 6 = 7 PLACED 
    for (int i = 0; i <line.length (); i + +) { 
    for (int j = 0, j <array.length j + +) { 
    if (character [i] == '') { 
    array [i] [j] = WHITE; 
    
    } Else if (character [i] == '#') { 
    array [i] [j] = WALL; 
    
    } Else if (character [i] == '2 ') { 
    array [i] [j] = JUMP; 
    
    } Else if (character [i] == '$') { 
    array [i] [j] = BLOCK; 
    
    } Else if (character [i] == '@') { 
    array [i] [j] = CHARACTER; 
    
    } Else if (character [i] == '.') { 
    array [i] [j] = FINALPOS; 
    } Else if (character [i] == '0 ') { 
    array [i] [j] = NO; 
    } Else if (character [i] == '*') { 
    array [i] [j] = REGISTERED; 
    Else {} 
    
    System.out.println ("WTF character that does not exist"); 
    } 
    } 
    } 
    } 
    } Catch (Exception e) { 
    System.out.println ("Error reading file"); 
    e.printStackTrace (); 
    } Finally { 
    try { 
    if (null! = fr) { 
    fr.close (); 
    } 
    } Catch (Exception e2) { 
    e2.printStackTrace (); 
    } 
    } 
    }
    The truth is that my code does not work (if compiled), because the press "up" for not only not moving but no longer incorporates keyboard. Please help me..

  2. #2
    Join Date
    Apr 2008
    Posts
    242

    Re: character movement in Java not working

    The problem is that with limited experience have you groping and improvising, but you should have done, is a small sketch or diagram of the structure of the game, how to conform the class, who inherited the who, what methods to use for collision, for sound, to paste the sprites ... understand? For example, from what little I saw of you project, I saw instructions executed and did not affect the behavior of the program, and possibly on ... But still you have not mentioned the coding for method that makes the character move (called verificarX). I think that you should mention that, so if there is any issue with it, some members will try to change it.

  3. #3
    Join Date
    Jul 2010
    Posts
    38

    Re: character movement in Java not working

    I am sorry for not mentioning that. The following is the code that makes the character move (called verificarX) :
    Code:
    @ Override 
    public void act () { 
    copyboard (); 
    if (up == true) { 
    verificarUp (); 
    / * This.setY (this.getY () - 30); 
    up = false; * / 
    } Else if (left == true) { 
    this.setX (this.getX () - 30); 
    left = false; 
    } Else if (right == true) { 
    this.setX (this.getX () + 30); 
    right = false; 
    } Else if (down == true) { 
    this.setY (this.getY () + 30); 
    down = false; 
    } 
    }
    And the following method for verifying movements:
    Code:
    public void verifyUp () { 
    for (int i = 0; i <array.length; i + +) { 
    for (int j = 0, j <array.length j + +) { 
    if (arr [i] [j] == CHARACTER) { 
    if (arr [i - 1] [j] == BLANK) { 
    this.setY (this.getY () - 30); 
    up = false; 
    array [i - 1] [j] = CHARACTER; 
    Else {} 
    / / To end 
    } 
    Else {} 
    / / To end 
    } 
    } 
    } 
    }
    Well with this and see if it seems right or wrong way to move my character, rather my idea, because the shape still does not work. I encounter a problem ... the level should be square to the array does not? occurred to me that the levels are not as writing a zero square where there is nothing for them and for the matrix square. And if you find me the reason for the malfunction of the code as great, I still continue spinning.

  4. #4
    Join Date
    Apr 2009
    Posts
    68

    Re: character movement in Java not working

    Do a test, if not moving and have reportedly directed the events of keyboard player object by addKeyListener (object), but I think that you do in your code beast plan, implement the methods of capture key KeyPress () and then call the KeyPress () the object player. Try clicking on the Sprite or the player, and then press the keys to see if it responds, it gives me to lose focus, then left to attend the events. I have some examples of 2D games and even the same game that I'm doing, but they must be very old or at least to me it seems they are not object oriented. In addition, many have incorporated levels of programming and that's what I did not want to do and that in the future I might like to add levels.

  5. #5
    Join Date
    Jul 2010
    Posts
    31

    Re: character movement in Java not working

    Even I want some help for the character movement. I've been watching the tutorial to make the sprites and then the character's movement. As for the sprites, it is clear that I have to order my class. Then in the character's movement, it is indeed as you say, from the kind called the keyPressed play the character class. So far my character moves seamlessly and without error. Now I'm trying is to move without crossing the walls. To do what I do to read a file .Txt to the level and according to what you read then fill an array of numbers. Later it traverses the array and if we clicked to where there is a white button (number 1) it can move upward. In this last one is where I get the error, ie does not move even if a target. Does this have anything to do with how I move the character? I think it might not fill either the matrix because making a stepping as variables in the matrix has not taken any security in any position. I am sure that you members will help me too.

  6. #6
    Join Date
    Apr 2009
    Posts
    78

    Re: character movement in Java not working

    Anyway, I think the issue of collisions is better to put them outside of class character, because you realize that while we can get the coordinates of the character and change them, then we can correct the movement before it is selected by the device shown Graphic. It is my opinion, your method is also valid, how many more will. It would be nice to also include the property sprites collide, and that the act or update, the method moves the character, return a value based on the direction you move. In this way the time to correct your path, we would know where to locate ...
    Code:
    ... 
    // Method update () of PlayableCharacter
    public int update () { 
    int status = 0, // also be a good idea to create a property or something ADDRESS 
    if (counter + +> Integer.MAX_VALUE) counter = Integer.MIN_VALUE; 
    if (top == true) {this.setY (this.getY () -10); state + = 1;} 
    if (down == true) {this.setY (this.getY () +10); state + = 2;} 
    if (left == true) {this.setX (this.getX () -10); state + = 4;} 
    if (right == true) {this.setX (this.getX () +10); state + = 8;} 
    return state; 
    } 
    ...

Similar Threads

  1. Replies: 3
    Last Post: 23-09-2011, 02:27 PM
  2. Compass movement to a text game (java)
    By juicer in forum Software Development
    Replies: 1
    Last Post: 08-04-2011, 12:09 AM
  3. Track movement through a camera
    By patelm in forum Technology & Internet
    Replies: 1
    Last Post: 03-02-2010, 02:37 PM
  4. Optical character recognition in java phone
    By Baker in forum Software Development
    Replies: 3
    Last Post: 21-09-2009, 02:49 PM
  5. KVM auto by mouse movement
    By Nurhan in forum Hardware Peripherals
    Replies: 5
    Last Post: 26-02-2009, 10:41 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,719,019,980.56101 seconds with 17 queries