I find creating text fields a bit tedious at times especially when your setting numerous parameters.
So i created SimpleTextField, it extends TextField and takes up to four parameters, a TextFormat, autosize which is automatically set to left, selectable set to false and anitaliastype set to normal.
These parameters are usually the ones i need to modify on all text fields i create via code, so this class speeds up development quite a bit.
Download and modify to your liking, there are certainly more parameters to be used or removed like embed fonts or even the text to be displayed.
Download: SimpleTextField.as
Code:
package com.arcticcode.greenFlames.text
{
import flash.text.TextField;
import flash.text.TextFormat;
public class SimpleTextField extends TextField
{
public function SimpleTextField(textFormat:TextFormat=null, autoSize:String = "left", selectable:Boolean = false, antiAliasType:String = "normal")
{
super();
this.autoSize = autoSize;
this.defaultTextFormat = textFormat;
this.selectable = selectable;
this.antiAliasType = antiAliasType;
}
public function move(x:Number, y:Number):void
{
super.x = x;
super.y = y;
}
}
}
Vote for this on HexoSearch!
[Update: Updated the core class with functiosn to scramble text, get random characters from specific paramaters, also updated the example to show both Scramble and Unscramble in action, may post a live example]
Came accross this AS1 Tutorial, and decided to code my own class from scratch, came along nicely.
Scrambler.as: Download
Code:
package com.arcticcode.greenFlames.Text
{
public class Scrambler
{
public static const ALPHABET:String = "abcdefghijklmnolpqrstuvwxyz";
public static const NUMBERS:String = "0123456789";
public static const SYMBOLS:String = "`¬!\"£$%^&*()_+=-[];'#./,{}~@:?><\|";
public static function scramble(input:String):String
{
var str:String = "";
for(var i:uint = 0;i<input.length;i++)
{
str += getRanChar(input.charAt(i));
}
return str;
}
public static function getRanChar(char:String):String
{
var ch:String = "";
if(ALPHABET.indexOf(char) != -1)
{
ch = ALPHABET.charAt(Math.floor(Math.random()*ALPHABET.length));
if(ch == char)
{
ch = getRanChar(char);
}
}
else if(ALPHABET.indexOf(char.toLowerCase()) != -1)
{
ch = ALPHABET.charAt(Math.floor(Math.random()*ALPHABET.length)).toUpperCase();
if(ch == char)
{
ch = getRanChar(char);
}
}
else if(NUMBERS.indexOf(char) != -1)
{
ch = NUMBERS.charAt(Math.floor(Math.random()*NUMBERS.length));
if(ch == char)
{
ch = getRanChar(char);
}
}
else if(SYMBOLS.indexOf(char) != -1)
{
ch = SYMBOLS.charAt(Math.floor(Math.random()*SYMBOLS.length));
if(ch == char)
{
ch = getRanChar(char);
}
}
else if(char == " ")
{
ch = " ";
}
return ch;
}
public static function unscramble(input:String,target:String,letters:Boolean,numbers:Boolean,symbols:Boolean,mixedCase:Boolean,...extraChars):String
{
var chars:String = "";
if(letters){chars += ALPHABET};
if(mixedCase){chars += ALPHABET.toUpperCase();}
if(numbers){chars += NUMBERS;}
if(symbols){chars += SYMBOLS;}
if(extraChars != null){chars += extraChars;}
var str:String = input;
var ts:String = "";
var char:String = "";
for(var i:uint=0;i<str.length;i++)
{
NUMBERS.indexOf(target.charAt(i)) != -1 ? char = NUMBERS.charAt(Math.floor(Math.random()*NUMBERS.length)) : char = chars.charAt(Math.floor(Math.random()*chars.length));
str.charAt(i) != target.charAt(i) ? ts += char : ts += str.charAt(i);
}
return ts;
}
}
}
To Use:
package
{
import com.arcticcode.greenFlames.Text.Scrambler;
import com.arcticcode.greenFlames.utils.StringUtils;
import flash.display.Sprite;
import flash.events.Event;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
[SWF(width=600,height=400,backgroundColor=0xFFFFFF)]
public class TextScramble extends Sprite
{
private var tf:TextField;
private var targetString:String="My Text 2008";
private var count:uint = targetString.length;
private var currString:String;
private var arr:Array;
private var char:String = "";
public function TextScramble()
{
init();
}
private function init():void
{
tf = new TextField();
tf.autoSize = TextFieldAutoSize.LEFT;
tf.defaultTextFormat = new TextFormat("Verdana",12,0);
addChild(tf);
tf.selectable = false;
tf.text = " ";
addEventListener(Event.ENTER_FRAME, loop);
}
private function loop(e:Event):void
{
tf.text = Scrambler.unscramble(tf.text, targetString, true, true, false, true, " ");
tf.text == targetString ? removeEventListener(Event.ENTER_FRAME, loop) : null;
}
}
}
Vote for this on HexoSearch!
So there’s no built in method to truncate a number in actionscript. Well a bit of playing around and a simple work around is just as good.
As most of you will know String’s can be truncated via substr();.
Well just typecast your value to be truncated to a String, call the substr(), using the length of the string minus your amount of digits to trincated, then typecast it back to a Number.
Code:
1
2
3
4
| function truncate(val:String,truncCount:uint):String
{
return val.substr(0,val.length-truncCount);
} |
To use:
1
2
3
4
| var num:Number = 56.673856;
trace(num)//56.673856
num = Number(truncate(num.toString(),5));
trace(num)//56.6 |
Vote for this on HexoSearch!
After a quick response on my last post, i thought i would post a quick how to and some explanation.
First of what the 5 variables mean, theta, phi, rep, r and r2.
Well theta and phi originate from the greek alphabet i think *Wikipedia’s the two* they are Theta and Phi courtesy of Wikipedia, and why they are used -
“The origin of theta into mathematical problems began with the Greeks. They logically chose this Greek symbol since it was the next in their alphabet not yet used and was easily legible to recreate for multiple uses.”, next is rep from experiments this is basically the amount the curve is repeated which when is low will only draw say a half a circle’s diamter of curve if that makes any sense.
R represents the radius of the curve and R2 is used in the second wrapping of the curve, “for added beauty, a second higher frequency curve winds about the initial Lissajous Figure”. All that mumbo jubo should look simpler via code, another note is a spherical lissajous figure lies on the surface of a sphere so it can be drawn in 3D, which i will also be showing in this post.
Right some code first up the core function:
//Theta and Phi both converted to radians
var theta:Number = (30) * (Math.PI/180);
var phi:Number = (12) * (Math.PI/180);
//REPETITION
var rep:Number = 30;
//RADIUS
var r:Number = 60;
//HIGHER FREQUENCY
var r2:Number = 2;
//Pass in a BitmapData and it will draw it via a shape then to the bitmap data
//with the X and Y coordinates passed in
//You will notice it calculates the Z as well if you are not plotting in 3D just pass in 0
//I left the Z calculation in for experimentalists =P
function lissajous(bmd:BitmapData=null,X:Number=0,Y:Number=0,Z:Number=0,colour:uint=0):void
{
var shape:Shape = new Shape();
shape.x = X;
shape.y = Y;
shape.graphics.lineStyle(0,colour);
for(var t:Number = 0;t<rep*Math.PI;t+=.01)
{
var xPos:Number = r * Math.sin(theta*t) * Math.cos(phi*t)+X;
var zPos:Number = r * Math.cos(theta*t)+Z;
var yPos:Number = r * Math.sin(theta*t) * Math.sin(phi*t)+Y;
if(t==0)shape.graphics.moveTo(xPos,yPos)
else shape.graphics.lineTo(xPos,yPos);
//bmd.setPixel(xPos,yPos,colour);
}
for(t=0;t<rep*Math.PI;t+=.01)
{
xPos = r2 * Math.sin(100*theta*t) * Math.cos(100*phi*t) + r * Math.sin(theta*t) * Math.cos(phi*t)+X;
zPos = r2 * Math.cos(100*theta*t) + r * Math.cos(theta*t)+Z;
yPos = r2 * Math.sin(100*theta*t) * Math.sin(100*phi*t) + r * Math.sin(theta*t) * Math.sin(phi*t)+Y;
if(t==0)shape.graphics.moveTo(xPos,yPos)
else shape.graphics.lineTo(xPos,yPos);
//bmd.setPixel(xPos,yPos,colour);
}
bmd.draw(shape);
shape.graphics.clear();
shape = null;
}
Thats the basics of 2D rendering of spherical lissajous figures. The two apps below are the 3D rendering of the figures one using curves and the other using circles.
Play around with the values to get some cool patterns, there not using any mainstream 3D engine like PV3D or Away3D just some old fashioned home made 3D.


Vote for this on HexoSearch!