In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

Nunchucks vs ninjas

bad ninjas are attacking now it's time to unleash your badass nunchucks skills!!
Get Adobe Flash player
by Jacky.Riawan 19 Jan 2016
/**
 * Copyright Jacky.Riawan ( http://wonderfl.net/user/Jacky.Riawan )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/9Kmi
 */

package  
{
    import Box2D.Collision.b2AABB;
    import Box2D.Collision.Shapes.b2CircleDef;
    import Box2D.Collision.Shapes.b2PolygonDef;
    import Box2D.Common.Math.b2Vec2;
    import Box2D.Dynamics.b2Body;
    import Box2D.Dynamics.b2BodyDef;
    import Box2D.Dynamics.b2DebugDraw;
    import Box2D.Dynamics.b2World;
    import Box2D.Dynamics.Joints.b2MouseJoint;
    import Box2D.Dynamics.Joints.b2MouseJointDef;
    import Box2D.Dynamics.Joints.b2RevoluteJointDef;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.text.TextField;
    import flash.text.TextFormat;
    import flash.text.TextFieldAutoSize;
    /**
     * ...
     * @author Jacky Riawan
     */
     [SWF(backgroundColor=0xFFFFFF, width=400, height=400, frameRate=40)]
    public class main extends Sprite
    {
        private var world:b2World
        private var myAABB:b2AABB
        private const ratio:int = 30
        private var joint:b2RevoluteJointDef = new b2RevoluteJointDef()
        private var prevBody:b2Body
        private var mouseJoint:b2MouseJoint;
        private var boxBody:b2Body;
        private var isHold:Boolean=false
        private var lightHandle:b2PolygonDef;
        private var heavyHandle:b2PolygonDef;
        private var bodyToDestroyList:Vector.<b2Body> = new Vector.<b2Body>()
        private var emoList:Vector.<Sprite> = new Vector.<Sprite>()
        private var faceList:Vector.<String>=Vector.<String>(["0_o","T_T","@_@",'(>_<")',"t(>o<t)","(X_X)","(>_<#)","(-_-)","(O.O;)","(~_~;)","(^_-;)","(T0T)","(T^T)","(*_*)","(+_+)","(o_ _)o"])
        private var timer:int=0;
        private var myTextFormat:TextFormat=new TextFormat("_sans",10);
        public function main() 
        {
            setupHeavyAndLightDef()
            setupWorld()
            setupDebugDraw()
            createNunchuck()
            showInstruction()
            
            addEventListener(Event.ENTER_FRAME, render)
            stage.addEventListener(MouseEvent.MOUSE_DOWN, downHandler)
            stage.addEventListener(MouseEvent.MOUSE_UP,upHandler)
        }
        
        private function showInstruction():void
        {
            var txt:TextField = new TextField()
            txt.text = "Oh no! bad ninjas are attacking! fend them off with your ultimate nunchucks skill \nHold Mouse to use tight handling\nRelease to use loose handling\nMove mouse to swing"
            txt.textColor = 0xFFF000
            txt.setTextFormat(myTextFormat)
            txt.mouseEnabled=false
            txt.autoSize = TextFieldAutoSize.LEFT
            txt.width=400
            txt.wordWrap=true
            addChild(txt)
        }
        
        private function setupHeavyAndLightDef():void
        {
            heavyHandle = new b2PolygonDef()
            heavyHandle.SetAsBox(20 / ratio, 2.5 / ratio)
            heavyHandle.density = 2000
            heavyHandle.filter.groupIndex = -1
            lightHandle = new b2PolygonDef()
            lightHandle.SetAsBox(20 / ratio, 2.5 / ratio)
            lightHandle.density = 10
            lightHandle.filter.groupIndex=-1
        }
        private function createNinja():void {
            var ninjaSize:Number=.5+Math.random()
            var shape:b2PolygonDef = new b2PolygonDef()
            shape.SetAsBox((8*ninjaSize) / ratio, (15*ninjaSize) / ratio)
            shape.filter.categoryBits=1
            shape.density = .3
            var head:b2CircleDef = new b2CircleDef()
            head.radius = 10*ninjaSize/ratio
            head.localPosition = new b2Vec2(0, -25 * ninjaSize / ratio)
            head.density = .3
            var limb1:b2PolygonDef = new b2PolygonDef()
            limb1.SetAsOrientedBox(25 * ninjaSize / ratio, 3 * ninjaSize / ratio, new b2Vec2(24 * ninjaSize / ratio, 13 * ninjaSize / ratio), 3)
            limb1.density = .3
            var limb2:b2PolygonDef = new b2PolygonDef()
            limb2.SetAsOrientedBox(3 * ninjaSize / ratio, 12 * ninjaSize / ratio, new b2Vec2( -7 * ninjaSize / ratio, 27 * ninjaSize / ratio), .4)
            limb2.density = .3
            var limb3:b2PolygonDef = new b2PolygonDef()
            limb3.SetAsOrientedBox(12 * ninjaSize / ratio, 3 * ninjaSize / ratio, new b2Vec2( -2 * ninjaSize / ratio, 34 * ninjaSize / ratio), 3)
            limb3.density = .3
            var limb4:b2PolygonDef = new b2PolygonDef()
            limb4.SetAsOrientedBox(15 * ninjaSize / ratio, 3 * ninjaSize / ratio, new b2Vec2( -18 * ninjaSize / ratio, -12 * ninjaSize / ratio), .3)
            limb4.density=.3
            var limb5:b2PolygonDef = new b2PolygonDef()
            limb5.SetAsOrientedBox(15 * ninjaSize / ratio, 3 * ninjaSize / ratio, new b2Vec2(15 * ninjaSize / ratio, -20 * ninjaSize / ratio), 2.14)
            limb5.density=.3
            var bodyDef:b2BodyDef = new b2BodyDef()
            bodyDef.position.Set(0 / ratio, Math.random()*200/ratio)
            var body:b2Body = world.CreateBody(bodyDef)
            body.CreateShape(shape)
            body.CreateShape(head)
            body.CreateShape(limb1)
            body.CreateShape(limb2)
            body.CreateShape(limb3)
            body.CreateShape(limb4)
            body.CreateShape(limb5)
            body.SetLinearVelocity(new b2Vec2(500/ratio,100*Math.random()/ratio))
            body.SetMassFromShapes()
            body.m_userData="ninja"
            bodyToDestroyList.push(body)
        }
        private function downHandler(e:MouseEvent):void 
        {
            boxBody.DestroyShape(boxBody.GetShapeList())
            boxBody.CreateShape(heavyHandle)
            boxBody.SetMassFromShapes()
            isHold=true
        }
        private function upHandler(e:MouseEvent):void 
        {
            boxBody.DestroyShape(boxBody.GetShapeList())
            boxBody.CreateShape(lightHandle)
            boxBody.SetMassFromShapes()
            isHold=false
        }
        private function createNunchuck():void
        {
            //part1
            var boxSD:b2PolygonDef = new b2PolygonDef()
            boxSD.SetAsBox(20 / ratio, 2.5 / ratio)
            boxSD.density = 10
            boxSD.filter.groupIndex=-1
            var boxBD:b2BodyDef = new b2BodyDef()
            boxBD.position.Set(mouseX / ratio, mouseY / ratio)
            boxBody = world.CreateBody(boxBD)
            boxBody.CreateShape(lightHandle)
            boxBody.SetMassFromShapes()
            boxBody.m_userData=""
            prevBody=boxBody
            //part 2
            
            var boxBD2:b2BodyDef = new b2BodyDef()
            boxBD2.position.Set((mouseX+75) / ratio, mouseY / ratio)
            var boxBody2:b2Body = world.CreateBody(boxBD2)
            boxBody2.SetBullet(true)
            boxBody2.CreateShape(boxSD)
            boxBody2.SetMassFromShapes()
            boxBody2.m_userData=""
            //chain
            var chainBodyShape:b2PolygonDef = new b2PolygonDef()
            chainBodyShape.SetAsBox(2.5 / ratio, .25 / ratio)
            
            chainBodyShape.filter.groupIndex = -1
            chainBodyShape.filter.categoryBits=1
            for (var i:int = 0; i < 7; i++) {
                var chainSD:b2BodyDef = new b2BodyDef()
                chainBodyShape.density = 1000-i*50
                chainSD.position.Set((mouseX+22.5 + i * 5) / ratio, mouseY / ratio)
                var chainBody:b2Body = world.CreateBody(chainSD)
                chainBody.CreateShape(chainBodyShape)
                chainBody.SetMassFromShapes()
                joint.Initialize(prevBody, chainBody, new b2Vec2(((mouseX+22.5 + i * 5) - 2.5) / ratio, mouseY/ratio))
                world.CreateJoint(joint)
                prevBody=chainBody
            }
            
            joint.Initialize(prevBody, boxBody2, new b2Vec2(((mouseX+22.5 + 7 * 5) - 2.5) / ratio, mouseY / ratio))
            world.CreateJoint(joint)
            var mouseJointDef:b2MouseJointDef = new b2MouseJointDef()
            mouseJointDef.body2 = boxBody
            mouseJointDef.body1 = world.GetGroundBody()
            mouseJointDef.target.Set(mouseX / ratio, mouseY / ratio)
            mouseJointDef.maxForce = 10000000
            mouseJointDef.timeStep = 1 / 80
            mouseJoint=world.CreateJoint(mouseJointDef) as b2MouseJoint
        }
        private function addEmoticon(pos:b2Vec2):void {
            pos.Multiply(ratio)
            var holder:Sprite=new Sprite()
            var emo:TextField = new TextField()
            holder.x = pos.x
            holder.y = pos.y
            holder.mouseChildren = false
            emo.textColor = 0xFFFFFF
            emo.text = faceList[int(faceList.length*Math.random())]
            emo.setTextFormat(myTextFormat)
            
            holder.addChild(emo)
            addChild(holder)
            emoList.push(holder)
        }
        private function render(e:Event):void 
        {
            if (isHold) {
                boxBody.SetAngularVelocity(0)//m_angularVelocity=0
            }
            timer++
            if (timer % Math.max(15,int(30-timer/800)) == 0) {
                createNinja()
            }
            if (world.m_contactCount > 0) {
                if (world.m_contactList.GetShape1().GetBody().GetUserData() == "ninja"&&world.m_contactList.GetShape2().GetBody().GetUserData() == "") {
                    world.m_contactList.GetShape1().GetBody().m_userData=""
                    addEmoticon(world.m_contactList.GetShape1().GetBody().GetPosition())
                }else if (world.m_contactList.GetShape1().GetBody().GetUserData() == ""&&world.m_contactList.GetShape2().GetBody().GetUserData() == "ninja") {
                    world.m_contactList.GetShape2().GetBody().m_userData=""
                    addEmoticon(world.m_contactList.GetShape2().GetBody().GetPosition())
                }
            }
            for (var j:int = 0; j < emoList.length; j++) {
                var myEmo:Sprite = emoList[j]
                myEmo.y--
                myEmo.alpha -= .02
                if (myEmo.alpha < 0) {
                    removeChild(myEmo)
                    emoList.splice(j,1)
                }
            }
            world.Step(1 / 40, 20)
            mouseJoint.SetTarget(new b2Vec2(mouseX / ratio, mouseY / ratio))
            for (var i:int = 0; i < bodyToDestroyList.length; i++) {
                var myBody:b2Body = bodyToDestroyList[i]
                var body_y:Number = myBody.GetPosition().y
                var body_x:Number = myBody.GetPosition().x
                if (body_x<0||body_x>400/ratio||body_y<0||body_y > 400 / ratio) {
                    world.DestroyBody(myBody)
                    bodyToDestroyList.splice(i,1)
                }
            }
        }
        
        private function setupDebugDraw():void
        {
            var sprite:Sprite = new Sprite()
            addChild(sprite)
            var debugMode:b2DebugDraw = new b2DebugDraw()
            debugMode.SetFlags(b2DebugDraw.e_shapeBit)
            debugMode.m_sprite=sprite
            debugMode.m_drawScale = ratio
            debugMode.m_fillAlpha = .3
            world.SetDebugDraw(debugMode)
            
        }
        
        private function setupWorld():void
        {
            myAABB=new b2AABB()
            myAABB.lowerBound.Set(-1000/ratio, -1000/ratio)
            myAABB.upperBound.Set(1000/ratio,1000/ratio)
            world=new b2World(myAABB,new b2Vec2(0,10),true)
        }
        
    }

}