Showing posts with label snippet. Show all posts
Showing posts with label snippet. Show all posts

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


Thursday, February 23, 2012

Object type can work like dictionary as3

//Did not know this could be done:
var mc:MovieClip = new MovieClip();
mc["x"] = 0; //works like mc.x
mc["y"] = 0; //works like mc.y

//Also, while trying to make a function to put objects in a "global dictionary",
//I realized that... I need an "Object" instance... not a "Dictionary" instance.
var obj:Object = new Object();
obj["kay"] = "123"; //store value
obj["pika"] = mc; //store object

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]

Saturday, February 18, 2012

HereDoc As3


var myString:String = (<![CDATA[
                        Here is my string
                        that spans multiple
                        lines.
                       ]]> ).toString();

found here:
http://dougmccune.com/blog/2007/05/15/multi-line-strings-in-actionscript-3/

Thursday, February 16, 2012

Swap without temp variable, AS3


var in_X1:int = -2;
var in_X2:int = 7;

in_X1 = in_X1 + in_X2; //1 & 2
in_X2 = in_X1 - in_X2; //1 & 2 remove 2, now is original 1.
in_X1 = in_X1 - in_X2; //1 & 2 remove 1, now is original 2.

trace("in_X1 == " + in_X1);
trace("in_X2 == " + in_X2);

tessellate signed number into positive range

//Tessellate negative or positive value into range: 0-4.
var xNum = 5; //means that we will modulate between 0 to 4.
var outp:int;
for (var i:int = -10; i<10; i++){
 outp = i % xNum;
 if(outp<0){outp = xNum + outp;}
 trace("["+i+"] --->" + outp );
}