Showing posts with label as3. Show all posts
Showing posts with label as3. Show all posts

Thursday, March 22, 2012

Extended "is" function through reflection, as3

I am making a blitting class for sprite objects.
I want it to make use of objects exported for actionscript
that are embedded on the stage of .FLA file.

If there are 10 objects on the stage that are exported for
actionscript, I want the FIRST one passed to my blitter to
store it's class in a dictionary.

Then when my blitter constructor is called on the other
objects on stage, it will see that bitmap data for that class
already exists, and this new object's bitmap will be linked
to the same bitmap data.

That way, there is only ONE movie clip in memory.

To do this, I need to compare not only the baseClass
but the subClass of objects passed to the constructor.
The baseClass is used to see if the object already exists in the blitter.
The subClasses are used to see what kind of display object I am dealing with.

The class I wrote is as follows:

package com.JMIM03.UT_isType{
 import flash.utils.describeType;
 import flash.utils.getDefinitionByName;
 import flash.utils.getQualifiedClassName;
//       1         2         3         4         5         6         7         8
//345678901234567890123456789012345678901234567890123456789012345678901234567890
//#********************CODE CREDITS******************************************#//
//#                                                        |                 #//
//#         Author: John Mark Isaac Madison                |                 #//
//#         EMAIL : HeavyMetalCookies@Gmail.com            |                 #//
//#         PHONE#: (586)214-3958                          |                 #//
//#         Favorite Music: Industrial                     |                 #//
//#                                                        |                 #//
//#**************************************************************************#//
//345678901234567890123456789012345678901234567890123456789012345678901234567890
//       1         2         3         4         5         6         7         8
 
 //class summary:
 //A class that extends the "is" function
 //so that it can for example, tell that
 //an embedded asset on the stage that is exported
 //for actionscript is indeed of type "MovieClip"
 //or type "Sprite";
 
 public class UT_isType {

  public function UT_isType() {
   // empty constructor code
  }//[constructor]
  
  public function exec(
  classOrInstance:*,
  classType:Class
  ):Boolean
  {//[FN:exec]
   var coi:* = classOrInstance; //shorthand it.
   var theClass:Class;
   
   //Work with a common data type of class:
   //if input was a class, we will work on that.
   //if input was object, get it's class.
   if(coi is Class){
    theClass = coi;
   }else{
    //ATTEMPT#1:
    //first do a simple "is" check:
    if(theClass is classType){ return true; }
    
    var qn:String = getQualifiedClassName(theClass);
    theClass = Class(getDefinitionByName(qn));
   }//[make it a class]
   
   //use describeType to get XML node and parse:
   var typeString:String;
   var classAsXML:XML = describeType(theClass);
   
   //ATTEMPT#2:
   //Get the base class from it's name:
   var baseName:String = classAsXML.@name.toString();
   theClass = Class(getDefinitionByName(baseName));
   if(theClass == classType){
    return true;
   }
   
   
   //ATTEMPT#3:
   //look at extendsClass:
   var ext:XMLList = classAsXML.extendsClass;
   for each (var a:XML in ext) 
   {
    typeString = a.@type.toString();
    theClass = Class(getDefinitionByName(typeString));
    if(theClass == classType){return true;}
    
   }
   
   //ATTEMP#4:
   //look at factory node:
   var fex:XMLList = classAsXML.descendants("factory").extendsClass;
   for each (var a:XML in fex) 
   {
    typeString = a.@type.toString();
    theClass = Class(getDefinitionByName(typeString));
    if(theClass == classType){return true;}
   }
   
   //if all attempts fail, return false:
   return false;
   
  }//[FN:exec]
 }//class
}//package


//The test code:
//requires an obect on the stage with an instance id of "S2".
//that has been exported for actionscript in the library.

//S2 is an object on the stage that has
//been exported for actionscript:
import com.JMIM03.UT_isType.UT_isType;

var c2:Class = S2.constructor;
var t:Function = trace;
var T:String = "TRUE";
var F:String = "FALSE";
if(c2 is MovieClip){ t(T); }else{ t(F); }
if(c2 is Sprite)   { t(T); }else{ t(F); }
if(c2 is DisplayObjectContainer){t(T);  }
if(c2 is c2){ t("c2 is c2"); }else{t("c2 not c2");}

var isType:UT_isType = new UT_isType();
var tf:Boolean = false;
tf = isType.exec(c2,MovieClip); trace("tf==" + tf);
tf = isType.exec(c2,Sprite); trace("tf==" + tf);
tf = isType.exec(c2,DisplayObjectContainer); trace("tf==" + tf);
tf = isType.exec(c2,c2); trace("tf==" + tf);

