Skip to content

Instantly share code, notes, and snippets.

@zachwlewis
Created July 24, 2013 04:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zachwlewis/6068072 to your computer and use it in GitHub Desktop.
Save zachwlewis/6068072 to your computer and use it in GitHub Desktop.
Testing tweens for proper behavior.
import flash.display.BitmapData;
import net.flashpunk.Entity;
import net.flashpunk.FP;
import net.flashpunk.graphics.Image;
import net.flashpunk.Tween;
import net.flashpunk.tweens.motion.LinearMotion;
import net.flashpunk.utils.Draw;
import net.flashpunk.utils.Ease;
import net.flashpunk.utils.Input;
import net.flashpunk.World;
/**
* Testing for tweens.
* @author Zachary Lewis (http://zacharylew.is)
*/
public class TweenWorld extends World
{
protected var _simpleSquare:BitmapData;
protected var _tweener:Entity;
protected var _placementBrush:Entity;
protected var _tween:Tween;
public function TweenWorld()
{
_simpleSquare = new BitmapData(32, 32, true, 0xffff3366);
_tweener = new Entity(50, 50, new Image(_simpleSquare));
_tweener.graphic.x = -16;
_tweener.graphic.y = -16;
_placementBrush = new Entity(50, 50, new Image(_simpleSquare));
_placementBrush.graphic.x = -16;
_placementBrush.graphic.y = -16;
Image(_placementBrush.graphic).alpha = 0.5;
}
override public function begin():void
{
add(_tweener);
add(_placementBrush);
super.begin();
}
override public function update():void
{
if (Input.mouseReleased)
{
// The mouse was released. Place our placement brush.
_placementBrush.x = Input.mouseX;
_placementBrush.y = Input.mouseY;
if (_tween != null)
{
_tween.cancel();
}
_tween = new LinearMotion(tweenComplete);
LinearMotion(_tween).setMotion(_tweener.x, _tweener.y, _placementBrush.x, _placementBrush.y, 1.5, Ease.backInOut);
//LinearMotion(_tween).setMotionSpeed(_tweener.x, _tweener.y, _placementBrush.x, _placementBrush.y, 500, Ease.backInOut);
LinearMotion(_tween).object = _tweener;
addTween(_tween, true);
}
super.update();
}
override public function render():void
{
super.render();
Draw.linePlus(_tweener.x, _tweener.y, _placementBrush.x, _placementBrush.y, 0xff3366, (_tween != null) ? (_tween.percent) : 0.0, (_tween != null) ? 5 * _tween.scale : 0.0);
}
protected function tweenComplete():void
{
FP.console.log("Tween complete.");
FP.console.log("Entity at", _tweener.x, _tweener.y);
FP.console.log("Should be", _placementBrush.x, _placementBrush.y);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment