# Character movement is jerky. Can this code be optimised?

**URL:** <https://forum.kirupa.com/t/character-movement-is-jerky-can-this-code-be-optimised/260686>\
**Category:** programming\
**Created:** [May 18, 2008, 9:33pm UTC](https://forum.kirupa.com/t/character-movement-is-jerky-can-this-code-be-optimised/260686 "2008-05-18T21:33:06Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![onion](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/onion/32/3349_2.png) [@onion](https://forum.kirupa.com/u/onion)\
**Post date:** [May 18, 2008, 9:33pm UTC](https://forum.kirupa.com/t/character-movement-is-jerky-can-this-code-be-optimised/260686/1 "2008-05-18T21:33:06Z")

</div>

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.

```auto
    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)
        }
    }

```

---

<div class="post-metadata">

**Author:** ![bpalermo](https://avatars.discourse-cdn.com/v4/letter/b/9dc877/32.png) [@bpalermo](https://forum.kirupa.com/u/bpalermo)\
**Post date:** [May 19, 2008, 12:56am UTC](https://forum.kirupa.com/t/character-movement-is-jerky-can-this-code-be-optimised/260686/2 "2008-05-19T00:56:47Z")

</div>

Some suggestions…

For the “Are you in front or behind?” stuff, try this instead:

```auto

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.

---

<div class="post-metadata">

**Author:** ![substance](https://avatars.discourse-cdn.com/v4/letter/s/c6cbf5/32.png) [@substance](https://forum.kirupa.com/u/substance)\
**Post date:** [May 19, 2008, 3:58pm UTC](https://forum.kirupa.com/t/character-movement-is-jerky-can-this-code-be-optimised/260686/3 "2008-05-19T15:58:48Z")

</div>

> **[This is Still Not Better Than Playing Games - To NY pa...](http://tonypa.pri.ee/not-better-playing-games/)**
>
> This page has an interesting story about wondering if playing games back home is more fun and more important than going on family vacations.

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

---

<div class="post-metadata">

**Author:** ![onion](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/onion/32/3349_2.png) [@onion](https://forum.kirupa.com/u/onion)\
**Post date:** [May 19, 2008, 9:46pm UTC](https://forum.kirupa.com/t/character-movement-is-jerky-can-this-code-be-optimised/260686/4 "2008-05-19T21:46:05Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![onion](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/onion/32/3349_2.png) [@onion](https://forum.kirupa.com/u/onion)\
**Post date:** [May 19, 2008, 10:00pm UTC](https://forum.kirupa.com/t/character-movement-is-jerky-can-this-code-be-optimised/260686/5 "2008-05-19T22:00:34Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![bpalermo](https://avatars.discourse-cdn.com/v4/letter/b/9dc877/32.png) [@bpalermo](https://forum.kirupa.com/u/bpalermo)\
**Post date:** [May 20, 2008, 12:09pm UTC](https://forum.kirupa.com/t/character-movement-is-jerky-can-this-code-be-optimised/260686/6 "2008-05-20T12:09:01Z")

</div>

[quote=onion;2328378]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?[/quote]

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. =)

```auto

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.

---

<div class="post-metadata">

**Author:** ![bpalermo](https://avatars.discourse-cdn.com/v4/letter/b/9dc877/32.png) [@bpalermo](https://forum.kirupa.com/u/bpalermo)\
**Post date:** [May 20, 2008, 12:10pm UTC](https://forum.kirupa.com/t/character-movement-is-jerky-can-this-code-be-optimised/260686/7 "2008-05-20T12:10:39Z")

</div>

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. 😛

---

<div class="post-metadata">

**Author:** ![Charleh](https://avatars.discourse-cdn.com/v4/letter/c/a9a28c/32.png) [@Charleh](https://forum.kirupa.com/u/Charleh)\
**Post date:** [May 20, 2008, 1:13pm UTC](https://forum.kirupa.com/t/character-movement-is-jerky-can-this-code-be-optimised/260686/8 "2008-05-20T13:13:35Z")

</div>

– 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’

---

<div class="post-metadata">

**Author:** ![onion](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/onion/32/3349_2.png) [@onion](https://forum.kirupa.com/u/onion)\
**Post date:** [May 22, 2008, 9:38pm UTC](https://forum.kirupa.com/t/character-movement-is-jerky-can-this-code-be-optimised/260686/9 "2008-05-22T21:38:29Z")

</div>

Thanks bpalermo it works brilliantly! And the code looks a lot nicer than my barrage of IF statements 😛

lol Charleh, do you have these nightmares because you live in Bolton? 😛

---

<div class="post-metadata">

**Author:** ![Charleh](https://avatars.discourse-cdn.com/v4/letter/c/a9a28c/32.png) [@Charleh](https://forum.kirupa.com/u/Charleh)\
**Post date:** [May 23, 2008, 10:02am UTC](https://forum.kirupa.com/t/character-movement-is-jerky-can-this-code-be-optimised/260686/10 "2008-05-23T10:02:22Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![onion](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/onion/32/3349_2.png) [@onion](https://forum.kirupa.com/u/onion)\
**Post date:** [May 23, 2008, 11:43pm UTC](https://forum.kirupa.com/t/character-movement-is-jerky-can-this-code-be-optimised/260686/11 "2008-05-23T23:43:27Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Charleh](https://avatars.discourse-cdn.com/v4/letter/c/a9a28c/32.png) [@Charleh](https://forum.kirupa.com/u/Charleh)\
**Post date:** [May 24, 2008, 8:49am UTC](https://forum.kirupa.com/t/character-movement-is-jerky-can-this-code-be-optimised/260686/12 "2008-05-24T08:49:38Z")

</div>

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!
