Archive for September, 2010:
Trace MovieClips Under Mouse
This is handy for those times when something is blocking a mouse action and you need to see what is under your mouse.
import flash.geom.Point;
stage.addEventListener (MouseEvent.CLICK, _checkMouseEventTrail, false, 0, true);
function _checkMouseEventTrail ($e:MouseEvent):void
{
var pt:Point = new Point (mouseX, mouseY);
var objects:Array = this.getObjectsUnderPoint (pt);
for (var i:int = 0; i< objects.length; i++)
{
trace(">>", objects[i].name,": ",objects[i]);
}
var p:* = $e.target;
while (p)
{
trace(">>", p.name,": ",p);
p = p.parent;
}
};
stage.addEventListener (MouseEvent.CLICK, _checkMouseEventTrail, false, 0, true);
function _checkMouseEventTrail ($e:MouseEvent):void
{
var pt:Point = new Point (mouseX, mouseY);
var objects:Array = this.getObjectsUnderPoint (pt);
for (var i:int = 0; i< objects.length; i++)
{
trace(">>", objects[i].name,": ",objects[i]);
}
var p:* = $e.target;
while (p)
{
trace(">>", p.name,": ",p);
p = p.parent;
}
};
1 Comment
Global vars in AS3
Found a solution to store variables in a class file that all other classes can access.
// make a class called GlobalVars.as and put inside com folder
package com
{
public class GlobalVars
{
public static var vars:Object = {};
}
}
// then in your other classes
import com.GlobalVars;
GlobalVars.vars.whatever = "something to store"
// then in any other class just do your import of com.GlobalVars, then:
trace (GlobalVars.vars.whatever) // returns "something to store"
package com
{
public class GlobalVars
{
public static var vars:Object = {};
}
}
// then in your other classes
import com.GlobalVars;
GlobalVars.vars.whatever = "something to store"
// then in any other class just do your import of com.GlobalVars, then:
trace (GlobalVars.vars.whatever) // returns "something to store"
AS3 Odd or Even
A function to check a number is odd or even which return true is even and false is odd.
function isEven(num)
{
// if(num % 2 == 0){return true;}else{return false;} //<–old
return !(num%2);//shorter
// return !(num & 1);//seems the fastest one
}
trace(isEven(2));//–> true
trace(isEven(3));//–> false
{
// if(num % 2 == 0){return true;}else{return false;} //<–old
return !(num%2);//shorter
// return !(num & 1);//seems the fastest one
}
trace(isEven(2));//–> true
trace(isEven(3));//–> false
Track Flash in Google Analytics using GAforFlash
GA for flash component enables you to easily make calls directly from your ActionScript code in order to track pageviews, events, conversions, etc.
// For Google Analytics tracking to work you need to link
// to the following two SWC files in Flash CS4 and above ...
// 'analytics.swc' and 'analytics_flash.swc'
// Download them from here http://code.google.com/p/gaforflash/
import com.google.analytics.GATracker;
import com.google.analytics.AnalyticsTracker;
var tracker:AnalyticsTracker;
//GATracker(display:DisplayObject, account:String, mode:String = "AS3", visualDebug:Boolean = false, config:Configuration = null, debug:DebugConfiguration = null);
tracker = new GATracker(this, "UA-XXXXXXX-X", "AS3", true);
// Google Analytics Event Tracking Example
// trackEvent(category:String, action:String, label:String = null, value:Number);
tracker.trackEvent("MyProject", "ButtonClicked", "HomeButton");
tracker.trackPageview("Home/Click/Start");
// to the following two SWC files in Flash CS4 and above ...
// 'analytics.swc' and 'analytics_flash.swc'
// Download them from here http://code.google.com/p/gaforflash/
import com.google.analytics.GATracker;
import com.google.analytics.AnalyticsTracker;
var tracker:AnalyticsTracker;
//GATracker(display:DisplayObject, account:String, mode:String = "AS3", visualDebug:Boolean = false, config:Configuration = null, debug:DebugConfiguration = null);
tracker = new GATracker(this, "UA-XXXXXXX-X", "AS3", true);
// Google Analytics Event Tracking Example
// trackEvent(category:String, action:String, label:String = null, value:Number);
tracker.trackEvent("MyProject", "ButtonClicked", "HomeButton");
tracker.trackPageview("Home/Click/Start");
AS3 Math
A very useful reminder for Math function.
n = Math.random();
// 0 <= n < 1
n = Math.round(x);
// rounds x to the closest integer
n = Math.floor(x);
// rounds x downwards to the closest integer
n = Math.ceil(x);
// rounds x upwards to the closest integer
n = Math.round(Math.random()*10);
// 0 <= n <= 10
n = Math.ceil(Math.random()*10);
// 1 <= n <= 10
n = Math.floor(Math.random()*10);
// 0 <= n <= 9
n = Math.random()*10 + 5;
// 5 <= n <= 15
n = Math.round(Math.random())*2 - 1;
// n will randomly be -1 or 1
// 0 <= n < 1
n = Math.round(x);
// rounds x to the closest integer
n = Math.floor(x);
// rounds x downwards to the closest integer
n = Math.ceil(x);
// rounds x upwards to the closest integer
n = Math.round(Math.random()*10);
// 0 <= n <= 10
n = Math.ceil(Math.random()*10);
// 1 <= n <= 10
n = Math.floor(Math.random()*10);
// 0 <= n <= 9
n = Math.random()*10 + 5;
// 5 <= n <= 15
n = Math.round(Math.random())*2 - 1;
// n will randomly be -1 or 1
