{"id":2665,"date":"2013-03-27T15:24:36","date_gmt":"2013-03-27T22:24:36","guid":{"rendered":"http:\/\/www.nathalielawhead.com\/candybox\/?p=2665"},"modified":"2013-06-20T17:55:35","modified_gmt":"2013-06-21T00:55:35","slug":"super-simple-script-snippets-standalone-leaderboard-system-and-more","status":"publish","type":"post","link":"http:\/\/www.nathalielawhead.com\/candybox\/super-simple-script-snippets-standalone-leaderboard-system-and-more","title":{"rendered":"Super Simple Script Snippets &#8211; Standalone Leaderboard System, and More"},"content":{"rendered":"<p><a href=\"https:\/\/itunes.apple.com\/us\/app\/offender\/id618430404?ls=1&#038;mt=8\">Offender 1 is available for purchase on the app store<\/a>. Because the submission process was so successful, and it&#8217;s a platform just waiting for more <a href=\"http:\/\/tetrageddon.com\" target=\"_blank\">Tetrageddon<\/a>, if taken the time off Offender 2 and am porting <a href=\"http:\/\/haxxx.alienmelon.com\" target=\"_blank\">Haxed By Megahurtz<\/a> to iOS.<br \/>\nIt&#8217;s nearly complete, and going well. I&#8217;m excited to see it working on mobile.<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/nathalielawhead.com\/noodles\/haxxx_1.jpg\" alt=\"\" \/><\/p>\n<p>The game (<a href=\"http:\/\/haxxx.alienmelon.com\" target=\"_blank\">Haxed By Megahurtz<\/a>) was developed for the Nintendo Wii. It took off like wild fire after a <a href=\"http:\/\/jayisgames.com\/archives\/2008\/02\/haxed_by_megahurtz.php\" target=\"_blank\">Jay Is Games review<\/a>.<br \/>\nIt was a lot of fun to see it spread the way it did, and the reactions people had to it. It was very well received, to say the least.<\/p>\n<p>So, as I port it to mobile, I&#8217;ll also be posting some source code bits, and pieces. They&#8217;re fun, and may be helpful to others Googling for help, etc\u2026<br \/>\nYou can find more in the Open Source Directory. <a href=\"http:\/\/nathalielawhead.com\/sourcefiles\/Haxed_By_Megahurtz\/snippets_AS3\/\" target=\"_blank\">Click here.<\/a><\/p>\n<p>The best of which is a standalone (non online) leaderboard system.<\/p>\n<p>Sometimes you find it necessary, and this is a functioning model that I&#8217;ve successfully used. Another fun alternative is to use <a href=\"http:\/\/www.nathalielawhead.com\/candybox\/twitter-tiny-url-stumble-upon-facebook-share-delicious-google-buzz-in-flash-with-source-files\" target=\"_blank\">Twitter (or other social sharing methods)<\/a> as your leaderboards. Just let your players tweet a message from inside the game &#8211; containing their score value, plus a link to the game. Of course they can cheat and manually change the score, but it has served as a fun marketing gimmick\u2026<\/p>\n<p>At any rate, this uses shared object, so it&#8217;s stored on the player&#8217;s machine. If the shared object doesn&#8217;t already exist it is created, and populated with an initial set of scores, and names. New name, and score, (player&#8217;s) is added to it later.<\/p>\n<p>CODE (<a href=\"http:\/\/nathalielawhead.com\/sourcefiles\/Haxed_By_Megahurtz\/snippets_AS3\/score_system_standalone.fla\" target=\"_blank\">CLICK TO DOWNLOAD SOURCE<\/a> ):<\/p>\n<p>&#8212;&#8212;&#8212;&#8211;<\/p>\n<p><code><br \/>\n\/**<br \/>\nSTANDALONE NON-NETWORK SCOREBOARD<br \/>\nSCORE SYSTEM<br \/>\n**\/<\/p>\n<p>\/\/This would be your current player's name, and score<br \/>\nvar plyr_name:String = \"PLYR\";\/\/your player name<br \/>\nvar score:Number = 2650;\/\/his score<\/p>\n<p>\/\/This is your score shared object, and arrays that get populated from it...<br \/>\nvar so_haxxx:SharedObject = SharedObject.getLocal(\"\/\"); \/\/shared object for the high scores, stored on the player's machine<br \/>\nvar arr_scores:Array = new Array(); \/\/array to contain the scores (saved to, or populated from, shared object)<br \/>\nvar arr_names:Array = new Array(); \/\/array with player names associated with the scores (saved to, or populated from, shared object)<\/p>\n<p>\/\/POPULATE SCORES FOR FIRST RUN - IF ALREADY DEFINED SET VALUES<br \/>\n\/\/If this is the first time they are playing populate (save) the scores, and names, array<br \/>\nif(so_haxxx.data.scores == undefined){<br \/>\n\t\/\/trace(\"called for first run\");<br \/>\n\tarr_scores = [3300, 3000, 2700, 2600, 2500, 2000, 1800, 1600, 1400, 1000];<br \/>\n\tarr_names = [\"MIFF\", \"KAWI\", \"COFA\", \"EKAW\", \"MOJA\", \"BIKA\", \"ISUX\", \"RAVA\", \"IGOT\", \"COMU\"];<br \/>\n\t\/\/<br \/>\n\tso_haxxx.data.scores = arr_scores;<br \/>\n\tso_haxxx.data.names = arr_names;<br \/>\n\tso_haxxx.flush();<br \/>\n}else{<br \/>\n\/\/If not, then populate both arrays with current scores, and names<br \/>\n\tarr_scores = so_haxxx.data.scores;<br \/>\n\tarr_names = so_haxxx.data.names;<br \/>\n}<\/p>\n<p>\/\/SUBMIT SCORE ...<br \/>\n\/\/A very basic script that checks if the score you are saving is greater than any previous scores.<br \/>\n\/\/If it is it saves it according to it's value on the board\u2026<br \/>\nfor (var i:Number = 0; i < arr_scores .length; ++i){\n\ttrace(arr_scores[i]);\n\tif(score>=arr_scores[i]){<br \/>\n\t\ttrace(score + \" is greater than \" + arr_scores[i]);<br \/>\n\t\t\/\/push new values<br \/>\n\t\tarr_scores[i] = score;<br \/>\n\t\tarr_names[i] = plyr_name;<br \/>\n\t\t\/\/update SO's<br \/>\n\t\tso_haxxx.data.scores = arr_scores;<br \/>\n\t\tso_haxxx.data.names = arr_names;<br \/>\n\t\tso_haxxx.flush();<br \/>\n\t\t\/\/break out of loop<br \/>\n\t\ti = arr_scores.length;<br \/>\n\t}<br \/>\n} <\/code><\/p>\n<p>[swfobj src=&#8221;http:\/\/nathalielawhead.com\/sourcefiles\/Haxed_By_Megahurtz\/snippets_AS3\/score_system_standalone.swf&#8221; height=&#8221;456&#8243; width=&#8221;608&#8243;]<br \/>\n&#8212;&#8212;&#8212;&#8211;<\/p>\n<p>Aside from that, which I think will probably be the most useful to a googler looking for a solution, there are also these fun bits\u2026<\/p>\n<p><strong>Cartoon Circles <\/strong> (<a href=\"http:\/\/nathalielawhead.com\/sourcefiles\/Haxed_By_Megahurtz\/snippets_AS3\/cartoon_circles.fla\" target=\"_blank\">DOWNLOAD SOURCE <\/a> )<br \/>\nA coded background graphic.<br \/>\n[swfobj src=&#8221;http:\/\/nathalielawhead.com\/sourcefiles\/Haxed_By_Megahurtz\/snippets_AS3\/cartoon_circles.swf&#8221; height=&#8221;456&#8243; width=&#8221;608&#8243;]<\/p>\n<p>&#8212;&#8212;&#8212;&#8211;<\/p>\n<p><strong>Mouse Pan Perspective Scrolling<\/strong> (Background Follows A Sprite or Mouse)<br \/>\n(<a href=\"http:\/\/nathalielawhead.com\/sourcefiles\/Haxed_By_Megahurtz\/snippets_AS3\/mouse_pan_perspective_scroll.fla\" target=\"_blank\">DOWNLOAD SOURCE <\/a> )<br \/>\n[swfobj src=&#8221;http:\/\/nathalielawhead.com\/sourcefiles\/Haxed_By_Megahurtz\/snippets_AS3\/mouse_pan_perspective_scroll.swf&#8221; height=&#8221;456&#8243; width=&#8221;608&#8243;]<\/p>\n<p>Code:<\/p>\n<p>\/*<br \/>\nPan top, and bottom border, acording to a movieclip&#8217;s position.<br \/>\nMake it appear that, as you follow the movieclip, you are subtly exploring a scene<br \/>\n*\/<\/p>\n<p>\/\/This is an example for a game that has 3 levels<br \/>\n\/\/background changes depending on the level you are on<br \/>\n\/\/so&#8230;<br \/>\nvar level:Number = 1;<\/p>\n<p>\/\/PAN FOR TOP IMAGE<br \/>\nfunction moveImg(clip:Object, targ:Object, sizeW:Number, sizeH:Number) {<br \/>\n\t\/\/sizeW\/sizeH &#8212; the lower the numbers the larger the clip<br \/>\n\tfunction clip_onEnterFrame(event:Event) {<br \/>\n\t\t\/\/distance, friction, target and size&#8230;<br \/>\n\t\tvar this_clip = event.currentTarget;<br \/>\n\t\tthis_clip.x += Math.ceil(((-Math.ceil(targ.x\/sizeW))-this_clip.x)\/20);<br \/>\n\t\tthis_clip.y += Math.ceil(((-Math.ceil(targ.y\/sizeH))-this_clip.y)\/20);<br \/>\n\t};<br \/>\n\t\/\/<br \/>\n\tfunction removed(event:Event){<br \/>\n\t\tevent.currentTarget.removeEventListener(Event.ENTER_FRAME, clip_onEnterFrame);<br \/>\n\t\tevent.currentTarget.removeEventListener(Event.REMOVED_FROM_STAGE, removed);<br \/>\n\t}<br \/>\n\tclip.addEventListener(Event.REMOVED_FROM_STAGE,removed);<br \/>\n\tclip.addEventListener(Event.ENTER_FRAME, clip_onEnterFrame);<br \/>\n\t\/\/<br \/>\n};<\/p>\n<p>\/\/PAN FOR LOWER IMAGE<br \/>\nfunction moveLowImg(clip:Object, targ:Object, sizeW:Number, sizeH:Number, fade:Number) {<br \/>\n\tvar ystart:Number = clip.y;<br \/>\n\tclip.x = Math.ceil(stage.stageWidth\/2);<br \/>\n\t\/\/<br \/>\n\tfunction clip_onEnterFrame(event:Event) {<br \/>\n\t\tvar this_clip = event.currentTarget;<br \/>\n\t\tvar targY:Number = ystart-(targ.y\/sizeH)+150<br \/>\n\t\tvar targX:Number = targ.x &#8211; (targ.x * 2)+sizeW<br \/>\n\t\t\/\/<br \/>\n\t\tthis_clip.y += Math.ceil((targY-clip.y)\/20);<br \/>\n\t\tthis_clip.x += Math.ceil((targX-clip.x)\/20);<br \/>\n\t\t\/\/<br \/>\n\t\tthis_clip.scaleY += (((targ.y\/fade)\/100)-clip.scaleY)\/20;<br \/>\n\t\tthis_clip.scaleX += (((targ.y\/fade)\/100)-clip.scaleX)\/20;<br \/>\n\t};<br \/>\n\t\/\/<br \/>\n\tfunction removed(event:Event){<br \/>\n\t\tevent.currentTarget.removeEventListener(Event.ENTER_FRAME, clip_onEnterFrame);<br \/>\n\t\tevent.currentTarget.removeEventListener(Event.REMOVED_FROM_STAGE, removed);<br \/>\n\t}<br \/>\n\tclip.addEventListener(Event.REMOVED_FROM_STAGE,removed);<br \/>\n\tclip.addEventListener(Event.ENTER_FRAME, clip_onEnterFrame);<br \/>\n\t\/\/<br \/>\n}<\/p>\n<p>moveImg(mc_1, mc_movieClipToFollow, .8, 1);<br \/>\nmoveLowImg(mc_2, movieClipToFollow, 700, 2, 5);<\/p>\n<p>&#8212;&#8212;&#8212;&#8211;<\/p>\n<p><strong>MovieClip To Mouse (Simple Inertia)<\/strong><\/p>\n<p>(<a href=\"http:\/\/nathalielawhead.com\/sourcefiles\/Haxed_By_Megahurtz\/snippets_AS3\/movieclip_to_mouse_inertia.fla\" target=\"_blank\">DOWNLOAD SOURCE <\/a> )<br \/>\n[swfobj src=&#8221;http:\/\/nathalielawhead.com\/sourcefiles\/Haxed_By_Megahurtz\/snippets_AS3\/movieclip_to_mouse_inertia.swf&#8221; height=&#8221;300&#8243; width=&#8221;300&#8243;]<\/p>\n<p>Code:<\/p>\n<p>\/\/inertia movement on clip<br \/>\nfunction inertia(clip:MovieClip, speed:Number) {<br \/>\n\tfunction this_onEnterFrame(event:Event) {<br \/>\n\t\t\/\/or event.target.stage.mouseY<br \/>\n\t\tclip.x += Math.ceil((mouseX-clip.x)\/speed);<br \/>\n\t\tclip.y += Math.ceil((mouseY-clip.y)\/speed);<br \/>\n\t};<br \/>\n\tclip.addEventListener(Event.ENTER_FRAME, this_onEnterFrame);<br \/>\n}<\/p>\n<p>inertia(mc_bgFollow, 10);<\/p>\n<p>&#8212;&#8212;&#8212;&#8211;<\/p>\n<p><strong>Cat In Space<\/strong> (Scrolling Stars)<br \/>\n(<a href=\"http:\/\/nathalielawhead.com\/sourcefiles\/Haxed_By_Megahurtz\/snippets_AS3\/nested_addChild_catinspace.fla\" target=\"_blank\">DOWNLOAD SOURCE <\/a> )<br \/>\n[swfobj src=&#8221;http:\/\/nathalielawhead.com\/sourcefiles\/Haxed_By_Megahurtz\/snippets_AS3\/nested_addChild_catinspace.swf&#8221; height=&#8221;597&#8243; width=&#8221;674&#8243;]<\/p>\n<p>&#8212;&#8212;&#8212;&#8211;<\/p>\n<p><strong>Particle Effects For Mouse<\/strong><br \/>\n(<a href=\"http:\/\/nathalielawhead.com\/sourcefiles\/Haxed_By_Megahurtz\/snippets_AS3\/particle_effect_in_container_clip.fla\" target=\"_blank\">DOWNLOAD SOURCE<\/a> )<br \/>\n[swfobj src=&#8221;http:\/\/nathalielawhead.com\/sourcefiles\/Haxed_By_Megahurtz\/snippets_AS3\/particle_effect_in_container_clip.swf&#8221; height=&#8221;300&#8243; width=&#8221;350&#8243;]<\/p>\n<p>&#8212;&#8212;&#8212;&#8211;<\/p>\n<p><strong>Pass Property As String <\/strong>(Organic Values)<br \/>\n(<a href=\"http:\/\/nathalielawhead.com\/sourcefiles\/Haxed_By_Megahurtz\/snippets_AS3\/pass_property_as_string.fla\" target=\"_blank\">DOWNLOAD SOURCE<\/a> )<br \/>\n[swfobj src=&#8221;http:\/\/nathalielawhead.com\/sourcefiles\/Haxed_By_Megahurtz\/snippets_AS3\/pass_property_as_string.swf&#8221; height=&#8221;100&#8243; width=&#8221;100&#8243;]<\/p>\n<p>Code:<\/p>\n<p>\/\/Send a property to function as a string reference<br \/>\n\/\/Complex movement as various properties<br \/>\n\/\/<br \/>\nfunction randomMovement(clip:MovieClip, mcProp:String, maxVal:Number, minVal:Number, decay:Number, decimal:Number) {<br \/>\n\t\/\/init variables<br \/>\n\tvar ymoveval:Number = 0;<br \/>\n\tvar yPos:Number = 0;<br \/>\n\tvar yTarget:Number = 0;<br \/>\n\tvar yval:Number = 0;<br \/>\n\tvar rVar:Number = 0;<br \/>\n\tvar fVar:Number = 0;<br \/>\n\tvar yVar:Number = 0;<br \/>\n\t\/\/<br \/>\n\tfunction clip_onEnterFrame(event:Event) {<br \/>\n\t\tif (ymoveval&lt;0.1 &#038;&#038; ymoveval>-0.1) {<br \/>\n\t\t\trVar = ((Math.ceil(Math.random()*4)+4)\/100);<br \/>\n\t\t\tfVar = ((Math.ceil(Math.random()*4)+4)\/10);<br \/>\n\t\t\tyVar = (Math.random()*(maxVal-minVal))+minVal;<br \/>\n\t\t}<br \/>\n\t\t\/\/<br \/>\n\t\tyPos = (yPos*fVar)+((yVar-yTarget)*rVar);<br \/>\n\t\tyTarget = yTarget+yPos;<br \/>\n\t\tymoveval = (yTarget-yval)*decimal+(ymoveval*decay);<br \/>\n\t\tyval = yval+ymoveval;<br \/>\n\t\tclip[mcProp] = yval;<br \/>\n\t};<br \/>\n\tclip.addEventListener(Event.ENTER_FRAME, clip_onEnterFrame);<br \/>\n}<\/p>\n<p>randomMovement(mc_test, &#8220;rotation&#8221;, 360, -360, 0.1, 0.01);<\/p>\n<p>&#8212;&#8212;&#8212;&#8211;<\/p>\n<p><strong>Random Colors<\/strong><br \/>\n(<a href=\"http:\/\/nathalielawhead.com\/sourcefiles\/Haxed_By_Megahurtz\/snippets_AS3\/random_colors.fla\" target=\"_blank\">DOWNLOAD SOURCE<\/a>)<br \/>\n[swfobj src=&#8221;http:\/\/nathalielawhead.com\/sourcefiles\/Haxed_By_Megahurtz\/snippets_AS3\/random_colors.swf&#8221; height=&#8221;224&#8243; width=&#8221;478&#8243;]<\/p>\n<p>Code:<\/p>\n<p>\/\/Apporach #1<br \/>\nfunction changeColFunc(myMc:MovieClip) {<br \/>\n\tvar randCol:ColorTransform = myMc.transform.colorTransform;<br \/>\n\trandCol.color = (Math.random()*256 < < 16 | Math.random()*256 << 8 | Math.random()*256);\n\tmyMc.transform.colorTransform = randCol; \n};\n\n\/\/Apporach #2\nfunction changeColFunc2(myMc:MovieClip) {\n\tvar randCol:ColorTransform = myMc.transform.colorTransform;\n\trandCol.color = (Math.ceil(Math.random()*999999));\n\tmyMc.transform.colorTransform = randCol; \n};\n\n-----------\n\nIt's always fun to see all the little bits and pieces...\n\n<img decoding=\"async\" src=\"http:\/\/nathalielawhead.com\/noodles\/haxxx_2.jpg\" alt=\"\" \/><\/p>\n<p><img decoding=\"async\" src=\"http:\/\/nathalielawhead.com\/noodles\/haxxx_3.jpg\" alt=\"\" \/><\/p>\n<p><img decoding=\"async\" src=\"http:\/\/nathalielawhead.com\/noodles\/haxxx_4.jpg\" alt=\"\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Offender 1 is available for purchase on the app store. Because the submission process was so successful, and it&#8217;s a platform just waiting for more Tetrageddon, if taken the time off Offender 2 and am porting Haxed By Megahurtz to iOS. It&#8217;s nearly complete, and going well. I&#8217;m excited to see it working on mobile. The game (Haxed By Megahurtz) was developed for the Nintendo Wii. It took off like wild fire after a Jay Is Games review. It was a lot of fun to see it spread the way it did, and the reactions people had to it. It&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":4522,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"","fifu_image_alt":"","footnotes":"","_links_to":"","_links_to_target":""},"categories":[30,12],"tags":[],"class_list":["post-2665","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-games","category-resources"],"_links":{"self":[{"href":"http:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/posts\/2665","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/comments?post=2665"}],"version-history":[{"count":32,"href":"http:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/posts\/2665\/revisions"}],"predecessor-version":[{"id":2691,"href":"http:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/posts\/2665\/revisions\/2691"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/media\/4522"}],"wp:attachment":[{"href":"http:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/media?parent=2665"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/categories?post=2665"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/tags?post=2665"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}