Tuesday, March 20, 2012

Nine(9) simple AI Types

//main computer down. This is F57.swf


Jaron said my game needed some sort of AI, so last night and early this
morning I put together 9 different AI types. They all use the same interface
and have the same base-class.
They shall all link together in a linked list.
The first item in the linked list will be a "root" node.
The root node may NOT be deleted.
If the "unlinkAndDie" function is called on the root note, it will
throw an error because it's variable "hasPrevious" will be false.
Cannot get rid of first object in chain because if we do we kill our entry
point to the linked list of enemies.

Friday, March 16, 2012

Chain Stalker Class



A class I wrote that hooks up objects into a chain.
The chain has a tolerance and all objects try to maintain s set distance
away from it's target object (+-) the tolerance value.

In this example, there are 4 chains, the head of each chain being the
ball in the center that says "click+drag".
It kind of makes for a quick&dirty AI for enemies.

Tuesday, March 13, 2012

Wednesday, March 7, 2012

Flash As3 Audio Visualizer

Sorry, may take some time to load.

//This is the same thing, but hosted on deviant-art.
//Use whichever loads first. :)




My first attempt at making an audio visualizer effect for flash.

Accessing Properties is SLOW.

I was having a really tough time making the trivial decision on how to store
an array of point data. My "Ben-Day-Dots" effects sometimes lagged, so I was
thinking of some optimizations I could do to it to make it faster.
I am working on an graphic equalizer that will also use point data,
so I wanted to not make the same mistake I made in my last effect.

Output:
tt==100___METHOD ONE(1)
tt==58___METHOD TWO(2)
tt==43___METHOD THREE(3)


    //timer vars:
    var tt:int = 0;
    var t1:int = 0;
    var t2:int = 0;

    //timer loop vars:
    var ii:int = 0;
    var MX:int = 6543; //max iterations.

    //What is faster... A vector of points, or a vector of Numbers?
    var pVec:Vector.<Point> = new Vector.<Point>(255);
    var nv_X:Vector.<Number> = new Vector.<Number>(255);
    var nv_Y:Vector.<Number> = new Vector.<Number>(255);
    var xx:int = 0;
    var pVar:Point;
    var xNum:Number;
    var yNum:Number;

    //Populate vectors with same numbers:
    for(xx = 0; xx<256; xx++){
        pVec[xx] = new Point(xx,xx);
        nv_X[xx] = xx;
        nv_Y[xx] = xx;
    }

    //access method#1
    t1 = getTimer();
    for(ii=0; ii<MX; ii++){
        //access method#1
        for(xx = 0; xx<256; xx++){
            xNum = pVec[xx].x;
            yNum = pVec[xx].y;
        }
    }
    t2 = getTimer();
    tt = t2-t1;
    trace("tt==" + tt + "___METHOD ONE(1)");

    //access method#2
    t1 = getTimer();
    for(ii=0; ii<MX; ii++){
        //access method#2
        for(xx = 0; xx<256; xx++){
            pVar = pVec[xx];
            xNum = pVar.x;
            yNum = pVar.y;
        }
    }
    t2 = getTimer();
    tt = t2-t1;
    trace("tt==" + tt + "___METHOD TWO(2)");

    //access method#3
    t1 = getTimer();
    for(ii=0; ii<MX; ii++){
        //access method#3
        for(xx = 0; xx<256; xx++){
            xNum = nv_X[xx];
            yNum = nv_Y[xx];
        }
    }
    t2 = getTimer();
    tt = t2-t1;
    trace("tt==" + tt + "___METHOD THREE(3)");

Phantom Paint As3



I've been trying to be resourceful with my code-library and make more effects
by using what I already have rather than coding a complete new class.
Click in the black square to paint and see the results.

Monday, March 5, 2012

Saturday, March 3, 2012

GlassFire Effect



GlassFire effect I made. I wanted to make flames but I didn't want to do the
usual Perlin noise multiplied by a luminosity mat approach. It thought back to
a really cool effect I made many years back in high-school and decided I'd recreate
it in flash.

Monday, February 27, 2012

Looping Wobbly Surface, coded in AS3



Still working on getting it real-time.
Need to make the tree have a "smart-update" option when updating recursively.

Sunday, February 26, 2012

Pre-Rendered Explosion in AS3



If I can make a script to center these bitmaps into a movieClip,
pre-rendered explosion effects may be a good way to go.

Self-Respect has been earned today.
I must now reward myself with caffeinated sugar water.

Thursday, February 23, 2012

Clear Dictionary AS3


