Actionscript:
-
// number of notes in chord
-
var noteNum:int = 3;
-
var lowNote:Number = 0;
-
var highNote:Number = 4;
-
// delay between chords
-
var delay:int = 2000;
-
////////////////////////////////////////
-
var chord:String = "";
-
highNote += 1;
-
var tab:TextField = TextField(addChild(new TextField()));
-
tab.x = tab.y = 20;
-
tab.defaultTextFormat = new TextFormat("Courier", 12);
-
tab.multiline = true;
-
tab.width = 200;
-
-
changeChord();
-
setInterval(changeChord, delay);
-
-
function changeChord():void{
-
var strings:Array = [];
-
strings[0] = "e|---------------\n"
-
strings[1] = "B|---------------\n"
-
strings[2] = "G|---------------\n"
-
strings[3] = "D|---------------\n"
-
strings[4] = "A|---------------\n"
-
strings[5] = "E|---------------\n"
-
-
for (var i:int = 0; i<3; i++){
-
var place:int = 5 + i * 4;
-
var choices:Array = [0,1,2,3,4,5];
-
for (var j:int = 0; j<noteNum; j++){
-
var ii:int = int(Math.random()*choices.length);
-
var index:int = choices[ii];
-
strings[index] = strings[index].slice(0, place) + (int(Math.random()*highNote)+lowNote) + strings[index].substring(place+1);
-
choices.splice(ii, 1);
-
}
-
}
-
chord = strings.join("");
-
tab.text = chord;
-
}
I'm working on a small program to help me practice guitar. It randomly generates guitar tabs. The idea for the program is similar to a program that helps you learn to type (like Mavis Beacon).
I wrote this snippet today as a proof of concept - just to help me start thinking about what kind of features I want the program to have. There are settings at the top so that you can tweak the number of notes in the chord and the delay between chords etc....
It generates three chords at a time... here are some stills:

Actionscript:
-
for (var i:int = 0; i<10; i++){
-
// randomly set a variable to -1 or positive 1
-
trace(int(Math.random()*2) - 1 | 1);
-
}
-
-
/* outputs something like this
-
1
-
1
-
-1
-
1
-
-1
-
-1
-
1
-
1
-
-1
-
-1
-
*/
The past few days I found myself using this one-liner a few times. Just a quick way to randomly get -1 or 1.
I was brainstorming today and wrote a weird/bad inline version of an old post:
var rand:Number = [-3, 2, 1, 1, 1, 0.5][int(Math.random() * 6)];
I need to install a related posts plugin for WordPress - would be nice if this post were related to my old post about Tausworthe random seeds.
Been posting medium/large snippets and snippets the relate to QuickBox2D - so today I figured I'd get back to basics...
Also posted in one-liners | Tagged actionscript, as3, flash |
By Zevan | December 17, 2008
Actionscript:
-
trace((new Date()).getTime() + Math.random()*0xFFFFFF);
A hacky way to get a unique ID, although this isn't, perfect it works well enough. For a project that you really expect to get huge amounts of traffic... you might consider using php's uniqid() function and passing it into your swf. That said, the odds of two users getting the same two ID values are about as good as the odds of you winning the lottery twice in a row.
Here is a version wrapped in a function, with a prefix argument:
Actionscript:
-
trace(uniqid("z"));
-
-
function uniqid(prefix:String):String{
-
return prefix + (new Date()).getTime() + Math.random()*0xFFFFFF;
-
}
Also posted in misc, one-liners | Tagged actionscript, flash |
By Zevan | November 25, 2008
Actionscript:
-
stage.frameRate = 30;
-
addEventListener(Event.ENTER_FRAME, onLoop);
-
function onLoop(evt:Event):void{
-
if (int(Math.random()*5)==1){
-
for (var i:int= 0; i<10; i++) createParticle();
-
}
-
}
-
-
function createParticle():void{
-
var s:MovieClip = new MovieClip();
-
s.graphics.beginFill(0);
-
s.graphics.drawCircle(0,0,Math.random()*10 + 2);
-
s.velX = Math.random()*10-5
-
s.velY = Math.random()*10-5
-
s.posX = s.x = 200;
-
s.posY = s.y = 200;
-
addChild(s);
-
s.addEventListener(Event.ENTER_FRAME, onRunParticle);
-
}
-
-
function onRunParticle(evt:Event):void {
-
var s:MovieClip = MovieClip(evt.currentTarget);
-
s.posX += s.velX;
-
s.posY += s.velY;
-
s.scaleX = s.scaleY -= .04;
-
if (s.scaleX <0){
-
removeChild(s);
-
s.removeEventListener(Event.ENTER_FRAME, onRunParticle);
-
}
-
s.x = s.posX;
-
s.y = s.posY;
-
}
Nothing special here. But my students are always asking me about this - in class I have them alter this code to make fire, water and abstract particle systems.