Skip to content

Instantly share code, notes, and snippets.

@Fintan
Created October 23, 2012 14:08
Show Gist options
  • Save Fintan/3938945 to your computer and use it in GitHub Desktop.
Save Fintan/3938945 to your computer and use it in GitHub Desktop.
Tube drawing
/*
see it working here: http://try.haxe.org/#DEacf
A circle follows the cursor and gets copied to a bitmap
Uses an adaptation of hype.extended.behavior.FixedVibration
*/
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.display.Bitmap;
class Test {
var bitmapData:BitmapData;
var circle:Circle;
var container:Sprite;
var speed:Float;
var vibrate:FixedVibration;
static function main() {
new Test();
}
function new () {
bitmapData = new BitmapData( 800, 600, false , 0x000000 );
var bitmap = new Bitmap(bitmapData);
flash.Lib.current.addChild( bitmap );
container = new Sprite();
circle = new Circle();
var dot = new Circle();
dot.scaleX = dot.scaleY = 0.5;
dot.x = 10;
circle.addChild(dot);
speed = 0.02;
vibrate = new FixedVibration(0.99, 0.05, 0.5, 1);
container.addChild(circle);
flash.Lib.current.addChild(container);
flash.Lib.current.addChild( bitmap );
flash.Lib.current.addEventListener(flash.events.Event.ENTER_FRAME, onLoop);
}
function onLoop(e) {
var yDistance = flash.Lib.current.mouseY - circle.y;
var xDistance = flash.Lib.current.mouseX - circle.x;
var radian = Math.atan2(yDistance, xDistance);
circle.rotation = radian * 180 / Math.PI;
circle.x -= (circle.x - flash.Lib.current.mouseX) * speed;
circle.y -= (circle.y - flash.Lib.current.mouseY) * speed;
circle.scaleX = circle.scaleY += vibrate.calc(circle.scaleX);
bitmapData.draw(container);
}
}
class Circle extends flash.display.Sprite {
public function new() {
super();
draw();
}
function draw() {
this.graphics.beginFill(0xff00ff);
this.graphics.lineStyle(1, 0x000000);
//this.graphics.beginFill(Std.int(Math.random()*0xffffff));
this.graphics.drawCircle(0, 0, 20);
this.graphics.endFill();
}
}
class FixedVibration {
public var spring:Float;
public var ease:Float;
public var min:Float;
public var max:Float;
var range:Float;
var speed:Float;
/**
* Constructor
*
* @param spring Springiness of movement
* @param ease Ease of movement
* @param min Minimum range of the vibration
* @param max Maximum range of the vibration
*/
public function new(spring:Float, ease:Float, min:Float, max:Float) {
this.spring = spring;
this.ease = ease;
this.min = min;
this.max = max;
speed = 0;
range = Math.abs(max - min);
}
public function calc(value:Float):Float {
var newPos = min + (Math.random() * range);
speed = (speed * spring) + (newPos - value) * ease;
return speed;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment