{"id":316,"date":"2009-06-15T17:15:51","date_gmt":"2009-06-16T00:15:51","guid":{"rendered":"https:\/\/actionsnippet.com\/?p=1543"},"modified":"2009-06-15T17:15:51","modified_gmt":"2009-06-16T00:15:51","slug":"quickbox2d-w-key-controls","status":"publish","type":"post","link":"https:\/\/actionsnippet.com\/?p=316","title":{"rendered":"QuickBox2D w\/ Key Controls"},"content":{"rendered":"<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\nimport com.actionsnippet.qbox.*;\r\nimport Box2D.Common.Math.*\r\n\r\n&#x5B;SWF(backgroundColor=0xEFEFEF, width=700, height=600, frameRate=60)]\r\n\r\nvar sim:QuickBox2D = new QuickBox2D(this);\r\n\r\nsim.setDefault({lineColor:0xCC0000, fillColor:0xCC0000});\r\n\r\n\/\/ create compound shape (two circles and a box for the character)\r\nvar charParts:Array = &#x5B;];\r\n\/\/ x and y position are now relative to center of compound shape\r\ncharParts&#x5B;0] = sim.addBox({x:0, y:0, width:1, height:2});\r\ncharParts&#x5B;1] = sim.addCircle({x:0, y:-1, radius:0.5});\r\ncharParts&#x5B;2] = sim.addCircle({x:0, y:1, radius:0.5});\r\nvar char:QuickObject = sim.addGroup({objects:charParts, x:2, y:2.5, allowSleep:false, angularDamping:0.8, linearDamping:1.5});\r\n\r\n\/\/ vector for linear velocity of character\r\nvar charVel:b2Vec2 = new b2Vec2();\r\n\r\n\/\/ angular velocity of character\r\nvar charVelAng:Number = 1;\r\nchar.body.SetAngularVelocity(charVelAng);\r\n\r\n\/\/ world\/platforms\r\nsim.setDefault({lineColor:0x666666, fillColor:0x666666, height:0.5, density:0});\r\nsim.createStageWalls();\r\nsim.addBox({x:3, y:5, width:5});\r\nsim.addBox({x:11, y:5, width:5});\r\nsim.addBox({x:8, y:9, width:8});\r\nsim.addBox({x:4, y:13, width:8});\r\nsim.addCircle({x:16, y:8, radius:2});\r\nsim.addCircle({x:12, y:15, radius:2});\r\n\r\n\/\/ falling circles\r\nsim.setDefault({lineColor:0x2870B5, fillColor:0x2870B5});\r\nfor (var i:int = 0; i&lt;15; i++){\r\n\tsim.addCircle({x:5 + i, y:2, radius:0.25 ,density:1});\r\n}\r\n\r\nsim.start();\r\n\r\n\/\/ key controls\r\naddEventListener(Event.ENTER_FRAME, onLoop);\r\nfunction onLoop(evt:Event):void {\r\n\tcharVel = char.body.GetLinearVelocity();\r\n\tcharVelAng =  char.body.GetAngularVelocity();\r\n\t\r\n\tif (key&#x5B;Keyboard.RIGHT]){\r\n\t\tcharVel.x += 1\r\n\t\tchar.body.SetLinearVelocity(charVel);\r\n\t\t\r\n\t\tcharVelAng += 1;\r\n\t\tchar.body.SetAngularVelocity(charVelAng);\r\n\t}\r\n\tif (key&#x5B;Keyboard.LEFT]){\r\n\t\tcharVel.x -=1;\r\n\t\tchar.body.SetLinearVelocity(charVel);\r\n\t\t\r\n\t\tcharVelAng -= 1;\r\n\t\tchar.body.SetAngularVelocity(charVelAng);\r\n\t}\r\n\tif (key&#x5B;Keyboard.UP]){\r\n\t\t charVel.y = -10;\r\n\t     char.body.SetLinearVelocity(charVel);\r\n\t\t\t\r\n\t\t charVelAng *= 0.8;\r\n\t\t char.body.SetAngularVelocity(charVelAng);\r\n\t} \r\n}\r\n\/\/ basic key setup\r\nvar key:Object = new Object();\r\nstage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPressed);\r\nstage.addEventListener(KeyboardEvent.KEY_UP, onKeyReleased);\r\nfunction onKeyPressed(evt:KeyboardEvent):void {\r\n\tkey&#x5B;evt.keyCode] = true;\r\n\tkey.keyCode = evt.keyCode;\r\n}\r\nfunction onKeyReleased(evt:KeyboardEvent):void { key&#x5B;evt.keyCode] = false}\r\n<\/pre>\n<p>This snippet shows one way to go about doing key controls using <a href=\"https:\/\/actionsnippet.com\/?page_id=1391\" target=\"blank\">QuickBox2D<\/a><\/p>\n<p><a data-asruffle-linked=\"true\" href=\"http:\/\/www.actionsnippet.com\/swfs\/qb2d_keys.html\" target=\"blank\">Take a look at the swf here&#8230;<br \/>\n<img decoding=\"async\" src=\"https:\/\/actionsnippet.com\/wp-content\/quickbox_key_controls.gif\" \/><\/a> <span class=\"asruffle-link\" data-swf=\"{&quot;url&quot;:&quot;https:\\\/\\\/actionsnippet.com\\\/swfs\\\/qb2d_keys.swf&quot;,&quot;title&quot;:&quot;qb2d keys&quot;,&quot;width&quot;:700,&quot;height&quot;:600,&quot;missing&quot;:false,&quot;allowScriptAccess&quot;:false,&quot;backgroundColor&quot;:&quot;#efefef&quot;}\"><button type=\"button\" hidden aria-expanded=\"false\">Play demo<span class=\"screen-reader-text\">: qb2d keys<\/span><\/button><\/span><\/p>\n<p>This works with the current version of QuickBox2D&#8230;. tomorrow I&#8217;ll be uploading the new version of QuickBox2D which supports FRIM (frame rate independent motion) and contains a few additional minor tweaks and fixes.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>import com.actionsnippet.qbox.*; import Box2D.Common.Math.* &#x5B;SWF(backgroundColor=0xEFEFEF, width=700, height=600, frameRate=60)] var sim:QuickBox2D = new QuickBox2D(this); sim.setDefault({lineColor:0xCC0000, fillColor:0xCC0000}); \/\/ create compound shape (two circles and a box for the character) var charParts:Array = &#x5B;]; \/\/ x and y position are now relative to center of compound shape charParts&#x5B;0] = sim.addBox({x:0, y:0, width:1, height:2}); charParts&#x5B;1] = sim.addCircle({x:0, y:-1, radius:0.5}); [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11,75,13,12],"tags":[3,14,15,4],"class_list":["post-316","post","type-post","status-publish","format-standard","hentry","category-box2d","category-keys","category-motion","category-quickbox2d","tag-actionscript","tag-as3","tag-box2d","tag-flash"],"_links":{"self":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/316","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=316"}],"version-history":[{"count":0,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/316\/revisions"}],"wp:attachment":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=316"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=316"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=316"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}