Why Code No Work?

Game Editor comments and discussion.

Why Code No Work?

Postby Soullem » Mon Nov 12, 2012 9:14 pm

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???
Current Project:
The Project That needs to be Done -- Pokemon http://game-editor.com/forum/viewtopic.php?f=4&t=12591

Temporarily Abandoned:
Souls of Gustara -- Awesome upgrade blimp game 42%ish
Eggventures of Eggman -- Adventure Game 20%ish
User avatar
Soullem
 
Posts: 105
Joined: Thu Aug 02, 2012 3:12 pm
Score: 5 Give a positive score

Re: Why Code No Work?

Postby skydereign » Tue Nov 13, 2012 12:29 am

That's an odd way to do movement, but it works for me. What combination of keys doesn't it work for you?
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Why Code No Work?

Postby Soullem » Tue Nov 13, 2012 1:05 am

Anything that isn't moving left. Its driving me crazy!!!
Current Project:
The Project That needs to be Done -- Pokemon http://game-editor.com/forum/viewtopic.php?f=4&t=12591

Temporarily Abandoned:
Souls of Gustara -- Awesome upgrade blimp game 42%ish
Eggventures of Eggman -- Adventure Game 20%ish
User avatar
Soullem
 
Posts: 105
Joined: Thu Aug 02, 2012 3:12 pm
Score: 5 Give a positive score

Re: Why Code No Work?

Postby Soullem » Tue Nov 13, 2012 1:19 am

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?
Current Project:
The Project That needs to be Done -- Pokemon http://game-editor.com/forum/viewtopic.php?f=4&t=12591

Temporarily Abandoned:
Souls of Gustara -- Awesome upgrade blimp game 42%ish
Eggventures of Eggman -- Adventure Game 20%ish
User avatar
Soullem
 
Posts: 105
Joined: Thu Aug 02, 2012 3:12 pm
Score: 5 Give a positive score

Re: Why Code No Work?

Postby jimmynewguy » Tue Nov 13, 2012 2:48 pm

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.
Attachments
Movement.zip
(1.8 KiB) Downloaded 106 times
Working on a probably too ambitious project! Wild-west-adventure-RPG-shooter-thing.
User avatar
jimmynewguy
 
Posts: 1137
Joined: Sat Mar 31, 2007 6:27 pm
Score: 89 Give a positive score

Re: Why Code No Work?

Postby Soullem » Sat Nov 17, 2012 10:44 pm

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...
Current Project:
The Project That needs to be Done -- Pokemon http://game-editor.com/forum/viewtopic.php?f=4&t=12591

Temporarily Abandoned:
Souls of Gustara -- Awesome upgrade blimp game 42%ish
Eggventures of Eggman -- Adventure Game 20%ish
User avatar
Soullem
 
Posts: 105
Joined: Thu Aug 02, 2012 3:12 pm
Score: 5 Give a positive score

Re: Why Code No Work?

Postby jimmynewguy » Mon Nov 19, 2012 5:21 am

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:
Working on a probably too ambitious project! Wild-west-adventure-RPG-shooter-thing.
User avatar
jimmynewguy
 
Posts: 1137
Joined: Sat Mar 31, 2007 6:27 pm
Score: 89 Give a positive score

Re: Why Code No Work?

Postby Soullem » Mon Nov 19, 2012 8:56 pm

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.
Current Project:
The Project That needs to be Done -- Pokemon http://game-editor.com/forum/viewtopic.php?f=4&t=12591

Temporarily Abandoned:
Souls of Gustara -- Awesome upgrade blimp game 42%ish
Eggventures of Eggman -- Adventure Game 20%ish
User avatar
Soullem
 
Posts: 105
Joined: Thu Aug 02, 2012 3:12 pm
Score: 5 Give a positive score

Re: Why Code No Work?

Postby jimmynewguy » Tue Nov 20, 2012 3:39 am

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;
}
Attachments
Movement.zip
(6.95 KiB) Downloaded 112 times
Working on a probably too ambitious project! Wild-west-adventure-RPG-shooter-thing.
User avatar
jimmynewguy
 
Posts: 1137
Joined: Sat Mar 31, 2007 6:27 pm
Score: 89 Give a positive score

Re: Why Code No Work?

Postby Soullem » Tue Nov 20, 2012 6:19 pm

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
Current Project:
The Project That needs to be Done -- Pokemon http://game-editor.com/forum/viewtopic.php?f=4&t=12591

Temporarily Abandoned:
Souls of Gustara -- Awesome upgrade blimp game 42%ish
Eggventures of Eggman -- Adventure Game 20%ish
User avatar
Soullem
 
Posts: 105
Joined: Thu Aug 02, 2012 3:12 pm
Score: 5 Give a positive score


Return to GE - General

Who is online

Users browsing this forum: No registered users and 1 guest