AS3 单例类

没有评论
package 
{
    /**
     * 单例类 范例
     * 在AS3里 单例类一般用 包外类 实现.(我以前也总是这样!)
     * 但不能用编译时类型检测,容易出错
     * 
     * 多申请一个包外类则
     *   既有了保外类实现单例类的优点,
     *   又可以在编译时进行类型检测,还有代码提示
     * 
     * www.lite.cn (lite3)
     * lite3@qq.com
     * qq:735486078
     * 欢迎访问我的博客 
     * 
     * @author lite3
     */
    public class SingleClass 
    {
        static private var instance:SingleClass;  /* 单例类实例 */

        static public function getInstance():SingleClass
        {
            if (!instance) instance = new SingleClass(new one());

            return instance;
        }

        public function SingleClass(p:one) 
        {
            if (!p)
            {
                /**
                 * 报错后,如果捕获    则返回为 null
                 * 如果未捕获    则直接挂掉,O(∩_∩)O~
                 */
                throw new Error("SingleClass不能直接new , 用静态方法 getInstance()!");
            }
        }

        // ...........
        // to do your code  
    }
}
class one { }
标签: , ,

像素级碰撞检测类

2 条评论
//像素级碰撞检测
package 
{
    import flash.display.BitmapData;
    import flash.display.BlendMode;
    import flash.display.DisplayObject;
    import flash.display.Sprite;
    import flash.geom.ColorTransform;
    import flash.geom.Matrix;
    import flash.geom.Point;
    import flash.geom.Rectangle;

    public class HitTest
    {
        /**
        * --------------------- 像素级碰撞检测
        * @param  target1    DisplayObject
        * @param  target2    DisplayObject
        * @param  accurracy  Number    检测的精度 [0 , 1]  0 <= n <= 1
        * @return        Boolean  
        */
        public static function complexHitTestObject ( target1:DisplayObject, target2:DisplayObject, accurracy:Number = 1 ):Boolean
        {
            accurracy > 1 ? accurracy = 1 : 0;
            return accurracy <= 0?false:complexIntersectionRectangle(target1,target2,accurracy).width != 0;
        }

        /**
         * -------------------获取矩形边框重叠区域
         * @param  target1    DisplayObject
         * @param  target2    DisplayObject
         * @return        Rectangle
         */
        public static function intersectionRectangle ( target1:DisplayObject, target2:DisplayObject ):Rectangle
        {
            // If either of the items don't have a reference to stage, then they are not in a display list
            // or if a simple hitTestObject is false, they cannot be intersecting.
            if ( !target1.root || !target2.root || !target1.hitTestObject( target2 ) )
            {
                return new Rectangle ;
            }

            // Get the bounds of each DisplayObject.
            var bounds1:Rectangle = target1.getBounds( target1.root );
            var bounds2:Rectangle = target2.getBounds( target2.root );

            // Determine test area boundaries.
            var intersection:Rectangle = new Rectangle();
            intersection.x = Math.max( bounds1.x, bounds2.x );
            intersection.y = Math.max( bounds1.y, bounds2.y );
            intersection.width = Math.min( ( bounds1.x + bounds1.width ) - intersection.x, ( bounds2.x + bounds2.width ) - intersection.x );
            intersection.height = Math.min( ( bounds1.y + bounds1.height ) - intersection.y, ( bounds2.y + bounds2.height ) - intersection.y );

            return intersection;
        }

        /**
         * -------------------------- 获取像素重叠区域
         * @param  target1    DisplayObject
         * @param  target2    DisplayObject
         * @param  accurracy  Number    精度
         * @return        Rectangle
         */
        public static function complexIntersectionRectangle ( target1:DisplayObject, target2:DisplayObject, accurracy:Number = 1 ):Rectangle
        {
            if ( accurracy <= 0 )
            {
                throw new Error("ArgumentError: Error #5001: Invalid value for accurracy",5001);
            }

            // If a simple hitTestObject is false, they cannot be intersecting.
            if ( !target1.hitTestObject( target2 ) )
            {
                return new Rectangle ;
            }

            var hitRectangle:Rectangle = intersectionRectangle( target1, target2 );
            // If their boundaries are no interesecting, they cannot be intersecting.
            if ( hitRectangle.width * accurracy <1 || hitRectangle.height * accurracy <1 )
            {
                return new Rectangle ;
            }

            var bitmapData:BitmapData = new BitmapData( hitRectangle.width * accurracy, hitRectangle.height * accurracy, false, 0x000000 );

            // Draw the first target.
            bitmapData.draw ( target1, HitTest.getDrawMatrix( target1, hitRectangle, accurracy ), new ColorTransform( 1, 1, 1, 1, 255, -255, -255, 255 ) );
            // Overlay the second target.
            bitmapData.draw ( target2, HitTest.getDrawMatrix( target2, hitRectangle, accurracy ), new ColorTransform( 1, 1, 1, 1, 255, 255, 255, 255 ), BlendMode.DIFFERENCE );

            // Find the intersection.
            var intersection:Rectangle = bitmapData.getColorBoundsRect( 0xFFFFFFFF,0xFF00FFFF );

            bitmapData.dispose ();

            // Alter width and positions to compensate for accurracy
            if ( accurracy != 1 )
            {
                intersection.x /= accurracy;
                intersection.y /= accurracy;
                intersection.width /= accurracy;
                intersection.height /= accurracy;
            }
            intersection.x += hitRectangle.x;
            intersection.y += hitRectangle.y;

            return intersection;
        }

        /**
         * -------------------------获取MC的矩阵
         * @param  target      DisplayObject
         * @param  hitRectangle  Rectangle
         * @param  accurracy    Number
         * @return          Matrix
         */
        protected static function getDrawMatrix ( target:DisplayObject, hitRectangle:Rectangle, accurracy:Number ):Matrix
        {
            var localToGlobal:Point;
            var matrix:Matrix;
            var rootConcatenatedMatrix:Matrix = target.root.transform.concatenatedMatrix;

            localToGlobal = target.localToGlobal( new Point( ) );
            matrix = target.transform.concatenatedMatrix;
            matrix.tx = localToGlobal.x - hitRectangle.x;
            matrix.ty = localToGlobal.y - hitRectangle.y;

            matrix.a = matrix.a / rootConcatenatedMatrix.a;
            matrix.d = matrix.d / rootConcatenatedMatrix.d;
            if ( accurracy != 1 )
            {
                matrix.scale ( accurracy, accurracy );
            }

            return matrix;
        }
    }
}

另类菜单效果

没有评论

这是模仿的一个网站的菜单效果,现在那个网站也不存在了,就不放链接了。O(∩_∩)O~
菜单内容没做,O(∩_∩)O~
阅读全文...

标签: , ,

判断字符串是否有中文

没有评论

用的是正则表达式 + UTF-8

/**
 * 测试是否有中文, 中文标点测试不出来
 */
function testChinese(str:String):Boolean
{
    return / .*[\u4e00 -\u 9fa5] + .*$ / .test(str);
}

纯AS相册, 未完

4 条评论

上午有个朋友让我帮他改相册, 是flash AS2的, 然后就想弄个纯AS的,O(∩∩)O~ 未完成的,O(∩∩)O~,完成了放 源码上来, 加载有点慢,请谅解 阅读全文...

标签: ,

位移操作符

2 条评论

位移操作符都好理解。

>>>>> 要区别下。

  • >> 所有位都向右移动某位,空出来的位用 0 填充,最后变成有符号的
  • >>> 所有位都向右移动某位,空出来的位用 0 填充,最后变成无符号的

示例

var unum:uint = 0xFFFFFF;
trace(unum<< 16 >> 16);     // -1;
trace(unum<< 16 >>> 16);    // 65535;
标签: , ,

鼠标扩展类 MouseUtil

5 条评论
这是一个鼠标扩展类demo的效果,如果你看不到这个flash,请到文章页面查看!

上面是我写的鼠标扩展类MouseUtil.as的示例。
阅读全文...

标签: , ,

在XMLList里插入节点

1 条评论
var xml:XML = new XML(  <data>
                            <user>
                                <user id="1"/>
                                <user id="2"/>
                            </user>
                        </data> );

var xmlList:XMLList = xml.user.user;
var myXML:XML = new XML(<user id="3"/>);
insertXML(xmlList, myXML);
trace("-------- xmlList -----------");
trace(xmlList);
trace("---------- xml -------------");
trace(xml);

function insertXML(xmlList:XMLList, xml:XML):void
{
    // 修改xmlList,建议用这个
    xmlList[xmlList.length()] = xml;
    // 不修改xmlList 
    //xmlList.parent().appendChild(xml);
}
标签: , , , ,

TextArea TextField 文本 自动滚动到最后一行

1 条评论

2009.10.14更新
由于Flex的文件比较大,所以这里只做了TextField的示例, 其他的原理都一样的。

这是一个文本框即时显示到最后一行的效果,如果你看不到这个flash,请到文章页面查看!

阅读全文...

检测flash的运行在本地还是网络

没有评论
trace(loaderInfo.url.indexOf("file:///") != -1);
trace(new LocalConnection().domain == "localhost");
trace(Security.sandboxType != "remote");
标签: ,
回到顶部