Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (567)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  [Solved] Moving a masive 2D map instead of the character  (Read 3550 times)
0 Members and 1 Guest are viewing this topic.
Offline gerard_pp

Junior Member


Medals: 1



« Posted 2012-04-06 15:18:32 »

*** Topic solved ***

Warning: Plz, excuse my bad english...
Offline _Al3x

Senior Member


Medals: 7


Indie Games FTW!


« Reply #1 - Posted 2012-04-10 21:16:41 »

Hey! Glad it got solved! Smiley
But, would you mind sharing? I wanted to know the answer Sad

Offline gerard_pp

Junior Member


Medals: 1



« Reply #2 - Posted 2012-04-16 19:55:25 »

Hello _Al3x

Sure, here is how i managed to solve this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
//Controls class
public class Controles implements KeyListener {
   protected Applet padre;
   protected String direccion;
         
   public Controles(Applet a){
      direccion = "";
      a.addKeyListener(this);
   }
     
   public void keyTyped(KeyEvent e){}
   public void keyPressed(KeyEvent e) {
      int id = e.getKeyCode();
      switch(id){
         case KeyEvent.VK_UP:
            direccion = "Ar";
            break;
           
         case KeyEvent.VK_DOWN:
            direccion = "Ab";
            break;
         
         case KeyEvent.VK_LEFT:
            direccion = "Iz";
            break;
         
         case KeyEvent.VK_RIGHT:
            direccion = "De";
            break;
      }
   }
   public void keyReleased(KeyEvent e) {
      direccion = "";
   }
   
   //Enviar la direccion de las teclas:
  public String getDireccion(){
      return direccion;
   }
   
}


And then the update implementation:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
public void movimientoGrid(String b){
      b = controls.getDireccion();
                switch(b){
      case "Ar":
         for(int i=0; i<mosagrid.size(); i++){
            mosagrid.get(i).posY += velocidad;
            mosagrid.get(i).paint(pintor);
         }
         break;
      case "Ab":
         for(int i=0; i<mosagrid.size(); i++){
            mosagrid.get(i).posY -= velocidad;
            mosagrid.get(i).paint(pintor);
         }
         break;
      case "Iz":
         for(int i=0; i<mosagrid.size(); i++){
            mosagrid.get(i).posX += velocidad;
            mosagrid.get(i).paint(pintor);
         }
         break;
      case "De":
         for(int i=0; i<mosagrid.size(); i++){
            mosagrid.get(i).posX -= velocidad;
            mosagrid.get(i).paint(pintor);
         }
         break;
      }
   }


salutations

Warning: Plz, excuse my bad english...
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Danny02
« Reply #3 - Posted 2012-04-16 20:08:01 »

would be a lot nicer if you wouldn't use Strings.
Use enums instead.
Offline gerard_pp

Junior Member


Medals: 1



« Reply #4 - Posted 2012-04-16 20:11:13 »

Hello Danny.

Im guessing it is because the numbers can be processed faster than strings. am i right?

Warning: Plz, excuse my bad english...
Offline _Al3x

Senior Member


Medals: 7


Indie Games FTW!


« Reply #5 - Posted 2012-04-16 21:20:41 »

gerard_pp Thank you for sharing, I'll give you a medal right now Smiley

Also, I suggest you code in English, not only for posting code in this forum (or any other) but also to be consistent:

getDireccion? why not obtenerDireccion? But if you change your method/variables/anything else to Spanish, then you'd still get a mix of Spanish and English (for reserved words like if, else, for, string...)

I suggest use only English for code, and then in the outpost choose any language you like:

1  
public void printMessage(String message){ System.out.Print(message) }


Btw, I'm from Argentina Smiley

Offline gerard_pp

Junior Member


Medals: 1



« Reply #6 - Posted 2012-04-16 22:08:08 »

@ _al3x

Wow, thank you very much bro. I appreciate the gesture!!!

I use all my variables in spanish for the sake of consistency. I like to understand everything i do in my code once written. When i post something in the forums i change some of the variables, so ppl can understand a bit.

Warning: Plz, excuse my bad english...
Offline _Al3x

Senior Member


Medals: 7


Indie Games FTW!


« Reply #7 - Posted 2012-04-16 23:40:16 »

I still suggest to move 100% to english, that way you force yourself to write in english. It's a good and sane practice. Imagine you get to work in an international project. It will be easier for you if you're already used to do everything in english.
If you don't know how to name a variable in english because the word just don't come up, use Google Translate. Have any grammar question, Use Spell Check!

That's what I do Smiley

PS: I gave you a medal because I like to encourage people to share code and their solution of propblems Smiley

Offline ra4king

JGO Kernel


Medals: 347
Projects: 3
Exp: 5 years


