Skip to content
Browse files

Adding an interactive coins sample. Original concept and source contr…

…ibuted by Terry Paton. http://pixelpaton.com/.

Signed-off-by: CoreyDotCom <corey.lucier@gmail.com>
  • Loading branch information...
1 parent e2f2224 commit 9661492ee1619e3080b3bf2a79892a1f4bf80ef5 CoreyDotCom committed Apr 6, 2011
View
58 samples/InteractiveRenderTest/.actionScriptProperties
@@ -0,0 +1,58 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<actionScriptProperties analytics="false" mainApplicationPath="LotsOfCoins.as" projectUUID="88f91c72-768c-4dd0-9d63-f9695571cf71" version="10">
+ <compiler additionalCompilerArguments="-locale en_US -swf-version=13" autoRSLOrdering="true" copyDependentFiles="false" fteInMXComponents="false" generateAccessible="true" htmlExpressInstall="true" htmlGenerate="false" htmlHistoryManagement="true" htmlPlayerVersionCheck="true" includeNetmonSwc="false" outputFolderPath="bin-debug" removeUnusedRSL="true" sourceFolderPath="src" strict="true" targetPlayerVersion="10.2.0" useApolloConfig="false" useDebugRSLSwfs="true" verifyDigests="true" warn="true">
+ <compilerSourcePath/>
+ <libraryPath defaultLinkType="0">
+ <libraryPathEntry kind="4" path="">
+ <modifiedEntries>
+ <libraryPathEntry applicationDomain="default" forceLoad="false" index="2" kind="3" linkType="4" path="${PROJECT_FRAMEWORKS}/libs/advancedgrids.swc" useDefaultLinkType="true">
+ <crossDomainRsls>
+ <crossDomainRslEntry autoExtract="true" policyFileUrl="" rslUrl="advancedgrids_4.5.0.0.swf"/>
+ </crossDomainRsls>
+ </libraryPathEntry>
+ <libraryPathEntry applicationDomain="default" forceLoad="false" index="1" kind="3" linkType="4" path="${PROJECT_FRAMEWORKS}/libs/osmf.swc" useDefaultLinkType="true">
+ <crossDomainRsls>
+ <crossDomainRslEntry autoExtract="true" policyFileUrl="" rslUrl="osmf_1.0.0.16316.swf"/>
+ </crossDomainRsls>
+ </libraryPathEntry>
+ <libraryPathEntry applicationDomain="default" forceLoad="false" index="0" kind="3" linkType="4" path="${PROJECT_FRAMEWORKS}/libs/textLayout.swc" useDefaultLinkType="true">
+ <crossDomainRsls>
+ <crossDomainRslEntry autoExtract="true" policyFileUrl="" rslUrl="textLayout_2.0.0.232.swf"/>
+ </crossDomainRsls>
+ </libraryPathEntry>
+ </modifiedEntries>
+ <excludedEntries>
+ <libraryPathEntry kind="3" linkType="1" path="${PROJECT_FRAMEWORKS}/libs/automation_charts.swc" useDefaultLinkType="false"/>
+ <libraryPathEntry kind="3" linkType="2" path="${PROJECT_FRAMEWORKS}/libs/player/10.2/playerglobal.swc" useDefaultLinkType="false"/>
+ <libraryPathEntry kind="1" linkType="1" path="${PROJECT_FRAMEWORKS}/locale/{locale}"/>
+ <libraryPathEntry kind="3" linkType="1" path="${PROJECT_FRAMEWORKS}/libs/qtp.swc" useDefaultLinkType="false"/>
+ <libraryPathEntry kind="3" linkType="1" path="${PROJECT_FRAMEWORKS}/libs/charts.swc" useDefaultLinkType="false"/>
+ <libraryPathEntry kind="3" linkType="1" path="${PROJECT_FRAMEWORKS}/libs/automation_air.swc" useDefaultLinkType="false"/>
+ <libraryPathEntry kind="3" linkType="1" path="${PROJECT_FRAMEWORKS}/libs/framework.swc" useDefaultLinkType="false"/>
+ <libraryPathEntry kind="3" linkType="1" path="${PROJECT_FRAMEWORKS}/libs/mx/mx.swc" useDefaultLinkType="false"/>
+ <libraryPathEntry kind="3" linkType="1" path="${PROJECT_FRAMEWORKS}/libs/netmon.swc" useDefaultLinkType="false"/>
+ <libraryPathEntry kind="3" linkType="1" path="${PROJECT_FRAMEWORKS}/libs/spark.swc" useDefaultLinkType="false"/>
+ <libraryPathEntry kind="3" linkType="1" path="${PROJECT_FRAMEWORKS}/libs/sparkskins.swc" useDefaultLinkType="false"/>
+ <libraryPathEntry kind="3" linkType="1" path="${PROJECT_FRAMEWORKS}/libs/rpc.swc" useDefaultLinkType="false"/>
+ <libraryPathEntry kind="3" linkType="1" path="${PROJECT_FRAMEWORKS}/libs/datavisualization.swc" useDefaultLinkType="false"/>
+ <libraryPathEntry kind="3" linkType="1" path="${PROJECT_FRAMEWORKS}/libs/qtp_air.swc" useDefaultLinkType="false"/>
+ <libraryPathEntry kind="3" linkType="1" path="${PROJECT_FRAMEWORKS}/libs/videoPlayer.swc" useDefaultLinkType="false"/>
+ <libraryPathEntry kind="3" linkType="1" path="${PROJECT_FRAMEWORKS}/libs/spark_dmv.swc" useDefaultLinkType="false"/>
+ <libraryPathEntry kind="3" linkType="1" path="${PROJECT_FRAMEWORKS}/libs/automation.swc" useDefaultLinkType="false"/>
+ <libraryPathEntry kind="3" linkType="1" path="${PROJECT_FRAMEWORKS}/libs/automation_dmv.swc" useDefaultLinkType="false"/>
+ <libraryPathEntry kind="3" linkType="1" path="${PROJECT_FRAMEWORKS}/libs/automation_flashflexkit.swc" useDefaultLinkType="false"/>
+ <libraryPathEntry kind="3" linkType="1" path="${PROJECT_FRAMEWORKS}/libs/automation_agent.swc" useDefaultLinkType="false"/>
+ </excludedEntries>
+ </libraryPathEntry>
+ <libraryPathEntry kind="3" linkType="1" path="/M2D/bin-debug/M2D.swc" useDefaultLinkType="false"/>
+ <libraryPathEntry kind="3" linkType="1" path="${MOLEHILL_PLAYERGLOBAL}" useDefaultLinkType="false"/>
+ </libraryPath>
+ <sourceAttachmentPath/>
+ </compiler>
+ <applications>
+ <application path="LotsOfCoins.as"/>
+ </applications>
+ <modules/>
+ <buildCSSFiles/>
+ <flashCatalyst validateFlashCatalystCompatibility="false"/>
+</actionScriptProperties>
View
17 samples/InteractiveRenderTest/.project
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+ <name>InteractiveRenderTest</name>
+ <comment></comment>
+ <projects>
+ </projects>
+ <buildSpec>
+ <buildCommand>
+ <name>com.adobe.flexbuilder.project.flexbuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ </buildSpec>
+ <natures>
+ <nature>com.adobe.flexbuilder.project.actionscriptnature</nature>
+ </natures>
+</projectDescription>
View
3 samples/InteractiveRenderTest/.settings/org.eclipse.core.resources.prefs
@@ -0,0 +1,3 @@
+#Tue Nov 30 16:33:57 PST 2010
+eclipse.preferences.version=1
+encoding/<project>=utf-8
View
100 samples/InteractiveRenderTest/src/CoinClip.as
@@ -0,0 +1,100 @@
+package {
+ import M2D.animation.CellAnimation;
+ import M2D.sprites.Actor;
+
+ public class CoinClip {
+ public function CoinClip(_actor:Actor) {
+ actorRef=_actor
+ super();
+ }
+
+ public function set x(_x:Number):void {
+ actorRef.x=_x
+ }
+
+ public function set y(_y:Number):void {
+ actorRef.y=_y
+ }
+
+ public function get x():Number {
+ return actorRef.x
+ }
+
+ public function get y():Number {
+ return actorRef.y
+ }
+
+ public function setAlpha(_alpha:Number):void {
+ actorRef.alpha=_alpha
+ }
+
+ public function rotate(_rotate:Number):void {
+ actorRef.rotation+=_rotate
+ newAngle=actorRef.rotation
+ }
+
+ public function move(_x:Number, _y:Number):void {
+ actorRef.x+=_x
+ actorRef.y+=_y
+ }
+ private var radians:Number
+ private var newAngle:Number
+ private var angleDiff:Number
+
+ public function manage():void {
+ // apply velocity
+ if (Math.abs(velocityx) > 0.1) {
+ velocityx*=.9
+ actorRef.x+=velocityx
+ if (Math.abs(velocityx) < .1) {
+ velocityx=0
+ }
+ }
+ if (Math.abs(velocityy) > 0.1) {
+ velocityy*=.9
+ actorRef.y+=velocityy
+ if (Math.abs(velocityy) < .1) {
+ velocityy=0
+ }
+ }
+ // adjust the rotation of the actor
+ if (Math.abs(velocityx) > 0 || Math.abs(velocityy) > 0) {
+ radians=Math.atan2(velocityy, velocityx)
+ newAngle=radians * 180 / Math.PI
+ } else {
+ newAngle=0
+ }
+ angleDiff=newAngle - actorRef.rotation
+ if (Math.abs(angleDiff) > 0.1) {
+ actorRef.rotation+=angleDiff * .1
+ if (actorRef.rotation > 360) {
+ actorRef.rotation-=360
+ }
+ if (actorRef.rotation < 0) {
+ actorRef.rotation+=360
+ }
+ actorRef.cell++
+ if (actorRef.cell > 23) {
+ actorRef.cell=0
+ }
+ }
+
+ if (actorRef.x > LotsOfCoins.stageWidth + LotsOfCoins.edgeBuffer) {
+ actorRef.x=-LotsOfCoins.edgeBuffer
+ }
+ if (actorRef.x < -LotsOfCoins.edgeBuffer) {
+ actorRef.x+=LotsOfCoins.stageWidth + LotsOfCoins.edgeBuffer
+ }
+ if (actorRef.y > LotsOfCoins.stageHeight + LotsOfCoins.edgeBuffer) {
+ actorRef.y=-LotsOfCoins.edgeBuffer
+ }
+ if (actorRef.y < -LotsOfCoins.edgeBuffer) {
+ actorRef.y=LotsOfCoins.stageHeight + LotsOfCoins.edgeBuffer
+ }
+ }
+ public var velocityx:Number=0;
+ public var velocityy:Number=0;
+ public var actorRef:Actor
+ public var animationRef:CellAnimation
+ }
+}
View
113 samples/InteractiveRenderTest/src/LotsOfCoins.as
@@ -0,0 +1,113 @@
+/*
+ * LotsOfCoins - M2D Sample
+ * Author: Terry Paton
+ */
+package {
+ import M2D.animation.CellAnimation;
+ import M2D.sprites.Actor;
+ import M2D.sprites.Asset;
+ import M2D.worlds.BatchTexture;
+ import flash.display.Sprite;
+ import flash.events.Event;
+ import flash.text.TextField;
+ import flash.text.TextFieldAutoSize;
+
+ [SWF(width="800", height="480", frameRate="40", backgroundColor="0xB0E0E5")]
+ public class LotsOfCoins extends Sprite
+ {
+ private static const totalObjects:int=4000;
+ public var images:Vector.<CoinClip>;
+ public var tf2:TextField;
+ protected var scene:RenderScene;
+ private var instanceCount:int=0;
+ private var fallingSpeed:Number=5
+ private var frames:int=0;
+ private var coinAsset:Asset;
+ public static var edgeBuffer:Number=10;
+ public static var stageWidth:Number=800;
+ public static var stageHeight:Number=480;
+ private var _mouseX:Number;
+ private var _mouseY:Number;
+ private var dx:Number;
+ private var dy:Number;
+ private var dist:Number;
+
+ [Embed(source="assets/coin.png")]
+ public var coinPng:Class;
+
+ public function LotsOfCoins()
+ {
+ super();
+ scene=new RenderScene(800, 480);
+ addChild(scene);
+ createStats();
+
+ images=new Vector.<CoinClip>();
+ var n:int=totalObjects;
+ while (n--) {
+ addObject()
+ }
+
+ addEventListener(Event.ENTER_FRAME, loop);
+ }
+
+ private function addObject():void
+ {
+ if (instanceCount < totalObjects)
+ {
+ var image:CoinClip = image=scene.addAnimatedSprite(coinPng);
+
+ image.x=Math.random() * stageWidth;
+ image.y=Math.random() * stageHeight;
+ image.rotate(Math.random() * 360);
+
+ images.push(image);
+ instanceCount++;
+ tf2.text = String(instanceCount) + " Objects";
+ var animation:CellAnimation=new CellAnimation(scene._clock, 0, 24);
+ animation.actor=image.actorRef;
+ image.actorRef.cell=int(Math.random() * 20)
+ image.animationRef=animation
+ }
+ }
+
+ protected function loop(event:Event):void
+ {
+ _mouseX = mouseX;
+ _mouseY = mouseY;
+
+ var image:CoinClip
+ var l:uint=images.length;
+ for (var i:Number=0; i < l; i++)
+ {
+ image=images[i];
+
+ // find how close item is to the mouse
+ dx=image.x - _mouseX;
+ dy=image.y - _mouseY;
+ dist=Math.floor(Math.sqrt(dx * dx + dy * dy));
+
+ if (dist < 100)
+ {
+ // push the item away
+ image.velocityx=dx * .1
+ image.velocityy=dy * .1
+ }
+ image.manage()
+ }
+ }
+
+ private function createStats():void
+ {
+ tf2=new TextField();
+ tf2.autoSize=TextFieldAutoSize.LEFT;
+ tf2.width=300;
+ tf2.x=80
+ tf2.background=true;
+ tf2.border=true;
+ addChild(tf2);
+ tf2.text=String(instanceCount) + " Objects";
+ addChild(new Stats());
+ }
+ }
+}
View
108 samples/InteractiveRenderTest/src/RenderScene.as
@@ -0,0 +1,108 @@
+package {
+ import M2D.sprites.Actor;
+ import M2D.sprites.Asset;
+ import M2D.time.Clock;
+ import M2D.time.IClockListener;
+ import M2D.worlds.BatchTexture;
+ import M2D.worlds.World;
+
+ import flash.display.Bitmap;
+ import flash.display.BitmapData;
+ import flash.display.DisplayObject;
+ import flash.display.Sprite;
+ import flash.events.Event;
+ import flash.geom.Rectangle;
+ import flash.utils.Dictionary;
+ import flash.utils.getTimer;
+
+ public class RenderScene extends Sprite implements IClockListener
+ {
+ public var world:World;
+ protected var _lastUpdate:Number=getTimer();
+ protected var _fps:Number;
+ protected var _symbols:Vector.<Actor>
+ protected var sceneWidth:Number;
+ protected var sceneHeight:Number;
+ private var depthTotal:Number=0;
+ public var onTick:Function=null;
+
+ // asset cache
+ private var assetsList:Dictionary=new Dictionary();
+
+ public function RenderScene(sceneWidth:Number, sceneHeight:Number)
+ {
+ this.sceneWidth=sceneWidth;
+ this.sceneHeight=sceneHeight;
+ addEventListener(Event.ADDED_TO_STAGE, init);
+ }
+
+ public function init(e:Event=null):void
+ {
+ _clock=new Clock(60);
+ _clock.addListener(this);
+ world=new World();
+ world.backgroundColor=0xB0E0E5;
+ clock.addListener(world);
+ world.initContext(stage, this, 0, new Rectangle(0, 0, sceneWidth, sceneHeight));
+ _symbols=new Vector.<Actor>();
+ _clock.start();
+ }
+
+ public function addAnimatedSprite(img:Class):CoinClip
+ {
+ var asset:Asset=getAssetByClass(img);
+ if (asset == null)
+ {
+ asset = world.assetMgr.createAssetFromDisplayObject(new img(), 3, 10);
+ assetsList[img]=asset;
+ }
+ var symbol:Actor=asset.createActor();
+ symbol.depth = 1;
+ _symbols.push(symbol);
+ return new CoinClip(symbol)
+ }
+
+ protected function getAssetByClass(cls:Class):Asset
+ {
+ return assetsList[cls];
+ }
+
+ protected function getAssetByDisplayObject(d:DisplayObject):Asset
+ {
+ return assetsList[d];
+ }
+
+ public function tick():void
+ {
+ if (onTick != null) {
+ onTick.call(this);
+ }
+ // FPS
+ var deltaT:Number=getTimer() - _lastUpdate;
+ _lastUpdate=getTimer();
+ _fps=Math.round(100 / deltaT) * 10;
+ }
+
+ // GETTERS/SETTERS
+ public function get fps():Number
+ {
+ return _fps;
+ }
+ public var _clock:Clock;
+
+ public function get clock():Clock
+ {
+ return _clock;
+ }
+
+ public function set clock(value:Clock):void
+ {
+ _clock=value;
+ }
+
+ public function get symbols():Vector.<Actor>
+ {
+ return _symbols;
+ }
+ }
+}
View
159 samples/InteractiveRenderTest/src/Stats.as
@@ -0,0 +1,159 @@
+/**
+ * Hi-ReS! Stats
+ *
+ * Released under MIT license:
+ * http://www.opensource.org/licenses/mit-license.php
+ *
+ * How to use:
+ *
+ * addChild( new Stats() );
+ *
+ * or
+ *
+ * addChild( new Stats( { bg: 0xffffff } );
+ *
+ * version log:
+ *
+ * 09.03.28 2.1 Mr.doob + Theme support.
+ * 09.02.21 2.0 Mr.doob + Removed Player version, until I know if it's really needed.
+ * + Added MAX value (shows Max memory used, useful to spot memory leaks)
+ * + Reworked text system / no memory leak (original reason unknown)
+ * + Simplified
+ * 09.02.07 1.5 Mr.doob + onRemovedFromStage() (thx huihuicn.xu)
+ * 08.12.14 1.4 Mr.doob + Code optimisations and version info on MOUSE_OVER
+ * 08.07.12 1.3 Mr.doob + Some speed and code optimisations
+ * 08.02.15 1.2 Mr.doob + Class renamed to Stats (previously FPS)
+ * 08.01.05 1.2 Mr.doob + Click changes the fps of flash (half up increases, half down decreases)
+ * 08.01.04 1.1 Mr.doob + Shameless ripoff of Alternativa's FPS look :P
+ * Theo + Log shape for MEM
+ * + More room for MS
+ * 07.12.13 1.0 Mr.doob + First version
+ **/
+package {
+ import flash.display.Bitmap;
+ import flash.display.BitmapData;
+ import flash.display.Sprite;
+ import flash.events.Event;
+ import flash.events.MouseEvent;
+ import flash.geom.Rectangle;
+ import flash.system.System;
+ import flash.text.StyleSheet;
+ import flash.text.TextField;
+ import flash.utils.getTimer;
+ /**
+ * <b>Hi-ReS! Stats</b> FPS, MS and MEM, all in one.
+ */
+ public class Stats extends Sprite {
+ private var _xml:XML;
+ private var _text:TextField;
+ private var _style:StyleSheet;
+ private var _timer:uint;
+ private var _fps:uint;
+ private var _ms:uint;
+ private var _ms_prev:uint;
+ private var _mem:Number;
+ private var _mem_max:Number;
+ private var _graph:BitmapData;
+ private var _rectangle:Rectangle;
+ private var _fps_graph:uint;
+ private var _mem_graph:uint;
+ private var _mem_max_graph:uint;
+ private var _theme:Object = {bg: 0x000033, fps: 0xffff00, ms: 0x00ff00, mem: 0x00ffff, memmax: 0xff0070};
+
+ /**
+ * <b>Hi-ReS! Stats</b> FPS, MS and MEM, all in one.
+ *
+ * @param theme Example: { bg: 0x202020, fps: 0xC0C0C0, ms: 0x505050, mem: 0x707070, memmax: 0xA0A0A0 }
+ */
+ public function Stats(theme:Object = null):void {
+ if (theme) {
+ if (theme.bg != null)
+ _theme.bg = theme.bg;
+ if (theme.fps != null)
+ _theme.fps = theme.fps;
+ if (theme.ms != null)
+ _theme.ms = theme.ms;
+ if (theme.mem != null)
+ _theme.mem = theme.mem;
+ if (theme.memmax != null)
+ _theme.memmax = theme.memmax;
+ }
+ addEventListener(Event.ADDED_TO_STAGE, init, false, 0, true);
+ addEventListener(Event.REMOVED_FROM_STAGE, kill, false, 0, true);
+ }
+
+ private function kill(e:Event):void {
+ removeEventListener(Event.ADDED_TO_STAGE, init);
+ removeEventListener(Event.REMOVED_FROM_STAGE, kill);
+ removeEventListener(MouseEvent.CLICK, onClick);
+ removeEventListener(Event.ENTER_FRAME, update);
+ }
+
+ private function init(e:Event):void {
+ removeEventListener(Event.ADDED_TO_STAGE, init);
+ graphics.beginFill(_theme.bg);
+ graphics.drawRect(0, 0, 70, 50);
+ graphics.endFill();
+ _mem_max = 0;
+ _xml = <xml><fps>FPS:</fps><ms>MS:</ms><mem>MEM:</mem><memMax>MAX:</memMax></xml>;
+ _style = new StyleSheet();
+ _style.setStyle("xml", {fontSize: '9px', fontFamily: '_sans', leading: '-2px'});
+
+ _style.setStyle("fps", {color: hex2css(_theme.fps)});
+ _style.setStyle("ms", {color: hex2css(_theme.ms)});
+ _style.setStyle("mem", {color: hex2css(_theme.mem)});
+ _style.setStyle("memMax", {color: hex2css(_theme.memmax)});
+ _text = new TextField();
+ _text.width = 70;
+ _text.height = 50;
+ _text.styleSheet = _style;
+ _text.condenseWhite = true;
+ _text.selectable = false;
+ _text.mouseEnabled = false;
+ addChild(_text);
+ var bitmap:Bitmap = new Bitmap(_graph = new BitmapData(70, 50, false, _theme.bg));
+ bitmap.y = 50;
+ addChild(bitmap);
+ _rectangle = new Rectangle(0, 0, 1, _graph.height);
+ addEventListener(MouseEvent.CLICK, onClick);
+ addEventListener(Event.ENTER_FRAME, update);
+ }
+
+ private function update(e:Event):void {
+ _timer = getTimer();
+ if (_timer - 1000 > _ms_prev) {
+ _ms_prev = _timer;
+ _mem = Number((System.totalMemory * 0.000000954).toFixed(3));
+ _mem_max = _mem_max > _mem ? _mem_max : _mem;
+ _fps_graph = Math.min(50, (_fps / stage.frameRate) * 50);
+ _mem_graph = Math.min(50, Math.sqrt(Math.sqrt(_mem * 5000))) - 2;
+ _mem_max_graph = Math.min(50, Math.sqrt(Math.sqrt(_mem_max * 5000))) - 2;
+ _graph.scroll(1, 0);
+ _graph.fillRect(_rectangle, _theme.bg);
+ _graph.setPixel(0, _graph.height - _fps_graph, _theme.fps);
+ _graph.setPixel(0, _graph.height - ((_timer - _ms) >> 1), _theme.ms);
+ _graph.setPixel(0, _graph.height - _mem_graph, _theme.mem);
+ _graph.setPixel(0, _graph.height - _mem_max_graph, _theme.memmax);
+ _xml.fps = "FPS: " + _fps + " / " + stage.frameRate;
+ _xml.mem = "MEM: " + _mem;
+ _xml.memMax = "MAX: " + _mem_max;
+ _fps = 0;
+ }
+ _fps++;
+ _xml.ms = "MS: " + (_timer - _ms);
+ _ms = _timer;
+ _text.htmlText = _xml;
+ }
+
+ private function onClick(e:MouseEvent):void {
+ mouseY / height > .5 ? stage.frameRate-- : stage.frameRate++;
+ _xml.fps = "FPS: " + _fps + " / " + stage.frameRate;
+ _text.htmlText = _xml;
+ }
+
+ // .. Utils
+ private function hex2css(color:int):String {
+ return "#" + color.toString(16);
+ }
+ }
+}
View
BIN samples/InteractiveRenderTest/src/assets/coin.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 9661492

Please sign in to comment.
Something went wrong with that request. Please try again.