// Copyright (C) 2007 Ian Langworth // // MovieClip creation style described here: http://osflash.org/flashcoders/as2 class Blob extends MovieClip { // Needed for MovieClip to be instantiated easily static var symbolName = '__Packages.Blob'; static var symbolLinked = Object.registerClass(Blob.symbolName, Blob) // Constants private static var MAX_SPEED = 20; private static var ACCELERATION = 2; private static var BOUNCE_DAMPER = .75; // Instance variables private var parent; private var radius; private var vx, vy; // Object constructor function Blob() { super(); } // Object factory to make constructing objects easier static function create(parent, depth, color, x, y, radius) { var obj = Blob( parent.attachMovie(Blob.symbolName, 'Blob' + depth, depth)); obj.init(parent, color, x, y, radius); return obj; } // Object initialization function init(parent, color, x, y, radius) { this.parent = parent; this.radius = radius; this.render(color, x, y, radius); this.vx = 0; this.vy = 0; } // Update a frame of the animation function update() { // Get the direction to the mouse. var direction = this.angleTo(this.parent._xmouse, this.parent._ymouse); // Calculate the distance in horizontal and vertical movement, then add // those to the existing velocity. Don't let the velocity exceed // MAX_SPEED. this.vx = Math.min(MAX_SPEED, this.vx + Math.cos(direction) * ACCELERATION); this.vy = Math.min(MAX_SPEED, this.vy + Math.sin(direction) * ACCELERATION); // If any blobs go off the stage, make them bounce back, but only after // losing a little momentum. if (this._x - this.radius < 0) this.vx = Math.abs(this.vx) * BOUNCE_DAMPER; if (this._y - this.radius < 0) this.vy = Math.abs(this.vy) * BOUNCE_DAMPER; if (this._x + this.radius > Stage.width) this.vx = -Math.abs(this.vx) * BOUNCE_DAMPER; if (this._y + this.radius > Stage.height) this.vy = -Math.abs(this.vy) * BOUNCE_DAMPER; // Update the position of the blob. this._x += this.vx; this._y += this.vy; } // Draw the blob as a vector (a naive way to draw a circle) function render(color, x, y, radius) { this.lineStyle(3, color); this.beginFill(color, 50); this.moveTo(0, -radius); this.curveTo(radius, -radius, radius, 0); this.curveTo(radius, radius, 0, radius); this.curveTo(-radius, radius, -radius, 0); this.curveTo(-radius, -radius, 0, -radius); this.endFill(); this._x = x; this._y = y; } // Return the angle from the center of our blob to the given point in radians function angleTo(x, y) { return Math.atan2((y - this._y), (x - this._x)); } }