Select Page

Restricting a number to a certain range

Quite often I need to restrict a number to a range with a defined maximum and minimum value. Rather than using if else statements you can do this simply with Math.max and Math.min:

var restricted_value:Number = Math.max(MIN_VALUE, Math.min(MAX_VALUE, value));

AS3 debugging with Arthropod

I was just put on to a great debugging tool called Arthropod.

It is highly configurable (including password protection) and includes many very handy features such as being able to switch between standard window behaviour and staying ‘always on top’. Also you can take a screenshot of your stage and send the bitmap through and it will show it to you in a new window!

Animated Rising Sun

Here is a small and simple class I wrote this morning to create an animated rising sun. You can configure the number of segments and the size very easily.

risingsun

package 
{
    import flash.display.Sprite;
    import flash.events.Event;
    
    public class RisingSun extends Sprite 
    {
        private var _segments    :Number = 40;
        private var _rad        :Number = 730;
        
        public function RisingSun()
        {
            super();
            
            x = 364;
            y = 45;
            
            graphics.clear();
            graphics.beginFill(0xFF0000, 0.8);
            graphics.drawCircle(0, 0, _rad);
            graphics.endFill();
            
            var _angle:Number = Math.PI / _segments;
            var a:Number = _angle;
            
            for (var i:Number = 0; i < _segments; i++)
            {
                var s:Sprite = new Sprite();
                s.graphics.clear();
                s.graphics.beginFill(0xCC0000, 0.5);
                s.graphics.lineTo(Math.cos(a)*_rad, Math.sin(a)*_rad);
                a += _angle;
                s.graphics.lineTo(Math.cos(a)*_rad, Math.sin(a)*_rad);
                s.graphics.lineTo(0, 0);
                s.graphics.endFill();
                a += _angle;
                addChild(s);
            }
            addEventListener(Event.ENTER_FRAME, _frame);
        }
        
        private function _frame(e:Event):void
        {
            rotation += 0.5;
        }
    }
}