This is so ugly code. There must be better way to write it, but I'm noob to AS3.
The goal is to make a square move at even speed counter-clockwise around the edge and on call reverseDirection();
, changeColor();
, or spin();
.
I'm trying to understand Tweenlite vars onStart
and onStartParams
. I'm sure that it can be used to write this elegantly with only one TweenLite.to.
I use AS3, Starling and Tweenlite. This is the code I'm trying to simplify:
public class Square extends Sprite implements ISquare {
public const square:Quad = new Quad(100, 100);
private var tweeningDirection:String;
public function Square() {
}
public function draw():void{
addChild(square);
square.pivotX = square.width / 2;
square.pivotY = square.height / 2;
square.x = 50;
square.y = 50;
square.color = 0x4500FF;
tweeningDirection = new String;
down();
}
public function reverseDirection():void{
TweenLite.killTweensOf(square);
switch (tweeningDirection){
case "down":
stoppedDown();
break;
case "left":
stoppedLeft();
break;
case "up":
stoppedUp();
case "right":
stoppedRight();
break;
case "stoppedDown":
down();
break;
case "stoppedLeft":
left();
break;
case "stoppedUp":
up();
break;
case "stoppedRight":
right();
break;
case "rDown":
stoppedRDown();
break;
case "rLeft":
stoppedRLeft();
break;
case "rUp":
stoppedRUp();
break;
case "rRight":
stoppedRRight();
break;
case "stoppedRDown":
rDown();
break;
case "stoppedRLeft":
rLeft();
break;
case "stoppedRUp":
rUp();
break;
case "stoppedRRight":
rRight();
break;
}
}
private function down():void {
tweeningDirection = "down";
TweenLite.to(square, stage.stageHeight / 200, {y: stage.stageHeight - 50, ease: Linear.easeNone, onComplete: right});
}
private function stoppedDown():void {
tweeningDirection = "stoppedDown";
TweenLite.to(square, square.y / 200, {y: 50, ease: Linear.easeNone, onComplete: rRight});
}
private function right():void {
tweeningDirection = "right";
TweenLite.to(square, stage.stageWidth / 200, {x: stage.stageWidth - 50, ease: Linear.easeNone, onComplete: up});
}
private function stoppedRight():void {
tweeningDirection = "stoppedRight";
TweenLite.to(square, square.x / 200, {x: 50, ease: Linear.easeNone, onComplete: rUp});
}
private function up():void {
tweeningDirection = "up";
TweenLite.to(square, stage.stageHeight / 200, {y:50, ease: Linear.easeNone, onComplete: left});
}
private function stoppedUp():void {
tweeningDirection = "stoppedUp";
TweenLite.to(square, (stage.stageHeight - square.y) / 200, {y: stage.stageHeight - 50, ease: Linear.easeNone, onComplete: rLeft});
}
private function left():void {
tweeningDirection = "left";
TweenLite.to(square, stage.stageWidth / 200, {x: 50, ease: Linear.easeNone, onComplete: down});
}
private function stoppedLeft():void {
tweeningDirection = "stoppedLeft";
TweenLite.to(square, (stage.stageWidth - square.x) / 200, {x: stage.stageWidth - 50, ease: Linear.easeNone, onComplete: rDown});
}
private function rRight():void {
tweeningDirection = "rRight";
TweenLite.to(square, stage.stageWidth / 200, {x: stage.stageWidth - 50, ease: Linear.easeNone, onComplete: rDown});
}
private function stoppedRRight():void {
tweeningDirection = "stoppedRRight";
TweenLite.to(square, square.x / 200, {x: 50, ease: Linear.easeNone, onComplete: down});
}
private function rDown():void {
tweeningDirection = "rDown";
TweenLite.to(square, stage.stageHeight / 200, {y: stage.stageHeight - 50, ease: Linear.easeNone, onComplete: rLeft});
}
private function stoppedRDown():void {
tweeningDirection = "stoppedRDown";
TweenLite.to(square, square.y / 200, {y: 50, ease: Linear.easeNone, onComplete: left});
}
private function rLeft():void {
tweeningDirection = "rLeft";
TweenLite.to(square, stage.stageWidth / 200, {x: 50, ease: Linear.easeNone, onComplete: rUp});
}
private function stoppedRLeft():void {
tweeningDirection = "stoppedRLeft";
TweenLite.to(square, (stage.stageWidth - square.x) / 200, {x: stage.stageWidth - 50, ease: Linear.easeNone, onComplete: up});
}
private function rUp():void {
tweeningDirection = "rUp";
TweenLite.to(square, stage.stageHeight / 200, {y:50, ease: Linear.easeNone, onComplete: rRight});
}
private function stoppedRUp():void {
tweeningDirection = "stoppedRUp";
TweenLite.to(square, (stage.stageHeight - square.y) / 200, {y: stage.stageHeight - 50, ease: Linear.easeNone, onComplete: right});
}
}
}