First things first - Take a look here at a kids game:
http://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):
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;
}
}
}
}