By Zevan | March 28, 2009
Actionscript:
-
var Pnt:Function = function(xp:Number, yp:Number){
-
-
var x:Number = xp;
-
var y:Number = yp
-
-
this.setX = function(v:int):void{
-
x = v;
-
}
-
this.setY = function(v:int):void{
-
y = v;
-
}
-
this.getX = function():int {
-
return x;
-
}
-
this.getY = function():int {
-
return y;
-
}
-
}
-
-
var p:Object = new Pnt(10,10);
-
-
trace(p.getX(), p.getY());
-
p.setX(100);
-
trace(p.getX());
-
-
/*
-
outputs:
-
10, 10
-
100
-
*/
Another way to define and instantiate Objects on the timeline. Interesting, but I don't recommend it over actual classes...
Also posted in OOP, functions | Tagged actionscript, flash |
By Zevan | December 7, 2008
Actionscript:
-
Object.prototype.myVar = "I am a variable";
-
-
var s:Sprite = new Sprite();
-
-
trace(Object(s).myVar);
-
-
var v:Video = new Video();
-
trace(Object(v).myVar);
This.... should not be done... and setting myVar like this:
Actionscript:
-
Object(s).myVar = "hello";
Will cause an error....
I keep a folder on my laptop for this website..... when I have a random snippet idea I put it in this folder.... Every couple of weeks I go through this folder and turn select snippets into posts.... when I rediscovered this snippet today it really made me laugh....
By Zevan | November 11, 2008
Actionscript:
-
// set multiple properties of an Object
-
function setProps(o:*, props:Object):void{
-
for (var key:String in props){
-
o[key] = props[key];
-
}
-
}
-
-
// example:
-
-
var s:Sprite = new Sprite();
-
s.graphics.beginFill(0);
-
s.graphics.drawRect(0,0,10,10);
-
addChild(s);
-
-
// set some properties
-
setProps(s, {x:100, y:100, scaleX:2, scaleY:2, rotation:45});
This was inspired by tweening engines like TweenLite.
Basically the same thing using a with statement:
Actionscript:
-
with(s) x = 100, y = 100, scaleX = 2, scaleY = 2, rotation = 45;
Also posted in properties |
By Zevan | November 30, 1999
Actionscript:
-
// create constants for all letter and number keys:
-
var alphabet:Array = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".split("");
-
var nums:Array = ["ZERO","ONE","TWO","THREE","FOUR","FIVE","SIX","SEVEN","EIGHT","NINE"];
-
-
var key:Object = new Object();
-
for (var i:int = 0; i<alphabet.length; i++)
-
key[alphabet[i]] = 65 + i;
-
-
for (i = 0; i<nums.length; i++){
-
var code:int = 48 + i;
-
key[nums[i]]= code;
-
key[i] = code;
-
}
-
-
-
// test them out
-
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPressed);
-
function onKeyPressed(evt:KeyboardEvent):void {
-
if (evt.keyCode == key.A){
-
trace("the a was pressed");
-
}
-
if (evt.keyCode == key.B){
-
trace("the b was pressed");
-
}
-
if (evt.keyCode == key.NINE){
-
trace("the 9 key was pressed");
-
}
-
if (evt.keyCode == key["0"]){
-
trace("the 0 key was pressed");
-
}
-
}
This is an easy way to store values for alphanumeric keys in variables that make sense... instead of having to do things like this:
Actionscript:
-
// hit the zero key
-
if (evt.keyCode == 48){
-
trace("the zero key was hit");
-
}
Also posted in keys | Tagged actionscript, flash, keys |