A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: Fluid Character Movement

  1. #1
    file not found Captain_404's Avatar
    Join Date
    Apr 2006
    Posts
    457

    Fluid Character Movement

    I was recently experimenting with different way to move a character in a flash game. I came to these conclusions using my own ideas, but I'm sure it's been done before.

    Anyway, with your typical overhead flash game, you have only eight directions (arrow keys) and one speed (that is, if it's not dependant on a certain time of movement therefore simulating momentum) this is a bit of script that can be used for varying directions and speeds.

    First, one used for single character movement, put on the main character itself.

    onClipEvent (enterFrame) {
    ydist = (this._y - _parent._ymouse)/20;
    xdist = (this._x - _parent._xmouse)/25;
    }

    onClipEvent (enterFrame) {
    this._y -= ydist;
    this._x -= xdist;
    }


    Secondly, one used for a game where the character stays centered, but the background moves to simulate movement.

    to be put on a background mc

    onClipEvent (enterFrame) {
    ydist = (200 - _parent._ymouse)/20;
    xdist = (275 - _parent._xmouse)/25;
    }

    onClipEvent (enterFrame) {
    if (_parent._ymouse < 139.0) {
    this._y += ydist;
    }
    if (_parent._xmouse > 331.1) {
    this._x += xdist;
    }
    if (_parent._ymouse > 258.0) {
    this._y += ydist;
    }
    if (_parent._xmouse < 214.8) {
    this._x += xdist;
    }
    }


    I might post a few demos later if I can get them up online...

    Anyway, like I said, this has probably been done before, but I just found this on my own, and I thought it was so cool that I'd share it.

  2. #2
    crossconscious
    Join Date
    Sep 2005
    Location
    Belgium
    Posts
    1,188
    Erm. Why do you split your code in two onenterframes? Me thinks that would cause a hit in performance.

  3. #3
    Junior Member
    Join Date
    Nov 2005
    Posts
    22
    Cool, but could you please tell me how to make the background repeat itself?

  4. #4
    file not found Captain_404's Avatar
    Join Date
    Apr 2006
    Posts
    457
    there's two different codes, for two different functions.

    If you want only the character to move, use the first code, if you want the backgound to move, use the second. They're not meant to be used simltaneously.

    And as for a looping background, I would suppose you would do it just like any other.
    If the background _y and/or _x exceeds a cartain point, move it back a certain number of pixels. You might have to make a few tiles so that they overlap, otherwise you might end up with blank space. I'll experiment w/ it once I get back on my computer with the file on it.

  5. #5
    crossconscious
    Join Date
    Sep 2005
    Location
    Belgium
    Posts
    1,188
    I'm not stupid you know - I realize these are two different scripts, and even though they are highly complex (lol), I do understand them. But what I meant is, instead of this :

    onClipEvent (enterFrame) {
    ydist = (this._y - _parent._ymouse)/20;
    xdist = (this._x - _parent._xmouse)/25;
    }

    onClipEvent (enterFrame) {
    this._y -= ydist;
    this._x -= xdist;
    }


    Why don't you do this :


    onClipEvent (enterFrame) {
    ydist = (this._y - _parent._ymouse)/20;
    xdist = (this._x - _parent._xmouse)/25;
    this._y -= ydist;
    this._x -= xdist;
    }

    Not sure if it matters much, but I really don't see why you'd put such a simple script in two event handlers?

    And furthermore, it's not really a good idea to use onenterframe on each movieclip in your game, you're better off writing one onenterframe, which loops through arrays and updates the different mc's. You'll get better performance, more readable code, and you'll have more control over the order in which everything is executed.

  6. #6
    file not found Captain_404's Avatar
    Join Date
    Apr 2006
    Posts
    457
    I'm sorry, I misunderstood your question.

    And, yes, you are right, it would be more efficient. I taught myself most of the stuff I know from simply glancing at tutorials, so I don't always remember to streamline my script. I'm glad you caught that. Thank you.

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