Results 1 to 12 of 12

Thread: Character movement is jerky. Can this code be optimised?

  1. #1

    Character movement is jerky. Can this code be optimised?

    I have a game in which you click an area of the screen and the character walks to it. I want the character to move at 90 degrees only, so horizontally and vertically, with no diagonal. I have used a fair few if statements inside the enterFrame, and the character walks a bit jerkily - I guess because there is too much calculations to be done every frame.

    My code is below. If you can think of any better way to optimise it please let me know.

    Code:
        scene.character.onEnterFrame = function(){
            
            //---walk horizontally first
            if(doneWalkingX == false){
    
                //--are you in front or behind?
                if(targetX > this._x){
                    distanceX = targetX - this._x
                    scene.character.gotoAndStop("walk_right")
                    if(distanceX > walkSpeed){
                        this._x += walkSpeed
                    }else{
                        doneWalkingX = true
                    }
                }else if(targetX < this._x){
                    scene.character.gotoAndStop("walk_left")
                    distanceX =  this._x - targetX
                    if(distanceX > walkSpeed){
                        this._x -= walkSpeed
                    }else{
                        doneWalkingX = true
                    }
                } else{
                    //--bang on
                    trace("bang on X")
                    doneWalkingX = true
                }
            }
            //--walk vertically second
            if(doneWalkingX == true and doneWalkingY == false){
                
                //--are you in front or behind?
                if(targetY > this._y){
                    distanceY = targetY - this._y
                    scene.character.gotoAndStop("walk_down")
                    if(distanceY > walkSpeed){
                        
                        this._y += walkSpeed //--move
                        this.swapDepths(this._y) //--be in front or behind of the objects on screen
                    }else{
                        doneWalkingY = true //--this means we are finished
                    }
                }else if(targetY < this._y){
                    distanceY =  this._y - targetY
                    scene.character.gotoAndStop("walk_up")
                    if(distanceY > walkSpeed){
                        this._y -= walkSpeed //--move
                        this.swapDepths(this._y) //--be in front or behind of the objects on screen
                    }else{
                        doneWalkingY = true //--this means we are finished
                    }
                } else{
                    //--bang on
                    trace("bang on Y")
                    doneWalkingY = true
                }
            }
            
            if(doneWalkingX == true and doneWalkingY == true){
                //--finished walking
                //--now do the thing they've set out to do
                _root[doingAction](currentTargetItem)  //--because we might do different things after walking, call one of many functions. The action tells us what
    
                doingAction=""
                statusText = ""
                delete(this.onEnterFrame)
                trace("done walking. xpos of character = "+scene.character._x)
            }
        }

  2. #2
    Some suggestions...

    For the "Are you in front or behind?" stuff, try this instead:
    Code:
    distanceX = targetX - this._x;
    scene.character.gotoAndStop("walk_right")
    if (Math.abs(distanceX) > walkSpeed) {
        this._x += walkSpeed * distanceX / Math.abs(distanceX);
    } else {
        doneWalkingX = true
    }
    Math.abs() returns the absolute value of the number, that is, no minus sign Thus, the result of the division of the distance by its absolute value can only be 1 (if distance is positive) or -1 (if it's negative), which adjusts the speed variable according to the relative position of your character to the target position. The same works for the Y axis.

    However, even though the code can be optimized, it doesn't justifies the said jerkness of movement. Most probably it's an animation problem, but I can only confirm that by seeing the thing working. Maybe the speed of animation doesn't match the real movement speed, or maybe it needs more smooth animation...

    Hope it helps.
    Bruno Palermo
    When everything is catastrophic, thank the Gods things aren't worse!

  3. #3
    http://www.tonypa.pri.ee/tbw/start.html

    That should work perfect for your situation. It's a big read but everything there is exetremely usefull.

  4. #4
    Here it is walking. bpalermo I replied to your post in the other thread, this question is actually about the same point and click game.

    Like many things, it works perfectly in the flash editor, then when you preview in IE or FF it just slows down and is jerky.

    Thanks for the recommendations bpalermo and substance, I will give them a try!
    Attached Files Attached Files

  5. #5
    bpalermo, I like your code because you have cut several if statements down to one, but it doesnt solve the problem of telling the character which way to face. I will still need another IF statement to work out if he should face left or right.
    Any ideas?

  6. #6
    Quote Originally Posted by onion View Post
    bpalermo, I like your code because you have cut several if statements down to one, but it doesnt solve the problem of telling the character which way to face. I will still need another IF statement to work out if he should face left or right.
    Any ideas?
    You mean the setting the image to the right facing, right? It moves but doesn't change the sprite to the correct direction, is that it? You're right I left your line regarding face, but I didn't put it where it should be, my fault.

    Code:
    distanceX = targetX - this._x;
    dirMod = distanceX / Math.abs(distanceX);
    if (Math.abs(distanceX) > walkSpeed) {
        if (dirMod > 0) {
            scene.character.gotoAndStop("walk_right");
        } else {
            scene.character.gotoAndStop("walk_left");
        }
        this._x += walkSpeed * dirMod;
    } else {
        doneWalkingX = true
    }
    We just have to include another if statement. If the division result is positive, it faces right, otherwise it goes for left.
    Bruno Palermo
    When everything is catastrophic, thank the Gods things aren't worse!

  7. #7
    Regarding the problem with browser performance, I'll check it when I get home, šause I'm at the office right now and I should be working. :p
    Bruno Palermo
    When everything is catastrophic, thank the Gods things aren't worse!

  8. #8
    1,627
    posts
    hugeExplosions = true;
    -- Off topic --

    LOL! Unrelated to the actual code, but the current graphics actually remind me of one of my nightmares! You should keep the graphics as is because if you don't create a game with graphics like these I WILL!

    I can imagine a game with these graphics being very weird with strange logic such as 'use black thing with green fish'
    MS Paint FTW!


  9. #9
    Thanks bpalermo it works brilliantly! And the code looks a lot nicer than my barrage of IF statements :-P

    lol Charleh, do you have these nightmares because you live in Bolton? :-P
    ----------------------
    Flasher Games

  10. #10
    1,627
    posts
    hugeExplosions = true;
    Hell yes, you should see some of the malformed malnourished freakophiles that live on the estate I reside on. I'd love to move it's just not a good time at the moment. I swear the kids there are all born with extra limbs.
    MS Paint FTW!


  11. #11
    Make a game about that: escaping the freaks of Bolton. I imagine it like 28 days later, but instead of swarms of zombies, swarms of chavs. Your goal could be to get to a safe-house in Burnley.
    ----------------------
    Flasher Games

  12. #12
    1,627
    posts
    hugeExplosions = true;
    Yeah, and extra hard enemies could be customised ford Novas and old Fiestas with huge spoilers on and thumping beats coming out of, full of no less than 30 occupants each wearing a baseball cap and some form of tracksuit, their lifeless brains causing them to bop to the music.

    The good thing is you'll hear the customised 1 litre engine popping through the customised 40cm wide exhaust long before they get close!
    MS Paint FTW!


Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012