Category Archives: Object

var obj:Object = new Function()

Actionscript:
  1. var Pnt:Function = function(xp:Number, yp:Number){
  2.    
  3.     var x:Number = xp;
  4.     var y:Number = yp
  5.    
  6.     this.setX = function(v:int):void{
  7.         x = v;
  8.     }
  9.     this.setY = function(v:int):void{
  10.         y = v;
  11.     }
  12.     this.getX = function():int {
  13.         return x;
  14.     }
  15.     this.getY = function():int {
  16.         return y;
  17.     }
  18. }
  19.  
  20. var p:Object = new Pnt(10,10);
  21.  
  22. trace(p.getX(), p.getY());
  23. p.setX(100);
  24. trace(p.getX());
  25.  
  26. /*
  27. outputs:
  28. 10, 10
  29. 100
  30. */

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 , | Leave a comment

Object.prototype Crap

Actionscript:
  1. Object.prototype.myVar = "I am a variable";
  2.  
  3. var s:Sprite = new Sprite();
  4.  
  5. trace(Object(s).myVar);
  6.  
  7. var v:Video = new Video();
  8. trace(Object(v).myVar);

This.... should not be done... and setting myVar like this:

Actionscript:
  1. 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....

Also posted in dynamic | Tagged , , | Leave a comment

Set Multiple Properties

Actionscript:
  1. // set multiple properties of an Object
  2. function setProps(o:*, props:Object):void{
  3.     for (var key:String in props){
  4.          o[key] = props[key];
  5.     }
  6. }
  7.  
  8. // example:
  9.  
  10. var s:Sprite = new Sprite();
  11. s.graphics.beginFill(0);
  12. s.graphics.drawRect(0,0,10,10);
  13. addChild(s);
  14.  
  15. // set some properties
  16. 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:
  1. with(s) x = 100, y = 100, scaleX = 2, scaleY = 2, rotation = 45;

Also posted in properties | Leave a comment

KeyCode Variables

Actionscript:
  1. // create constants for all letter and number keys:
  2. var alphabet:Array = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".split("");
  3. var nums:Array = ["ZERO","ONE","TWO","THREE","FOUR","FIVE","SIX","SEVEN","EIGHT","NINE"];
  4.  
  5. var key:Object = new Object();
  6. for (var i:int  = 0; i<alphabet.length; i++)
  7.      key[alphabet[i]] = 65 + i;
  8.      
  9. for (i = 0; i<nums.length; i++){
  10.      var code:int = 48 + i;
  11.      key[nums[i]]= code;
  12.      key[i] = code;
  13. }
  14.      
  15.  
  16. // test them out
  17. stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPressed);
  18. function onKeyPressed(evt:KeyboardEvent):void {
  19.     if (evt.keyCode == key.A){
  20.         trace("the a was pressed");
  21.     }
  22.     if (evt.keyCode == key.B){
  23.         trace("the b was pressed");
  24.     }
  25.     if (evt.keyCode == key.NINE){
  26.         trace("the 9 key was pressed");
  27.     }
  28.     if (evt.keyCode == key["0"]){
  29.         trace("the 0 key was pressed");
  30.     }
  31. }

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:
  1. // hit the zero key
  2. if (evt.keyCode == 48){
  3.    trace("the zero key was hit");
  4. }

Also posted in keys | Tagged , , | Leave a comment