Return to Snippet

Revision: 28609
at July 10, 2010 06:56 by adamcoulombe


Initial Code
//Get Traer.Physics for AS3 at http://blog.sqrtof5.com/?p=53
import traer.physics.Attraction;
import traer.physics.Particle;
import traer.physics.ParticleSystem;
import traer.physics.Spring;

//get k-lib Kuler API Library at http://code.google.com/p/k-lib/
import com.adobe.kuler.events.GetResultEvent;
import com.adobe.kuler.params.SearchParams;
import com.adobe.kuler.swatches.swatch.color.Color;
import com.adobe.kuler.KLibService;

import flash.geom.Vector3D;

//
//
//
//
// Get a Kuler API key at http://kuler.adobe.com/api/
//
// BE SURE TO ENTER YOUR API KEY HERE FOR THIS TO WORK
//		||
//		||
//		||
//		\/

var YOUR_KULER_API_KEY = "";



var s:ParticleSystem = new ParticleSystem(new Vector3D(0, 0, 0), .2);
var waterMolecules = new Array();
var waterParticles = new Array();
var sky = new Array();
var earth = new Array();
var magnet = s.makeParticle(0.12, new Vector3D(0, 0, 0));
var hover=new Molecule();
var points = new Array();
var water = new Sprite();
var lastColor = "000000";
var container = new Sprite();
var stripes = new Array();
var colors = new Array();
var totalWidth = 0;
var swatch;


var klib:KLibService = new KLibService(YOUR_KULER_API_KEY);
klib.addEventListener(GetResultEvent.GET_RESULTS, onResults);
klib.getRecent();


function onResults(e){
	swatch = e.results.swatches[0];
	trace(swatch.colors[ int(Math.random() * swatch.colors.length -1) ]);
	init();
}



function init(){
	magnet.makeFixed();
	magnet.position.y=200;
	var i=0;
	while(totalWidth<stage.stageWidth){

		colors[i] = lastColor;
		while(colors[i]==lastColor){
			colors[i]= swatch.colors[ int(Math.random() * swatch.colors.length -1) ].swatchHexColor;
			trace(colors[i]);
		}
		sky[i]=s.makeParticle(10, new Vector3D(totalWidth, 100, 0)); sky[i].makeFixed();
		waterParticles[i]=s.makeParticle(0.5, new Vector3D(totalWidth, 200, 0));
		earth[i]=s.makeParticle(10, new Vector3D(totalWidth, 300, 0)); earth[i].makeFixed();
		var thisWidth = Math.random()*20 + 5;
		totalWidth += thisWidth;
		stripes[i]=new Sprite();
		addChild(stripes[i]);

		if(i>0){
			s.makeSpring(waterParticles[i-1], waterParticles[i], 0.1 , .05, 0);
			s.makeSpring(sky[i], waterParticles[i], 0.05 , .05, 0);
			s.makeSpring(earth[i], waterParticles[i], 0.05 , .05, 0);
			
			s.makeAttraction(magnet, waterParticles[i], -15000 , 10);
			
		}
		lastColor = colors[i];
		i++
	}
	
	waterParticles[0].makeFixed();
	waterParticles[waterParticles.length-1].makeFixed();
	waterParticles[waterParticles.length-1].position.x=stage.stageWidth;

	stage.addEventListener(Event.ENTER_FRAME,loop);
}

function loop(e){
	s.tick(1);
	magnet.position.x = hover.x = mouseX;

	for(var i in waterParticles){
		stripes[i].graphics.clear();
		stripes[i].graphics.beginFill("0x"+colors[i]);
		if(i<waterParticles.length){
			stripes[i].graphics.moveTo(waterParticles[i].position.x,0);
			stripes[i].graphics.lineTo(waterParticles[i].position.x,stage.stageHeight);
		}
		if(i>0){
			stripes[i].graphics.lineTo(waterParticles[i-1].position.x,stage.stageHeight);
			stripes[i].graphics.lineTo(waterParticles[i-1].position.x,0);			
		}
		stripes[i].graphics.endFill();

	}

}

Initial URL
http://adamcoulombe.info/lab/as3/stripes.html

Initial Description
see demo at http://adamcoulombe.info/lab/as3/stripes.html.

This demonstrates the use of k-lib, Kuler API library for AS3. As well as Traer Physics for to handle the springiness.

Initial Title
Randomly Colored Elastic Stripes

Initial Tags
color

Initial Language
ActionScript 3