//Use two steps to avoid changing the dictionary while iterating over it.
//Also, the keys are type "Object" not "String. For speed, use a vector and
//store it's length in a variable before iterating over it. Lastly, just to be safe,
//nullify entries in vector as you go along. Since the whole point of clearing out
//a dictionary in the first place is to de-reference the items within it.

function clearD(d:Dictionary):void{
    //Get Keys from dictionary.
    var idVec:Vector.<Object> = new Vector.<Object>(0);
    for(var obj:Object in d){
        idVec.push(obj);
    }//[next obj]
    
    //Delete Keys from dictionary and clear from vector at same time.
    var vLen:int = idVec.length;
    for(var vi:int = 0; vi<vLen; vi++){
        delete d[ idVec[vi] ];
        idVec[vi] = null;
    }//[next vi]
}//[FN:clearD]

Tuesday, February 21, 2012

Problem with OH_CenterOfUniverse



I had more ambitious goals for my next update...
But instead spent a lot of time getting bugs out of existing code
and polishing up the UI functionality a bit. Scrolling background is not
parallaxing correctly because it is wired to a value that is tessellating/wrapping.
That value coming from my "OH_CenterOfUniverse" class... I swear there is no tessellation code in that class... Mind is fried. Just going to post what I have.

Friday, February 17, 2012

String Swapper App for my personal coding use


I made this app because I was sick of redundantly swapping out X an Y's in code.

A text-editor takes 3 steps and requires a proxy variable.
Example:
Step1: X replaced to K.
Step2: Y replaced to X.
Step3: K replaced to Y.

My App Requires ONE(1) step:
Step1: X swapped with Y.

Done!

Unfortunately, I thought this code was going to be simple...
And as a result of my hubris, this function is NOT token based.
Which means that if you tried to swap "A" with "APPLE" you might
have a problem since "A" is contained within "APPLE".

Good news though, for complex variable names, you can put in a list of
things you want swapped and do it in one operation!

Thursday, February 9, 2012

custom bitArray decision AS3

A custom "bitArray" made with vectors and uInts is FASTER than
using a bitmapData object as a bitArray... 6X faster... now need to
test my assumptions against a byteArray object to see if my method is also
faster than that.

I would post code... But the bit shifting operators ">>>" break the HTML
in my post and the code and pre tags are not working. :(

Parallaxing and Tesselating background code finished!





This didn't get done as soon as I wanted due to some hard drive failure.
Recovery wasn't too tedious since I backup often... But it did make me take
most of the day to restore my files and then re-think my backup strategy
so next time restoration is quicker when something goes wrong.

Tuesday, February 7, 2012

parallaxing background code as3


Need Sleep...


Though it looks like I am shifting MANY tiles positions....
The positions of all the tiles are STATIC. But, I have one master tile
that is sliced, diced, and pieced back together so that it's top-left corner can
end up anywhere inside itself.

In simpler terms: The tiles are not moving.
They are static tiles with animations on them giving the illusion of movement.

Tuesday, January 31, 2012

Loader And Functional Menu Framework complete


Thankyou Tyler for the music.
The "pre-loader" I made worked fine when testing on my computer in CS5...
But something is going wrong here... I think that... The pre-loader glitches
out when it tries to load but the .swf is not in focus. Just a hunch.

Update:
After trying multiple versions of loading I have decided the best way to
get the loader to work is by hard-coding the file-sizes to be downloaded in the
swf.
I just tried a 3rd party plugin with file: "F37_D" (version D) and it still had
the same problem. I hear this can be solved with a server side solution that
can answer requests for file sizes. BUT... Lets patch it up the simple way.
My loader class will have an optional parameter for a file-size override.
When in effect, it ignores the file-size given by flashes progress event.

Update#2:2012.02.03[5:49PM]
Version D has had the 3rd party plugin removed.
For the time being, my solution is a simple "sizeOverride"
parameter in the items I add to the loader.

As3 TLFTextField instance problems: may be a struct, not a class





I made a class that takes TLFTextfield objects on the stage and passes them to a constructor that uses them to setup an instance of my PL_PreLoader class. While testing I found that it looks like TLFTextFields do not work like ordinary display objects in that when I passed their instance name to my constructor they seem to have been passed by value rather than reference. This would explain why there are now DUPLICATE copies of each TLFTextfield sent to my constructor. I will do another simle test to see if this theory holds. I am thinking maybe TLFTextField internally is actually a STRUCT and not a CLASS.

Update: Further testing shows this problem only happens when the text object is
passed to a class that extends Sprite or MovieClip. And my guess, any display object. Also interesting is that this duplication only happens ONCE. Meaning taking the TLFTextField instance on stage and passing it multiple times to constructors of a class that extends sprite... the cloning will only happen once.