A Flash Developer Resource Site

Results 1 to 17 of 17

Thread: [F5] Diagonal Movement with Keyboard

  1. #1
    Junior Member
    Join Date
    Aug 2007
    Posts
    3

    [F5] Diagonal Movement with Keyboard

    Hi everyone! I'm relatively new to Flash Kit [as a member I mean; I've been using it though for a year or two now]. Anyway, I've taken to the task of creating my first game. I'd call it, I guess, for lack of a better term, a first person sniping game. The rifle stays fixed and the background moves [that's why my code is "backwards"]. But that's not my question. What I'm wondering is how I can move diagonally. Here's my current code for left, right, up and down and it's pretty basic [I'm using Flash 4; sorry I don't have an updated version but I don't think my parents will let me spend a couple 100 dollars just for a computer program ]:

    On (Key: <Down>)
    Set Property ("/screen", Y Position) = GetProperty ("/screen", _y) - 10
    End On
    On (Key: <Up>)
    Set Property ("/screen", Y Position) = GetProperty ("/screen", _y) + 10
    End On
    On (Key: <Right>)
    Set Property ("/screen", X Position) = GetProperty ("/screen", _x) - 10
    End On
    On (Key: <Left>)
    Set Property ("/screen", X Position) = GetProperty ("/screen", _x) + 10
    End On


    I was wondering if the On Key function allowed 2 keys to be pressed at once. If not, is it possible to move diagonally any other way?

    I'm sorry if this is a newbie question but I haven't had much ActionScripting experience prior [I'm usually the graphics guy], but please help. Thanks in advance.

  2. #2
    Member
    Join Date
    Dec 2005
    Location
    Adelaide, South Australia
    Posts
    63
    Hi,

    Ive never seen the kind of syntax your using, in fact im not even sure 'On (Key: <Down>)" is even actionscript.

    Usually when im working with key downs I use something like this:

    Code:
    keyRIGHT = Key.isDown(Key.RIGHT);
    keyUP = Key.isDown(Key.UP);
    
    if (keyRIGHT and !keyUP) {
    //move right
    }else (!keyRIGHT and keyUP) {
    //move up
    }else if (keyUP and keyRIGHT
    //move up-right
    }else (etc....
    or something along those lines.

    Also instead of trying to move the actual screen (were you???), try creating a movieclip background you can scroll around with the _x and _y properties.

    eg.
    backGroundMC._x += moveSpeed;

    Hopefully that helps, good luck with making some games

    EDIT: oops, sorry. Got confused with the
    [F5] Diagonal Movement with Keyboard
    , I posted flash 5 code before I realised your only using 4...
    Unlucky mate, no wonder I didn't recognize the script.
    Last edited by Thomas Aquinas; 08-09-2007 at 12:14 PM. Reason: mistaken flash version

  3. #3
    Junior Member
    Join Date
    Aug 2007
    Posts
    3
    Okay, maybe I didn't explain well enough...

    1. I am using FLASH 4 ActionScript
    2. No, I wasn't attempting to move the actual stage itself. I made the background a movie clip (instance name "screen") with a button in it to handle the ActionScript.

    Basically, what I'm asking is how do I get Flash 4 to respond to multiple keys being pressed down at the same time?

    *EDIT* Sorry about the "[F5]"...I chose the radio button of "Flash 5 or earlier" but it only says F5...my first time using the forums...please don't hurt me
    Last edited by Wolfie001; 08-09-2007 at 12:20 PM.

  4. #4
    Senior Member
    Join Date
    Apr 2005
    Posts
    467
    well to move diagonaly means to increase/decrease the same mc._x and mc._y value at the same time, so if you press up and right key, that should do, for instance.

    But men, Flash 4, fair play to ya, that brought memories and tears in my eyes, like the sound of choppers and the smel of napalm in the early nam morning.
    Hope nobody knows I am still on Flash 5
    ______________________________________
    All artists are prepared to suffer for their work
    but why are so few prepared to learn to draw?(Banksy)

  5. #5
    Senior Member
    Join Date
    Jan 2005
    Posts
    107
    how did you even get flash 4

    if as1 started with flash 5, what is that your using.

  6. #6
    Student
    Join Date
    Apr 2001
    Location
    -
    Posts
    4,756
    Quote Originally Posted by artlink
    well to move diagonaly means to increase/decrease the same mc._x and mc._y value at the same time, so if you press up and right key, that should do, for instance.
    No,- it means walking in 45°,- wich is not the same same powers on booth axis just as it would be on forward.


    but bleh- flash 4 I guess I am not going to help you then (that´s almost 8 years ago since I touched that application)

  7. #7
    Feeling adventurous? T1ger's Avatar
    Join Date
    Mar 2004
    Posts
    850
    What a horrible, horrible syntax! You should at least go up to standard AS1 syntax if you want people to help you.

    Which means:
    screen._y -= 10;
    instead of:
    Set Property ("/screen", Y Position) = GetProperty ("/screen", _y) - 10

    I feel im already bonded to AS3..
    I don't have a photograph, but you can have my footprints. They're upstairs in my socks.

  8. #8
    Developer
    Join Date
    Apr 2007
    Location
    UK
    Posts
    324
    That old syntax is like applescript lol. I cant believe Flash users HAD to code like that...

  9. #9
    crossconscious
    Join Date
    Sep 2005
    Location
    Belgium
    Posts
    1,188
    So, it wasn't just nightmares I was having, that kind of code WAS real.

    Now I'm scared. Someone please hold me...

  10. #10
    Developer
    Join Date
    Apr 2007
    Location
    UK
    Posts
    324
    Quote Originally Posted by Fall_X
    Now I'm scared. Someone please hold me...
    Haha I think I'll pass

  11. #11
    Hype over content... Squize's Avatar
    Join Date
    Apr 2001
    Location
    Lost forever in a happy crowd...
    Posts
    5,926
    When I first started with Flash, Flash 5 ( And dot syntax ) was just coming out, but all the tutorials were still Flash 4.
    I think that's why it pains me so much when people ask for a tutorial on a multiplayer RPG game. In 3D. Like there should be a tutorial for everything they could ever want to write.

    Anyway, sorry Wolfie I don't know how much help we're going to be able to give you mate. Lot's of people here weren't even using F4, and those of us who did can't really remember how to code that way.
    I know it's an easy thing to say, but you really should look to upgrade. You're always going to struggle like hell coding like that, it's just making a hard job a lot harder.

    Squize.

  12. #12
    Senior Member
    Join Date
    Apr 2005
    Posts
    467
    Quote Originally Posted by renderhjs
    No,- it means walking in 45°


    if you move 5 pixels left or right and 5 pixels up or down then you are walking diagonally. If not you got to tell me because I've been moving my MCs like that for the last 8 years and strangely they moved diagonally.

    Is it a Flash5 bug?
    Hope nobody knows I am still on Flash 5
    ______________________________________
    All artists are prepared to suffer for their work
    but why are so few prepared to learn to draw?(Banksy)

  13. #13
    Student
    Join Date
    Apr 2001
    Location
    -
    Posts
    4,756
    @artlink:
    see this:

    it´s howeworld but basicly what I meant,- you need to apply trigonometry for doing a propper direction esspecially when moving Diagonal,- because the x/y offsets are different as if the player would just move right/left/up/down.

    For the very same reason people cheated for years on many FPS shooters they just walked diagonally to move faster as the game creators didn´t implemented walk movement propper.

  14. #14
    Senior Member
    Join Date
    Apr 2005
    Posts
    467
    Quote Originally Posted by renderhjs
    @artlink:
    see this: it´s howeworld
    yep, I don't have a problem with that, but that's moving with the mouse not with the keyboard which is what has been asked, and the only way to move a mc with the keyboard in diagonal is by combining up/down with left/right.

    No?
    Hope nobody knows I am still on Flash 5
    ______________________________________
    All artists are prepared to suffer for their work
    but why are so few prepared to learn to draw?(Banksy)

  15. #15
    Student
    Join Date
    Apr 2001
    Location
    -
    Posts
    4,756
    Quote Originally Posted by artlink
    yep, I don't have a problem with that, but that's moving with the mouse not with the keyboard which is what has been asked, and the only way to move a mc with the keyboard in diagonal is by combining up/down with left/right.No?
    hopefully this explains better what I mean:
    it´s just that with equal values as moving not- diagonally the character moves faster because the squareroot distance is higher (see pythagoras equation d=sqrt(dx²+dy²) ).
    That way if one would do it propper the character would move not the way I understood you.

    Grid or tilestuff is a different matter but still it would mean that the character moves faster when moving diagonally

  16. #16
    Senior Member
    Join Date
    Apr 2005
    Posts
    467
    ok, got your point.

    Oh my god I've been walking too fast all those years...no wonder I feel so tired...
    Hope nobody knows I am still on Flash 5
    ______________________________________
    All artists are prepared to suffer for their work
    but why are so few prepared to learn to draw?(Banksy)

  17. #17
    Senior Member
    Join Date
    Aug 2007
    Posts
    291
    The only thing I could suggest is to use different keys instead of the arrow keys. Set it up to read something like

    code:
    On (Key: <X>)
    Set Property ("/screen", Y Position) = GetProperty ("/screen", _y) - 10
    End On
    On (Key: <W>)
    Set Property ("/screen", Y Position) = GetProperty ("/screen", _y) + 10
    End On
    On (Key: `<D>)
    Set Property ("/screen", X Position) = GetProperty ("/screen", _x) - 10
    End On
    On (Key: <A>)
    Set Property ("/screen", X Position) = GetProperty ("/screen", _x) + 10
    End On
    On (Key: <Q>)
    Set Property ("/screen", X Position) = GetProperty ("/screen", _x) + 5
    Set Property ("/screen", Y Position) = GetProperty ("/screen", _y) + 5
    End On
    On (Key: <E>)
    Set Property ("/screen", X Position) = GetProperty ("/screen", _x) - 5
    Set Property ("/screen", Y Position) = GetProperty ("/screen", _y) + 5
    End On
    On (Key: <Z>)
    Set Property ("/screen", X Position) = GetProperty ("/screen", _x) + 5
    Set Property ("/screen", Y Position) = GetProperty ("/screen", _y) - 5
    End On
    On (Key: <C>)
    Set Property ("/screen", X Position) = GetProperty ("/screen", _x) - 5
    Set Property ("/screen", Y Position) = GetProperty ("/screen", _y) - 5
    End On



    I hardly know anything about flash 4 anymore, so I don't even know if those keys can be used in that way... I would assume they can, but that would be the way to do it. You may want to find some kind of math function to figure out the movement speed a little better than what I did there.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center