/* attempt to deal with prototype, bootstrap, jquery conflicts */ /* for dropdown menus */

\$15

### Image of the Day Submit

IOTD | Top Screenshots

## Why isn't this moving?

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

14 replies to this topic

### #1Programmer557  Members

Posted 19 May 2013 - 03:23 PM

I am trying to create a program that will randomly generate rooms as the user presses the arrow keys, and it's been working fine, but I've hit a bit of a snag.  I'm trying to get the key press commands working, but no matter how long I look at the code, I can't figure out why the little figure I have made isn't moving.

I've included the entire file for the program, but here is where I think the problem is taking place:


while(SDL_PollEvent(&Event))
{
if(Event.type == SDL_KEYDOWN)
{
if (Event.key.keysym.sym)
{
if (SDL_KEYDOWN == SDLK_UP)
{
if (PlayerLocationy == 0)
{
Message = DeniedMessage;
ApplySurface((ScreenWidth - Message->w)/2,(ScreenHeight - Message->h + 420)/2,Message,Screen,NULL);
Message == NULL;
}
else
{
PlayerLocationy =- 42;
ApplySurface(0,421,Sprites,Screen,&TextBox);
}
}
if (SDL_KEYDOWN == SDLK_DOWN)
{
if (PlayerLocationy == 378)
{
Message = DeniedMessage;
ApplySurface((ScreenWidth - Message->w)/2,(ScreenHeight - Message->h + 420)/2,Message,Screen,NULL);
Message == NULL;
}
else
{
PlayerLocationy =+ 42;
ApplySurface(0,421,Sprites,Screen,&TextBox);
}
}
if (SDL_KEYDOWN == SDLK_LEFT)
{
if (PlayerLocationx == 0)
{
Message = DeniedMessage;
ApplySurface((ScreenWidth - Message->w)/2,(ScreenHeight - Message->h + 420)/2,Message,Screen,NULL);
Message == NULL;
}
else
{
PlayerLocationx =- 42;
ApplySurface(0,421,Sprites,Screen,&TextBox);
}
}
if (SDL_KEYDOWN == SDLK_RIGHT)
{
if (PlayerLocationy == 0)
{
Message = DeniedMessage;
ApplySurface((ScreenWidth - Message->w)/2,(ScreenHeight - Message->h + 420)/2,Message,Screen,NULL);
Message == NULL;
}
else
{
PlayerLocationy =+ 42;
ApplySurface(0,421,Sprites,Screen,&TextBox);
}
}
if (SDL_KEYDOWN == SDLK_UP)
{
Message == EscapeMessage;
ApplySurface((ScreenWidth - Message->w)/2,(ScreenHeight - Message->h + 420)/2,Message,Screen,NULL);

if (SDL_KEYDOWN == SDLK_y)
{
QUIT == true;
}
}
}
}

if(Event.type == SDL_QUIT)
{
QUIT = true;
}
}


Any help that could be given would be very much appreciated.  I have a feeling it's something smal like a missing = sign that I can't find, but maybe another programmer would be able to see what I have not.

-Programmer557

### #2Juliean  GDNet+

Posted 19 May 2013 - 03:33 PM

