Created
December 14, 2014 12:46
-
-
Save liuzhuan/c9f9b9a197d6dfc89319 to your computer and use it in GitHub Desktop.
Node Garden in ActionScript 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package { | |
import flash.display.Sprite; | |
public class Ball extends Sprite { | |
public var vx:Number; | |
public var vy:Number; | |
public function Ball() { | |
init(); | |
} | |
private function init():void | |
{ | |
graphics.beginFill(0xFFFFFF); | |
graphics.drawCircle(0, 0, randRange(1, 3)); | |
graphics.endFill(); | |
resetSpeed(); | |
cacheAsBitmap = true; | |
} | |
public function update():void { | |
// vx += 0.1 | |
// vy += 0.4; | |
this.x += vx; | |
this.y += vy; | |
} | |
public function resetSpeed():void { | |
var randAngle:Number = Math.random() * 2 * Math.PI; | |
var speed:Number = randRange(5, 10); | |
vx = Math.cos(randAngle) * speed; | |
vy = Math.sin(randAngle) * speed; | |
} | |
private function randRange(min:Number, max:Number):Number { | |
return Math.random() * (max - min) + min; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package { | |
import flash.display.Sprite; | |
import flash.events.Event; | |
import flash.ui.Mouse; | |
public class Main extends Sprite { | |
public function Main() { | |
init(); | |
} | |
private function init():void | |
{ | |
Mouse.hide(); | |
_particles = []; | |
addEventListener(Event.ENTER_FRAME, render); | |
} | |
private function render(e:Event):void | |
{ | |
if (_particles.length < _numParticles) { | |
var particle:Ball = new Ball(); | |
// addChild(particle); | |
particle.x = mouseX; | |
particle.y = mouseY; | |
_particles.push(particle); | |
} | |
for (var i:int = 0; i < _particles.length; i++) | |
{ | |
var b:Ball = _particles[i] as Ball; | |
b.update(); | |
if (b.x < -b.width / 2 | |
|| b.x > stage.stageWidth + b.width / 2 | |
|| b.y < -b.height / 2 | |
|| b.y > stage.stageHeight + b.height / 2) { | |
b.resetSpeed(); | |
b.x = mouseX; | |
b.y = mouseY; | |
} | |
} | |
drawLines(); | |
} | |
private function drawLines():void | |
{ | |
graphics.clear(); | |
graphics.lineStyle(0.25, 0xFFFFFF, 0.5); | |
for (var i:int = 0; i < _particles.length -1; i++ ) { | |
for (var j:int = i+1; j < _particles.length ; j++) | |
{ | |
var a:Ball = _particles[i] as Ball; | |
var b:Ball = _particles[j] as Ball; | |
var dist:Number = getDist(a.x, a.y, b.x, b.y); | |
if (dist < 100 && dist > 50) { | |
graphics.moveTo(a.x, a.y); | |
graphics.lineTo(b.x, b.y); | |
} | |
} | |
} | |
} | |
private function getDist(x0:Number, y0:Number, x1:Number, y1:Number):Number { | |
var dx:Number = x1 - x0; | |
var dy:Number = y1 - y0; | |
return Math.sqrt(dx * dx + dy * dy); | |
} | |
private var _numParticles:int = 100; | |
private var _particles:Array; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment