# Scrolling animation lags - what to do?

**URL:** <https://forum.kirupa.com/t/scrolling-animation-lags-what-to-do/279463>\
**Category:** Uncategorized\
**Created:** [January 14, 2009, 10:44pm UTC](https://forum.kirupa.com/t/scrolling-animation-lags-what-to-do/279463 "2009-01-14T22:44:13Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![knalle](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/knalle/32/3310_2.png) [@knalle](https://forum.kirupa.com/u/knalle)\
**Post date:** [January 14, 2009, 10:44pm UTC](https://forum.kirupa.com/t/scrolling-animation-lags-what-to-do/279463/1 "2009-01-14T22:44:13Z")

</div>

First things first - Take a look here at a kids game:  
[http://knalle.dk/kidsgame/](http://www.knalle.dk/kidsgame/)

The animation happens on mouse over. Try it.  
Here on my computer it lags a bit - like screen tearing on the laundry clothes (all MCs). How can you optimize the code or do it a better way?

Here’s the full code (document class):

```php

package {
    import flash.display.MovieClip;    
    import flash.display.Stage;
    import flash.events.Event;
    import flash.events.MouseEvent;
    
    public class Main extends MovieClip {
        
        var sHeight : Number;
        var sWidth : Number; 
        var rope:MovieClip;
        var ropeSpeed:Number = 0;
        var pleaseStopScroll:Boolean = true;
        
        public function Main() {
            //this.addEventListener(Event.ENTER_FRAME, moveRope);
            rope = this.game.scene.rope;
            this.game.scene.mouseChildren = false;
            this.game.scene.addEventListener( MouseEvent.MOUSE_OVER, mouseInside);
        }
        
        protected function mouseInside(e:MouseEvent) {
            pleaseStopScroll = false;
            this.game.scene.mouseChildren = true;
            this.addEventListener(Event.ENTER_FRAME, watchForMouseEntry);
        }
        
        protected function moveRope(e:Event = null):void {
            if (pleaseStopScroll) {
                ropeSpeed -= 1;
                if (ropeSpeed <= 0) {
                    this.removeEventListener(Event.ENTER_FRAME, watchForMouseEntry);
                }
            } else if (!pleaseStopScroll && ropeSpeed < 10) {
                ropeSpeed += 1;
            }
            
            var diff:Number = Math.cos((-this.game.scene.mouseX/600)*Math.PI)*ropeSpeed;
            trace(diff);
            rope.x += diff;
            
            if (rope.x > 0) {
                rope.x = 0;
            }
            if (-rope.x > (rope.width-600)) {
                rope.x = -(rope.width-600);
            }
        }
        
        protected function watchForMouseEntry(e:Event):void {
            moveRope();
            if (this.game.scene.sky.hitTestPoint(stage.mouseX,stage.mouseY,true)) {
                pleaseStopScroll = false;
            } else {
                pleaseStopScroll = true;
            }
        }
        
    }
    
}

```
