#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2006
    Location
    America
    Posts
    146
    Rep Power
    8

    Question RPG walking in Flash games


    Hi, I have a charcter (mc) and I want it to walk. In its timeline I have it facing north, east, south, and west. And an animation of it walking (in all four directions). But now I don't know what code to put in actions for it to move.....
    Can anyone tell me what code to put or a web site for me to investigate?

    Any help will do
  2. #2
  3. Psycho Canadian
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Jan 2001
    Location
    Canada
    Posts
    4,846
    Rep Power
    635
    sorry I've never used flash, I'm moving both your posts to the flash forum, since it's about a specific language.
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2006
    Location
    America
    Posts
    146
    Rep Power
    8
    ......but we are in the Flash forum....
  6. #4
  7. Psycho Canadian
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Jan 2001
    Location
    Canada
    Posts
    4,846
    Rep Power
    635
    Now we are that I moved it, you had posted it in Game development. You should get better help here as it's flash specific
  8. #5
  9. Prisoner of the Sun

    Join Date
    Jul 2004
    Location
    The Mews At Windsor Heights
    Posts
    5,309
    Rep Power
    2350
    You need to use the Key object.

    The basic code for 2D movement is this:
    actionscript Code:
     
    var myListener:Object = new Object();
     
    myListener.onKeyDown = function()
    {
    	var character:MovieClip = _root.characterClip;
     
    // these 2 vars can be changed to affect speed of forward/backward movement
    forwardSpeed = 5;
    backSpeed = 3;
     
    character.xspeed = 0;
    character.yspeed = 0;
     
    // LEFT
    if (Key.isDown(Key.LEFT)) {
    character._rotation += -15;
    }
     
    // RIGHT
    if (Key.isDown(Key.RIGHT)) {
    character._rotation += 15;
    }
     
    // UP
    if (Key.isDown(Key.UP)) {
    character.yspeed += Math.cos(character._rotation * Math.PI/180) * -forwardSpeed;
    character.xspeed += Math.sin(character._rotation * Math.PI/180) * forwardSpeed;
    }
     
    // DOWN
    if (Key.isDown(Key.DOWN)) {
    character.yspeed += Math.cos(character._rotation * Math.PI/180) * backSpeed;
    character.xspeed += Math.sin(character._rotation * Math.PI/180) * -backSpeed;
    }
     
    // UPDATE MOVEMENT/DIRECTION DATA
    character._x += character.xspeed;
    character._y += character.yspeed;
    }
     
    Key.addListener(myListener);


    Paste that code on a frame and create a Movieclip symbol with the instance name of characterClip. I suggest making it a simple triangle shape to start with so you can easily see how the keys affect it's movement. Make sure the registraion point of the character movie clip is set to the Center and NOT the Top-Left. You change it via the Window > Info panel. Just make sure that the little black box is in the center and not the top-left. Also make sure that whatever your draw in the characterClip symbol is positioned at 0,0

    Comments on this post

    • Viper_SB agrees : nice answer
    • SimonGreenhill agrees
    Last edited by b3n; June 28th, 2006 at 06:39 AM.
    .
    :: My blip.fm tunes :: Web Design Feeds :: Web Dev Feeds :: CheatSheets :: PHP :: MySQL :: 13 Moon FB App.

    "All matter is merely energy condensed to a slow vibration. We are all one consciousness experiencing itself - subjectively. There is no such thing as death, life is only a dream. We are the imaginations of ourselves."
    - Bill Hicks


    "Truth is hidden in the subtle nature of the heart of everything, although it is invisible. One cannot see it from inside and neither from the surface. One can only live and experience it."
    - Heart Sutra
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2006
    Posts
    201
    Rep Power
    8
    there is a tutorial and a downloadable FLA file here all about player movement:

    http://www.flashkit.com/tutorials/Games/Building-David_Do-598/index.php

IMN logo majestic logo threadwatch logo seochat tools logo