So, I'm trying to move a sprite across the screen and using a do-while loop. But the sprite isn't moving. Here's my code:

Code:
 //move train across screen and define main menu
    train_x = -156;
    train_y = 110;
    do {
        train_x += 10;
        gfx_TransparentSprite(train, train_x, train_y);
        if (train_x = 326)
            train_x = -156;
    }while (kb_ScanGroup(kb_group_1) != kb_2nd);

Help! The sprite is 156 px long so that's why train_x is -156. It starts off the screen and goes right.
train_x = 326 means that the variable train_x gets set to 326. (Because the result of that expression is the non-zero value 326, the if statement is always true, and train_x always gets set to -156.) If you want to compare train_x to 326, use == instead of =.
Yes, its definitely the (train_x = 326). It should be (train_x == 326). Yeah, its really annoying. It gets me all the time. Sad
Well it's actually 324 because going from negative to positive adding 10 goes from the ones digit being 6 to 4.
So, I'm gonna post a Gif soon however I need to know one more thing. I have a menu screen with New Game, Load Game, Settings, and Quit. ">" is moved up and down every time you press the Power(up) or Divide(down) keys(however, I'm hoping that I can switch that to the Up and Down keys), but my problem is that sometimes it moves down two, or up two and things like that. I want it to only move one. Does anyone know what I should do?

Edit:
Basically, I want to know how to turn off sticky keys in C. And turn it back on for that matter.
Also, I know you have to use a url to post a gif on here, but how would I make a url for it?
If you are using the keypadc library you have to add a delay after a key is pressed.
The code below is an example of how you could do that.

Code:
if(key){  //A key was pressed.
   int i;
   for(i=0;i<400 && kb_ScanGroup(kb_group_7);i++){} //Change kb_group_7 to the key group that you want to add a delay to. Change i<400 to make it slower/faster
}

Or you can use os_GetCSC(); instead of the keypadc library, that is slower so you don't have to add a delay.
I finally have the main menu done. Yay. Tell me what you think.
So, I have the entire settings menu created. The whole thing works except for the fact when I put it in CEmu, it returns ERROR: MEMORY. I don't know why. I checked for infinite loops, even optimized the settings() just in case. CEmu still has over 30kb of memory so that's not an issue. I'm lost. Help.
Try resetting/reloading the calc, and if that doesnt work may sure that there is RAM available as well, just in case your program is to big. (though I doubt it is)
Unicorn wrote:
Try resetting/reloading the calc, and if that doesnt work may sure that there is RAM available as well, just in case your program is to big. (though I doubt it is)


Thanks. That worked. Now, it's either CEmu that's giving me a big deal about some code, or it's my computer. But it works so I'll port it to my physical CE and see how that goes.
This is a problem I'm not experienced enough to fix. Anyone who knows char, string, and pointers well, please help.

Code:
startcity = (SFlags);
    *cityString = "<   Chicago   >";
    do {
        gfx_SetDraw(gfx_buffer);
        gfx_FillScreen(0x83);
        gfx_PrintStringXY("Start City:", 20, 10);
        gfx_PrintStringXY(*cityString, 100, 10);
        key = os_GetCSC();
        switch (key) {
            case 2:         //left
                switch (startcity) {
                    case (chicago):
                        *cityString = "<   Boston   >";
                        startcity = (SFlags ^ (boston | chicago));
                        break;
                    case (NYC):
                        *cityString = "<   Chicago   >";
                        startcity = (SFlags ^ (chicago | NYC));
                        break;
                    case (stLouis):
                        *cityString = "<   NYC   >";
                        startcity = (SFlags ^ (NYC | stLouis));
                        break;
                    case (boston):
                        *cityString = "<   St. Louis   >";
                        startcity = (SFlags ^ (stLouis | boston));
                        break;
                }
            case 3:         //right
                switch (startcity) {
                    case (chicago):
                        *cityString = "<   NYC  >";
                        startcity = (SFlags ^ (NYC | chicago));
                        break;
                    case (NYC):
                        *cityString = "<   St. Louis   >";
                        startcity = (SFlags ^ (stLouis | NYC));
                        break;
                    case (stLouis):
                        *cityString = "<   Boston   >";
                        startcity = (SFlags ^ (boston | stLouis));
                        break;
                    case (boston):
                        *cityString = "<   Chicago   >";
                        startcity = (SFlags ^ (chicago | boston));
                        break;
                }
        }
        gfx_SwapDraw();
    } while (os_GetCSC() != 54);
    return startcity;