I'm the King!


« Reply #8 - Posted 2012-04-17 00:06:12 »

There is a much faster and more efficient way of moving the map than looping through and modifying the position of all the tiles when you move. Simply have an xOffset and yOffset variable that you add to the X and Y of all the tiles at render time. Always set the xOffset to screenWidth/2-(player.x+player.width/2) and yOffset to screenHeight/2 - (player.y+player.height/2) to center him.

Offline sproingie

JGO Kernel


Medals: 202



« Reply #9 - Posted 2012-04-17 00:48:48 »

Sticking to your native language for single-developer projects that consist of code you're not already comfortable with is pretty reasonable, especially if that language uses the Roman alphabet anyway.  I'm not too inclined to look at code that's in Chinese though... 

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline _Al3x

Senior Member


Medals: 7


Indie Games FTW!


« Reply #10 - Posted 2012-04-17 00:59:13 »

Sticking to your native language for single-developer projects that consist of code you're not already comfortable with is pretty reasonable, especially if that language uses the Roman alphabet anyway.  I'm not too inclined to look at code that's in Chinese though... 

Sure, but we spanish people use "mañana" for saying "tomorrow". I find it odd to make a method

private string getTomorrow();

like this

private string getManana();

Since the "ñ" isn't in the code language, also the mix of spanish-english "getManana" instead of "obtenerManana" ("get" means "obtener" in spanish). Anyway, if it works, it's ok anyway you do it, you can handle this sort of things later with refactor -> rename (Eclipse) Smiley

Offline sproingie

JGO Kernel


Medals: 202



« Reply #11 - Posted 2012-04-17 01:06:29 »

The default conventions for JavaBeans is to use prefixes of "get" (or "is" for booleans) and "set".  You can change it with BeanInfo, but realistically no one ever does.  Not following the conventions makes life a lot harder if you want to use something like EL or some other scripting language later. 

So yeah you're necessarily going to get your native language entirely, but most English speakers I know can deal with Latin languages.  I had occasion to read code in Polish (or some accent-free approximation thereof) that really made me cock my head sideways like a confused puppy.
Offline StumpyStrust
« Reply #12 - Posted 2012-04-17 06:14:48 »

There is a much faster and more efficient way of moving the map than looping through and modifying the position of all the tiles when you move. Simply have an xOffset and yOffset variable that you add to the X and Y of all the tiles at render time. Always set the xOffset to screenWidth/2-(player.x+player.width/2) and yOffset to screenHeight/2 - (player.y+player.height/2) to center him.

You know ra4king I can not count the number of times I have seen you tell someone to just use an Offset for scrolling. The first time I read one of your posts telling someone to do it I tried it and It works perfectly so....its what I do when working with java2D.

Offline ra4king

JGO Kernel


Medals: 347
Projects: 3
Exp: 5 years


I'm the King!


« Reply #13 - Posted 2012-04-17 11:29:37 »

Yeah I speak from experience too. I used to manually loop through all the Entity's on the screen and modify their location when the player was moved. There were major problems with some maths later on. Using an offset cleanly separates data and view Smiley

Offline gerard_pp

Junior Member


Medals: 1



« Reply #14 - Posted 2012-04-17 14:17:22 »

Quote
Yeah I speak from experience too. I used to manually loop through all the Entity's on the screen and modify their location when the player was moved. There were major problems with some maths later on. Using an offset cleanly separates data and view

i'm only worried about the changes i have to make to the calculations of positioning the character (and other elements) in the grid.

Warning: Plz, excuse my bad english...
Offline _Al3x

Senior Member


Medals: 7


Indie Games FTW!


« Reply #15 - Posted 2012-04-17 14:38:07 »

When he tried to register in the forum he tried "ra4" and then the forum automatically added "king". It must have been for something, right? Smiley

Offline ra4king

JGO Kernel


Medals: 347
Projects: 3
Exp: 5 years


I'm the King!


« Reply #16 - Posted 2012-04-17 17:37:19 »

When he tried to register in the forum he tried "ra4" and then the forum automatically added "king". It must have been for something, right? Smiley

ROFLMAO Grin

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Pippogeek (39 views)
2014-09-24 16:13:29

Pippogeek (30 views)
2014-09-24 16:12:22

Pippogeek (19 views)
2014-09-24 16:12:06

Grunnt (45 views)
2014-09-23 14:38:19

radar3301 (28 views)
2014-09-21 23:33:17

BurntPizza (63 views)
2014-09-21 02:42:18

BurntPizza (33 views)
2014-09-21 01:30:30

moogie (41 views)
2014-09-21 00:26:15

UprightPath (50 views)
2014-09-20 20:14:06

BurntPizza (54 views)
2014-09-19 03:14:18
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!