*** Topic solved ***
Hey! Glad it got solved!
But, would you mind sharing? I wanted to know the answer
Hello _Al3x
Sure, here is how i managed to solve this:
//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:
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
would be a lot nicer if you wouldn’t use Strings.
Use enums instead.
Hello Danny.
Im guessing it is because the numbers can be processed faster than strings. am i right?
gerard_pp Thank you for sharing, I’ll give you a medal right now
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:
public void printMessage(String message){ System.out.Print(message) }
Btw, I’m from Argentina
@ _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.
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
PS: I gave you a medal because I like to encourage people to share code and their solution of propblems
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.
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)
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.
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.
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
[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
[/quote]
i’m only worried about the changes i have to make to the calculations of positioning the character (and other elements) in the grid.
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?
http://www.jejaktrend.com/wp-content/uploads/2012/01/true-story.png
ROFLMAO ;D