Actor moving problem

Non-platform specific questions.

Actor moving problem

Postby schnellboot » Sat Nov 20, 2010 7:55 pm

Hey,
I already coded the part with
a -> actor.x -= 5;
d -> actor.x += 5;
w -> actor.y -= 5;
s -> actor.y += 5;

know if I press d and then w my actor moves x + 5 and y - 5, but I want my actor only move in one direction.
example:
d and then w : x + 5
s and then d : y + 5

how can I do that?
schnellboot
 
Posts: 819
Joined: Sat Mar 31, 2007 1:35 pm
Location: Germany
Score: 102 Give a positive score

Re: Actor moving problem

Postby skydereign » Sat Nov 20, 2010 11:23 pm

Quickest way is to set a lock variable. So create a variable, in my case moving. There are 5 possible values for moving. 0 is no key pressed, 1 is right, 2 is up, 3 is left, 4 is down. On your keydown events, it should look like this.
actor -> Keydown (right) -> Script Editor
Code: Select all
if(moving==0) // no key pressed
{
    x+=5; // switch these to the appropriate x or y
    moving=1; // set this to the appropriate value
}

actor -> Keyup (right) -> Script Editor
Code: Select all
if(moving==1)
{
    moving=0;
}


If you want you can #define or enum the values for left, up, right, and down, so that typing the word left, will be the same as typing 3.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Actor moving problem

Postby schnellboot » Sun Nov 21, 2010 7:21 am

please check this out
[urlhttp://www.multiupload.com/BYG7DONQ3D][/url]

click game game mode and move with w, a, s ,d

coded everything as you told me
schnellboot
 
Posts: 819
Joined: Sat Mar 31, 2007 1:35 pm
Location: Germany
Score: 102 Give a positive score

Re: Actor moving problem

Postby skydereign » Sun Nov 21, 2010 8:19 am

Sorry, forgot the || case. As you noted, it only operates once, and it is because the keydown sets move not to equal zero. With xvelocity and yvelocity you don't need to do that, and originally I was going to suggest using xvelocity and yvelocity (no need for repeat events).
Code: Select all
if(moving==0 || moving==1)
{
    x+=5;
    moving=1;
}
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Actor moving problem

Postby schnellboot » Sun Nov 21, 2010 5:27 pm

thanks, but I tried it out and now I want it like that
if you press d and then w actor should move y-5 how can I do that?
schnellboot
 
Posts: 819
Joined: Sat Mar 31, 2007 1:35 pm
Location: Germany
Score: 102 Give a positive score


Return to General

Who is online

Users browsing this forum: No registered users and 1 guest