# How can I more efficiently write my Math-Code in AS3

So this might be an odd question to ask. But I'm not really the biggest mathematician out there, and I have a weird habit of writing code. So I'm going to post what I'm working on. I'm doing a self-game jam this weekend which will be 48 hours long, and I've kind of cheated by already starting. The question is -- well what I'm trying to do is create a ease-in / bouncing effect without using a Tween library.

I feel that what I have is suffice, but I don't think it is very efficient and that I'm overcomplicating my code with clutter.

Below is code from my Engine class with comments that should help decipher what I am doing.

package {
import flash.display.MovieClip;
import flash.ui.Mouse;
import flash.display.Stage;
import flash.events.Event;

public class Engine extends MovieClip {

public var spaceShip:Ship = new Ship(stage);
private var logo:IcarusLogo = new IcarusLogo();

//Math, Velocities , Numbers
private var vF : Number = 0.5; // Velocity Forward
private var vX : Number = 0; // Velocity X
private var vY : Number = 0; // Velocity Y
private var iV : Number = 0.2; // Initial Velocity
private var fV : Number = .90; //Friction
private static var pI:Number = 3.14; // pI

public function Engine() {
initGraphics();
runGame();
Mouse.hide();
}
//INIT EVERYTHING
private function initGraphics():void {
initShip();
initLogo();
}
private function initShip():void {
spaceShip.x = stage.stageWidth/2;
spaceShip.y = stage.stageHeight/2;
}
private function initLogo():void {
logo.x = stage.stageWidth - 1200;
logo.y = 200;
}
//RUN THE GAME
private function runLogoStyles():void {
logo.x += vX;
iV = .025;
vX *= fV + iV;
if (logo.x <= 450) {
logo.x += vX;
vX += fV + iV;
iV = -.2;
}
}
public function runGame():void {
}
public function update(e:Event):void {
runGame();
runLogoStyles();
}
//TRASH EVERYTHING THATS NOT NEEDED.
//AS3 HAS NO NARC RULE SO JUST REMOVE.CHILD
public function trashCan():void {
//TrashCan is similar to init. Create Sub-Functions // and init everything in here.
}
}
}


Here's a screenshot to give a better understanding : http://f.cl.ly/items/3X3j0L052v0M2b0v2p1b/screen.png

One way quick and easy way of doing this which I've used many times

ease in:

private function runLogoStyles():void {
var speed = 5;
var maxSpeed = 50;
logo.x += Math.min((450 - logo.x) / speed, maxSpeed);
}


wobble:

private function runLogoStyles():void {
var wobbleSpeed = 1.4;
logo.x = 450 - ((450 - logo.x) / wobbleSpeed);
}

1. Try the ease in forumla and see the effect working.
2. Try the wobble forumla to see the effect.

Once you are happy that these forumla works. Play ease in until it like 10 pixels or so away from the target then kick in the wobble forumla.

Some tweaking with with speed, wobbleSpeed and maxSpeed values and when to kick in the wobble is needed.

Added: The wobble could well be too violent for your liking so I've combined both forumla to form this:

Tip: Experiment with all the numbers shown in red

private function runLogoStyles():void {
logo.x += Math.min(50,(currentTarget - logo.x) / 3);

if (Math.abs(logo.x-currentTarget) < Math.abs(target-currentTarget) / 1.9) {
currentTarget= target + ((target - logo.x) / 1.2);
}
}


Before calling this function add and initialise the following class member variables:

private var target:Number = 450;
private var currentTarget:Number = 450 + 20;

• I've updated the wobble forumla as I got it slightly wrong. If you have copied earlier version then grab this one. – John May 25 '12 at 23:01
• Thanks! I'll have to give this a try. I've been up for a while so I'm going to crash. But when I get up I'll give this a whirl. Thanks for your help! :P – Monstr92 May 25 '12 at 23:39
• The wobble will probably be too violent so I've added another section at the end – John May 26 '12 at 0:33
• Cool, thanks! I'll try these out tonight. Thanks a lot :P – Monstr92 May 26 '12 at 1:31