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);

Blitting Research


//Blitting Research.
//Used on a file with 3 objects on stage
//that are exported for actionscript:
//They Are:
//[1]:
//Instance S1 on stage:
//Exported as class: "SYM01"
//[2]:
//Instance "S1_copy" on stage:
//Is the SAME symbol in library
//as "S1". Thus should have the
//same .constructor result.
//[3]:
//Instance "S2" on stage:
//Exported as class: "SYM02"

//Theory behind research:
//I want to take movieClips embedded
//on stage or actual classes and pass
//them to my blit constructor.
//The blit constructor will look
//at if the object's class is already
//in the master static vector
//for the class. If it already exists,
//the blitter class will make the
//new bitmap an instance of a bitmap
//data object already in memory.

import flash.utils.Dictionary;
import flash.display.Bitmap;
import flash.display.BitmapData;

var kDict:Dictionary = new Dictionary();

//get class:
var c:Class = S1.constructor;
kDict[c] = 1;

//see if it exists:
if( kDict[c] != null ){
 trace("Test Pass");
}else{
 trace("FAIL");
}

//get another class that does NOT
//exist in dictionary:
var d:Class = S2.constructor;
if( kDict[d] != null ){
 trace("FAIL");
}else{
 trace("Test Pass");
}

//get another instance on the screen
//that has also been exported for actionscript,
//but this one is of the SAME
//class as variable "c".
var c2:Class = S1_Copy.constructor;
if( kDict[c2] != null ){
 trace("Test Pass");
}else{
 trace("FAIL");
}

trace("end of script");

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.

Monday, March 19, 2012

Scrolling Docking UI class Demo

//main computer down. This is F56.swf


My roommate Jaron said I needed a non-intrusive UI for the game I am working on.
Something that only pops up when you hover over it. Here is a test of the
UI component I came up with to solve this problem.

Sunday, March 18, 2012

Pixel Based Collision Detection:

Sometimes for the sake of efficiency you have to cheat a bit.
So, the question is... Do I take every-other pixel on the bounding box
of an object to see if it is colliding with the background? or do I only
take the pixels that make up the boarder?

Well, it depends on the size. Any square bitmap >=7x7 will be more
efficient using the boarder pixels only for collision detection.
Not only that, but we don't have to worry about diagonal 45 degree
pixels shooting through the object without getting detected... Ok...
It could still happen. Depends on the design of your game.


var boxWid:Number = 1;
var square:Number;
var halfDensity:Number = 0;
var boarderOnly:Number = 0;

do{
 boxWid++;
 square=boxWid*boxWid;
 halfDensity = square/2;
 boarderOnly = (boxWid-1)*4;
 
 if(boarderOnly < halfDensity){
  break;
 }
 
}while(true);

trace("boxWid===" + boxWid);


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

Blob Chain Effect



New interactive effect. Check it out. :)

Spacer Comment#2

It would be more professional.

Spacer Comment

Note to self:
Start implementing pause function on all demo code.
That way multiple embedded objects on my blog do not
take up more of the client's processing power than needed.

Saturday, March 10, 2012

Rave on a house boat

You must close all other tabs using flash for this to work.
There is an error in the "computeSpectrum" method in adobe flash
that raises a security error if another window is using flash. :(

If you don't see pulsing light... it's probably not working right.
Also, give it about a minute to load. Sorry. Next-time I'll put in a pre-loader.


Making Space. :)

Two large flash apps should not belong on the same web-page.

Break is slightly faster in a do loop.

//When I say slightly faster, I mean the difference in this
//code is about 20 milliseconds... You should probably just
//use whatever is easiest for.


var xx:int = 0;
var loopTrue:Boolean;
var tt:int;
var t1:int;
var t2:int;
var maxVal:int = 7654321;

t1 = getTimer();
xx=0;
do{
 xx++;
 if(xx>maxVal){break;}
}while(true);
t2 = getTimer();
tt = t2-t1;
trace("method1===" + tt);


t1 = getTimer();
xx=0;
loopTrue=true;
do{
 xx++;
 if(xx>maxVal){loopTrue=false;}
}while(loopTrue);
t2 = getTimer();
tt = t2-t1;
trace("method2===" + tt);

Thursday, March 8, 2012

Explosion Grid



First particle effect I have made. All from my own classes, no 3rd party plugins.
I used linked-lists for super-fast access to each particle in the system.
Once my friend Mike gets the collision code done, we will be implementing this
explosion effect in our game. :) :) :)

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.

Tuesday, March 6, 2012

Ben-Day-Dot Effect

Download Source Here: www.jmim.com/FLASH/F48.zip



I wasn't happy with how fast this one runs...
If you use an image that is too bright or saturated, it may lag... :(

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.