Page 1 of 1

Why Code No Work?

PostPosted: Mon Nov 12, 2012 9:14 pm
by Soullem
I have this movement code for my player:
Code: Select all
int dir;
char*key=GetKeyState();
int tim;
int move;
int mov[4];

mov[0]=key[KEY_RIGHT];
mov[1]=key[KEY_LEFT];
mov[2]=key[KEY_UP];
mov[3]=key[KEY_DOWN];

dir = mov[0]-mov[1]+10*(mov[2]-mov[3]);
if (tim<8)
{
tim+=1;
}
if (tim==8)
{
    move=1;
    tim=0;
}
if (move ==1)
{
    switch(dir)
    {
        case 1:
            x+=width;
        break;
        case -1:
            x-=width;
        break;
        case 10:
            y-=height;
        break;
        case -10:
            y+=height;
        break;
    }
move=0;
}


But for some reason I can only move left... And I have no idea why. Can you help???

Re: Why Code No Work?

PostPosted: Tue Nov 13, 2012 12:29 am
by skydereign
That's an odd way to do movement, but it works for me. What combination of keys doesn't it work for you?

Re: Why Code No Work?

PostPosted: Tue Nov 13, 2012 1:05 am
by Soullem
Anything that isn't moving left. Its driving me crazy!!!

Re: Why Code No Work?

PostPosted: Tue Nov 13, 2012 1:19 am
by Soullem
When I moved the event off of the filled region to the visual it worked. I also changed it and added this so when he is moving and I press a direction he keeps on moving...
Code: Select all
if (R==1)
{
    L=0;
    U=0;
    D=0;
}
if (U==1)
{
    R=0;
    L=0;
    D=0;
}
if (L==1)
{
    U=0;
    R=0;
    D=0;
}
if (D==1)
{
    L=0;
    U=0;
    R=0;
}

Do you know a shorter way to do this???
making the final code this...

Code: Select all
int dir;
int tim;
int move;
char*key=GetKeyState();
int R;
int L;
int U;
int D;
R=key[KEY_RIGHT];
L=key[KEY_LEFT];
U=key[KEY_UP];
D=key[KEY_DOWN];
if (R==1)
{
    L=0;
    U=0;
    D=0;
}
if (U==1)
{
    R=0;
    L=0;
    D=0;
}
if (L==1)
{
    U=0;
    R=0;
    D=0;
}
if (D==1)
{
    L=0;
    U=0;
    R=0;
}
dir = R-L+10*(U-D);
switch(dir)
{
    case 1:
        ChangeAnimation("Event Actor", "PokemonGuyWalkRight", NO_CHANGE);
        x+=32;
    break;
    case -1:
        ChangeAnimation("Event Actor", "PokemonGuyWalkLeft", NO_CHANGE);
        x-=32;
    break;
    case 10:
        ChangeAnimation("Event Actor", "PokemonGuyWalkUp", NO_CHANGE);
        y-=32;
    break;
    case -10:
        ChangeAnimation("Event Actor", "PokemonGuyWalkDown", NO_CHANGE);
        y+=32;
    break;
}



But this defeats my purpose. I want to make him move one square at a time, but with a little time before he moves to the next square. Like a little bit of lag. Do you know what i'm saying?

Re: Why Code No Work?

PostPosted: Tue Nov 13, 2012 2:48 pm
by jimmynewguy
You'll have to count and do the actual movement every couple of frames or so, otherwise the movement won't "lag" like you want it too. I stored the last pressed key in newKey on the keydown event and then every frame made the pastKey equivalent to the newKey so you can compare them before doing actions.
Code: Select all
newKey = getLastKey();//last key pressed

Then I checked what keys were still held down whenever a key was released, and only if there was a combination where two opposing directions were not held would I change the newKey in order to have continuous movement without having to only press one key at a time.
Code: Select all
char * key = GetKeyState();
if((key[KEY_RIGHT] ^ key[KEY_LEFT]) ^ (key[KEY_DOWN] ^ key[KEY_UP]))//keys in unopposed directions
{
if(key[KEY_UP])
newKey = KEY_UP;
else if(key[KEY_DOWN])
newKey = KEY_DOWN;
else if(key[KEY_LEFT])
newKey = KEY_LEFT;
else if(key[KEY_RIGHT])
newKey = KEY_RIGHT;
}

Then on the draw actor event, I checked if one of the movement keys was still being held and increased a timer (tick) by once every frame if the newKey and the pastKey were equal so we don't get sudden movement when switching directions and otherwise reset the variable to 0. Then if tick had "counted" 10 frames I would check the direction using newKey since it was our most recent direction and move that way.
Code: Select all
char * key = GetKeyState();
moveing = (key[KEY_RIGHT] - key[KEY_LEFT]) || (key[KEY_DOWN] - key[KEY_UP]);// are we moving
tick = (newKey == pastKey && moveing) ? (tick+= 1):(tick = 0);//update tick
if(tick >= 10)//every 10 frames of moving in the same direction
{
switch(newKey)//find what direction and move there
{
case KEY_UP: y -= 16;
break;
case KEY_DOWN: y += 16;
break;
case KEY_LEFT: x -= 16;
break;
case KEY_RIGHT: x += 16;
break;
}
tick = 0;
}
pastKey = newKey;//pastKey updated


Demo attached of this working.

Re: Why Code No Work?

PostPosted: Sat Nov 17, 2012 10:44 pm
by Soullem
Thats great, but how do I then round the Player to a 32 by 32 grid. Everything ive tried so far messes it up when you hold down 2 keys with the right(or wrong) timing. also in your code you used ?. What does that mean.

Thanks for your help, I just cant find out how to round it...

Re: Why Code No Work?

PostPosted: Mon Nov 19, 2012 5:21 am
by jimmynewguy
This will round the coordinates, just do it on the create actor since the movement is grid-based
Code: Select all
x = round(x/32)*32;
y = round(x/32)*32;


the '?' is the ternary operator
http://en.wikipedia.org/wiki/%3F:

Re: Why Code No Work?

PostPosted: Mon Nov 19, 2012 8:56 pm
by Soullem
Ive done that, but the thing is is that I want him to move seamlessly from one grid to the next, and if he is not moving then he will be perfectly in the grid. This code makes him snap to the next square, not move to it.

Re: Why Code No Work?

PostPosted: Tue Nov 20, 2012 3:39 am
by jimmynewguy
Just add two variables dirY and dirX that count down as the movement goes, like this:
Code: Select all
if(tick >= 10)
{
switch(newKey)
{
case KEY_UP: dirY -= 32;
break;
case KEY_DOWN: dirY += 32;
break;
case KEY_LEFT: dirX -= 32;
break;
case KEY_RIGHT: dirX += 32;
break;
}
tick = 0;
}
pastKey = newKey;

if(dirX < 0)
{
x -= 2; dirX += 2;
}
else if(dirX > 0)
{
x += 2; dirX -= 2;
}
if(dirY < 0)
{
y -= 2; dirY += 2;
}
else if(dirY > 0)
{
y += 2; dirY -= 2;
}

Re: Why Code No Work?

PostPosted: Tue Nov 20, 2012 6:19 pm
by Soullem
Thank you so much +1 to you. I changed it a little bit to fix some diagonal movement and now it is working perfectly. Thanks a ton!! :D