if (Event.key.keysym.sym)
{
if (SDL_KEYDOWN == SDLK_UP)
{

I'm not familiar with SDL, but that just looks wrong. Shouldn't it be more along those lines?

if (Event.key.keysym.sym == SDLK_UP)
{

Right now you are appearantly comparing the event-constant with the key-constant in the latter if, and as for the first one, I don't think that is necessary - is "Event.key.keysym.sym" a pointer? You are basically checking if this variable is any other value than 0, can't tell if this is what you intended or not.

Edited by Juliean, 19 May 2013 - 03:35 PM.

### #3Ectara  Members

Posted 19 May 2013 - 03:42 PM

if (Event.key.keysym.sym)
{
if (SDL_KEYDOWN == SDLK_UP)
{

I'm not familiar with SDL, but that just looks wrong. Shouldn't it be more along those lines?

>if (Event.key.keysym.sym == SDLK_UP)
{

Right now you are appearantly comparing the event-constant with the key-constant in the latter if, and as for the first one, I don't think that is necessary - is "Event.key.keysym.sym" a pointer? You are basically checking if this variable is any other value than 0, can't tell if this is what you intended or not.

Yes, you're correct. SDL_KEYDOWN is a constant used to determine the type of the event.

Event.key.keysym.sym is an integer containing the key symbol representing which key was pressed.

### #4Ectara  Members

Posted 19 May 2013 - 03:49 PM

if (PlayerLocationy == 378)
{
Message = DeniedMessage;
ApplySurface((ScreenWidth - Message->w)/2,(ScreenHeight - Message->h + 420)/2,Message,Screen,NULL);
Message == NULL;
}

if (SDL_KEYDOWN == SDLK_y)
{
QUIT == true;
}

I am almost 100% positive you mean to use a single equals sign here. Double equals compares them for equality, and then the result is discarded here with nothing happening.

else
{
PlayerLocationy =+ 42;
ApplySurface(0,421,Sprites,Screen,&TextBox);
}

else
{
PlayerLocationx =- 42;
ApplySurface(0,421,Sprites,Screen,&TextBox);
}

I can't quite place what language you must be getting this from, but in compound assignment operators, the equals sign always comes last. "PlayerLocationy =+ 42;" is parsed the same as "PlayerLocationy = +42;", which isn't what you wanted. Likewise, "PlayerLocationy =- 42;" is the same as "PlayerLocationy = -42;".

### #5Juliean  GDNet+

Posted 19 May 2013 - 04:28 PM

Also, you should use

int x;

if( x == 0)
{
}
else if(x == 1)
{
}


for mutually exclusive conditions. Or better yet:

int x;

switch(x)
{
case 0:
// ...
break;
case 1:
// ...
break;
}


as long as you have an enumarable variable (which you should have with the SDL-event, just change "x" with Event.type and use "SDL_KEYDOWN" etc... in the "case" statement). Both of these will not only help performance, but also make your code more understandable.

### #6Programmer557  Members

Posted 19 May 2013 - 05:07 PM

I found a little bit later that I had forgotten to flip the screen, but even after adding that the program still doesen't do what I want it to.  Just thought I'd add that before anyone made the comment.

### #7Khatharr  Members

Posted 19 May 2013 - 10:17 PM

I think you're going to save yourself a lot of time, now and in the future, if you immediately remove all the repetition here. As it is, any change you make to the core routine here has to be made 4 times, and that means 4 times the probability that you'll make another mistake.

DRY

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

### #8Six222  Members

Posted 20 May 2013 - 12:30 AM

.

Edited by Six222, 20 May 2013 - 12:33 AM.

### #9Ectara  Members

Posted 20 May 2013 - 10:34 AM

.

That's exactly what I was saying. It was clear that he wanted to add to and subtract from the variables, but the order of the two symbols in compound operators is standardized. The equals sign must come last, otherwise, whenever he moves, it will only allow him to move to -42 or 42 on either axis, which I can guarantee is not what he wanted to do for a room generator, unless he only wants 7 rooms maximum, some he can only visit once.

I'll accept the downvote, anyway.

### #10Ectara  Members

Posted 20 May 2013 - 10:38 AM

if (SDL_KEYDOWN == SDLK_UP)
{
Message == EscapeMessage;
ApplySurface((ScreenWidth - Message->w)/2,(ScreenHeight - Message->h + 420)/2,Message,Screen,NULL);

if (SDL_KEYDOWN == SDLK_y)
{
QUIT == true;
}

}

I just notice on another read-through, that this condition will never be true. A key press event only contains information on one key press; to get another key press, you have to check for the next event. Even if we were checking which key was pressed correctly here, the nested condition will never be true, because we just checked to see if the same value was equal to SDLK_UP before entering the if-block.

### #11Programmer557  Members

Posted 20 May 2013 - 11:13 AM

All right, I've made these slight changes and the sprite moves now, but it will only move down, and it will not move more than once.  The message "You Cannot Move Here" also appears.  This isn't supposed to happen unless the player is at the edge of the screen and tries to move in the direction of the edge.  Here is the altered code:

if (SDLK_UP)
{
if (PlayerLocationy == 0)
{
Message = DeniedMessage;
ApplySurface((ScreenWidth - Message->w)/2,(ScreenHeight - Message->h + 420)/2,Message,Screen,NULL);
Message = NULL;
}
else
{
PlayerLocationy = PlayerLocationy - 42;
ApplySurface(0,421,Sprites,Screen,&TextBox);
}
}

if (SDLK_DOWN)
{
if (PlayerLocationy == 378)
{
Message = DeniedMessage;
ApplySurface((ScreenWidth - Message->w)/2,(ScreenHeight - Message->h + 420)/2,Message,Screen,NULL);
Message = NULL;
}
else
{
PlayerLocationy = PlayerLocationy + 42;
ApplySurface(0,421,Sprites,Screen,&TextBox);
break;
}
}

if (SDLK_LEFT)
{
if (PlayerLocationx == 0)
{
Message = DeniedMessage;
ApplySurface((ScreenWidth - Message->w)/2,(ScreenHeight - Message->h + 420)/2,Message,Screen,NULL);
Message = NULL;
}
else
{
PlayerLocationx = PlayerLocationx - 42;
ApplySurface(0,421,Sprites,Screen,&TextBox);
}
}

if (SDLK_RIGHT)
{
if (PlayerLocationx == 378)
{
Message = DeniedMessage;
ApplySurface((ScreenWidth - Message->w)/2,(ScreenHeight - Message->h + 420)/2,Message,Screen,NULL);
Message = NULL;
}
else
{
PlayerLocationx = PlayerLocationx + 42;
ApplySurface(0,421,Sprites,Screen,&TextBox);
}
}

if (SDLK_ESCAPE)
{
Message = EscapeMessage;
ApplySurface((ScreenWidth - Message->w)/2,(ScreenHeight - Message->h + 420)/2,Message,Screen,NULL);

if (SDL_KEYDOWN == SDLK_y)
{
QUIT = true;
}
}


I noticed that Khatharr had said something about not repeating the same functions, but I don't know how to do that quite yet.

### #12Ectara  Members

Posted 20 May 2013 - 11:22 AM

if (SDLK_UP)

This isn't quite right, either. SDLK_UP, SDLK_DOWN, SDLK_LEFT, SDLK_RIGHT, and SDLK_ESCAPE are non-zero constants; all of these compare true no matter what. You want to test if the key symbol from the event is equal to these constants.

Additionally, you should use if-else-if blocks, because an event cannot be contain than one key press at a time, so it is a waste of time for your machine to check if it is each symbol after it already found which one it is. This would be perfect for a switch statement, if you are proficient in their use.

if (SDLK_ESCAPE) { Message = EscapeMessage; ApplySurface((ScreenWidth - Message->w)/2,(ScreenHeight - Message->h + 420)/2,Message,Screen,NULL); if (SDL_KEYDOWN == SDLK_y) { QUIT = true; } }

There's still this problem, here.

### #13Khatharr  Members

Posted 20 May 2013 - 07:58 PM

This code is just in response to what you asked me about. There's still problems here that other people have touched on. Additionally, your render code seems too close to your logic. You should try to separate these into different sections unless the program structure is exceedingly simplistic.
const int MOVE_DIST = 42;
const int BOUNDARY_MIN = 0;
const int BOUNDARY_MAX = 378;

if(SDLK_UP)    {PlayerLocationy -= MOVE_DIST;}
if(SDLK_DOWN)  {PlayerLocationy += MOVE_DIST;}
if(SDLK_LEFT)  {PlayerLocationx -= MOVE_DIST;}
if(SDLK_RIGHT) {PlayerLocationx += MOVE_DIST;}

//Correct out-of-bounds condition
Message = NULL;
if(PlayerLocationy < BOUNDARY_MIN) {
Message = DeniedMessage;
PlayerLocationy = BOUNDARY_MIN;
}
if(PlayerLocationy > BOUNDARY_MAX) {
Message = DeniedMessage;
PlayerLocationy = BOUNDARY_MAX;
}
if(PlayerLocationx < BOUNDARY_MIN) {
Message = DeniedMessage;
PlayerLocationx = BOUNDARY_MIN;
}
if(PlayerLocationy > BOUNDARY_MAX) {
Message = DeniedMessage;
PlayerLocationx = BOUNDARY_MAX;
}

//Render
if(Message != NULL) {
int mess_x = (ScreenWidth - Message->w) / 2;
//This next line is not clear. Get rid of the magic number and clarify order of operations
int mess_y = (ScreenHeight - Message->h + 420) / 2;

ApplySurface(mess_x,mess_y,Message,Screen,NULL);

Message = NULL;
}
else {
ApplySurface(0,421,Sprites,Screen,&TextBox);
}


The key here is that the things you're likely to change are easy to change and it's clear what's going on. You can change the constants at the top or the render stuff at the bottom and not have to worry about changing it all 4 times. The organization and alignment of the first set of tests also makes it easier to spot if there's any errors lurking there, such as having PlayerLocationy where there should be a PlayerLocationx, or a - where there should be a +.

Edited by Khatharr, 20 May 2013 - 08:18 PM.

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

### #14Ectara  Members

Posted 20 May 2013 - 08:47 PM

Additionally, your render code seems too close to your logic. You should try to separate these into different sections unless the program structure is exceedingly simplistic.

+1 for separation between logic and rendering.

### #15Programmer557  Members

Posted 21 May 2013 - 08:45 AM

This code is just in response to what you asked me about. There's still problems here that other people have touched on. Additionally, your render code seems too close to your logic. You should try to separate these into different sections unless the program structure is exceedingly simplistic.

const int MOVE_DIST = 42;
const int BOUNDARY_MIN = 0;
const int BOUNDARY_MAX = 378;

if(SDLK_UP)    {PlayerLocationy -= MOVE_DIST;}
if(SDLK_DOWN)  {PlayerLocationy += MOVE_DIST;}
if(SDLK_LEFT)  {PlayerLocationx -= MOVE_DIST;}
if(SDLK_RIGHT) {PlayerLocationx += MOVE_DIST;}

//Correct out-of-bounds condition
Message = NULL;
if(PlayerLocationy < BOUNDARY_MIN) {
Message = DeniedMessage;
PlayerLocationy = BOUNDARY_MIN;
}
if(PlayerLocationy > BOUNDARY_MAX) {
Message = DeniedMessage;
PlayerLocationy = BOUNDARY_MAX;
}
if(PlayerLocationx < BOUNDARY_MIN) {
Message = DeniedMessage;
PlayerLocationx = BOUNDARY_MIN;
}
if(PlayerLocationy > BOUNDARY_MAX) {
Message = DeniedMessage;
PlayerLocationx = BOUNDARY_MAX;
}

//Render
if(Message != NULL) {
int mess_x = (ScreenWidth - Message->w) / 2;
//This next line is not clear. Get rid of the magic number and clarify order of operations
int mess_y = (ScreenHeight - Message->h + 420) / 2;

ApplySurface(mess_x,mess_y,Message,Screen,NULL);

Message = NULL;
}
else {
ApplySurface(0,421,Sprites,Screen,&TextBox);
}


The key here is that the things you're likely to change are easy to change and it's clear what's going on. You can change the constants at the top or the render stuff at the bottom and not have to worry about changing it all 4 times. The organization and alignment of the first set of tests also makes it easier to spot if there's any errors lurking there, such as having PlayerLocationy where there should be a PlayerLocationx, or a - where there should be a +.

Thank you very much.  With some slight modification, my program is now working how I want it to.  Thank you also, everyone else who posted.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.