For reference, SFlags is my settings bit flags. The XOR operand is used so that the SFlags carry's the new startcity, and get's rid of the old one per the Bitwise OR operator.

There also seems to be something wrong dealing with the os_GetCSC(), because I have to hit the button multiple times before it actually works. Any help is appreciated. Smile
I think I will translate some TI-Basic code I have to C and use string.h to fix the problem.
Nevermind. I figured out the problem.

New Problems:

1. os_GetCSC won't read the keypress every time I press a key. It takes a couple clicks. I don't know why.

2. Clicking the left button doesn't work. It won't switch the strings if I click it, even multiple times.

Here is the code and a screenshot:


When the strings switch, I'm pressing the right button, when it stops at NYC, I'm clicking the left button.


Code:
uint8_t settings() {
    startcity = (SFlags);
    cityString = "<   Chicago   >";
    do {
        gfx_SetDraw(gfx_buffer);
        gfx_FillScreen(0x83);
        gfx_PrintStringXY("Start City:", 20, 10);
        gfx_PrintStringXY(cityString, 100, 10);
        key = os_GetCSC();
        switch (key) {
        case 2:         //left
            switch (startcity) {
            case chicago:
                cityString = "<   Boston   >";
                startcity ^= (boston | chicago);
                break;
            case NYC:
                cityString = "<   Chicago   >";
                startcity ^= (chicago | NYC);
                break;
            case stLouis:
                cityString = "<   NYC   >";
                startcity ^= (NYC | stLouis);
                break;
            case boston:
                cityString = "<   St. Louis   >";
                startcity ^= (stLouis | boston);
                break;
            }
        case 3:         //right
            switch (startcity) {
            case chicago:
                cityString = "<   NYC  >";
                startcity ^= (NYC | chicago);
                break;
            case NYC:
                cityString = "<   St. Louis   >";
                startcity ^= (stLouis | NYC);
                break;
            case stLouis:
                cityString = "<   Boston   >";
                startcity ^= (boston | stLouis);
                break;
            case boston:
                cityString = "<   Chicago   >";
                startcity ^= (chicago | boston);
                break;
            }
        }
        gfx_SwapDraw();
    } while (os_GetCSC() != 54);
    return startcity;
}
seanlego23 wrote:
1. os_GetCSC won't read the keypress every time I press a key. It takes a couple clicks. I don't know why.

You are constantly redrawing the screen, so when you press a key it doesn't respond cause it is drawing everything to the screen at that moment. You can solve this by only redraw everything to the screen and swapping the buffers when you pressed a key.

seanlego23 wrote:
2. Clicking the left button doesn't work. It won't switch the strings if I click it, even multiple times.

Maybe because you forgot break; in you key switch.
Rico wrote:
seanlego23 wrote:
1. os_GetCSC won't read the keypress every time I press a key. It takes a couple clicks. I don't know why.

You are constantly redrawing the screen, so when you press a key it doesn't respond cause it is drawing everything to the screen at that moment. You can solve this by only redraw everything to the screen and swapping the buffers when you pressed a key.

That's a good idea. I'll see if it works. Thanks.

Quote:
seanlego23 wrote:
2. Clicking the left button doesn't work. It won't switch the strings if I click it, even multiple times.

Maybe because you forgot break; in you key switch.

But then why does 'right' work? Is it because it's at the end of the code. I'll try it and see.
The clicking the left button now works, however os_GetCSC still has a problem of not registering every click. Since this is not a major problem, I will continue on coding. If anyone knows the answer, please let me know. Thanks.
seanlego23 wrote:
The clicking the left button now works, however os_GetCSC still has a problem of not registering every click. Since this is not a major problem, I will continue on coding. If anyone knows the answer, please let me know. Thanks.

Does it work if you do it like this:

Code:
uint8_t settings() {
   uint8_t key = 1;
   startcity = (SFlags);
    cityString = "<   Chicago   >";
   
   gfx_SetDraw(gfx_buffer);
   
   while(key != 54) {
        if(key){ //key was pressed; redraw the screen
         gfx_FillScreen(0x83);
         gfx_PrintStringXY("Start City:", 20, 10);
         gfx_PrintStringXY(cityString, 100, 10);
         gfx_SwapDraw();
        }
      
        key = os_GetCSC();
      
        if(key == 0x02){   //left
            switch (startcity) {
            case chicago:
                cityString = "<   Boston   >";
                startcity ^= (boston | chicago);
                break;
            case NYC:
                cityString = "<   Chicago   >";
                startcity ^= (chicago | NYC);
                break;
            case stLouis:
                cityString = "<   NYC   >";
                startcity ^= (NYC | stLouis);
                break;
            case boston:
                cityString = "<   St. Louis   >";
                startcity ^= (stLouis | boston);
                break;
            }
        }
      
        if(key == 0x03){   //right
            switch (startcity) {
            case chicago:
                cityString = "<   NYC  >";
                startcity ^= (NYC | chicago);
                break;
            case NYC:
                cityString = "<   St. Louis   >";
                startcity ^= (stLouis | NYC);
                break;
            case stLouis:
                cityString = "<   Boston   >";
                startcity ^= (boston | stLouis);
                break;
            case boston:
                cityString = "<   Chicago   >";
                startcity ^= (chicago | boston);
                break;
            }
        }
       
    }
    return startcity;
}
?
I'm going to suggest this: Try to use the libraries's keypad routines. They make things pretty easy, and are known to work. Wink
Rico wrote:
seanlego23 wrote:
The clicking the left button now works, however os_GetCSC still has a problem of not registering every click. Since this is not a major problem, I will continue on coding. If anyone knows the answer, please let me know. Thanks.

Does it work if you do it like this:

Code:
uint8_t settings() {
   uint8_t key = 1;
   startcity = (SFlags);
    cityString = "<   Chicago   >";
   
   gfx_SetDraw(gfx_buffer);
   
   while(key != 54) {
        if(key){ //key was pressed; redraw the screen
         gfx_FillScreen(0x83);
         gfx_PrintStringXY("Start City:", 20, 10);
         gfx_PrintStringXY(cityString, 100, 10);
         gfx_SwapDraw();
        }
      
        key = os_GetCSC();
      
        if(key == 0x02){   //left
            switch (startcity) {
            case chicago:
                cityString = "<   Boston   >";
                startcity ^= (boston | chicago);
                break;
            case NYC:
                cityString = "<   Chicago   >";
                startcity ^= (chicago | NYC);
                break;
            case stLouis:
                cityString = "<   NYC   >";
                startcity ^= (NYC | stLouis);
                break;
            case boston:
                cityString = "<   St. Louis   >";
                startcity ^= (stLouis | boston);
                break;
            }
        }
      
        if(key == 0x03){   //right
            switch (startcity) {
            case chicago:
                cityString = "<   NYC  >";
                startcity ^= (NYC | chicago);
                break;
            case NYC:
                cityString = "<   St. Louis   >";
                startcity ^= (stLouis | NYC);
                break;
            case stLouis:
                cityString = "<   Boston   >";
                startcity ^= (boston | stLouis);
                break;
            case boston:
                cityString = "<   Chicago   >";
                startcity ^= (chicago | boston);
                break;
            }
        }
       
    }
    return startcity;
}
?

No it doesn't. It clears out after one execution.

Unicorn wrote:
I'm going to suggest this: Try to use the libraries's keypad routines. They make things pretty easy, and are known to work. Wink

The keypadc library doesn't reset after 1 click of a button. If I click 2nd to exit "this" function, and the next function executed has an exit routine of hitting 2nd, then 1 of 2 things happen. If it's a while loop, it executes 0 times. If it's a do-while loop, it executes 1 time.
Is it possible to have a pointer array that holds a string be initialized to a variable?
Example:
const char* c[] = "< Chicago >";
[...]
char* citystring;
[...]
citystring = c;

So far, it hasn't worked for me. Anyone know any solution?
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 2 of 3
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement