Note that there are some explanatory texts on larger screens.

plurals
  1. POSimple Flash test application runs very slowly compared to exact same Processing applet
    text
    copied!<p>So I'm gradually, painfully moving from Processing to Flash in order to hopefully develop games to a wider audience. At long last, I get a working application in Flash made that simply lets the user click to create blocks which subsequently gravitate towards the mouse. I made the exact same thing in Processing too, just to compare speeds. However, when I run the Flash version and add around 15-20 blocks, the framerate drops to 5-10 FPS. In the Processing version I can add ~60 with no noticeable slowdown. What's the deal, Flash?</p> <p>Links to each version's source:</p> <p><a href="http://ge.tt/6vrRdBe" rel="nofollow">Flash version</a></p> <p><a href="http://ge.tt/4TwQdBg" rel="nofollow">Processing version</a></p> <p>Here is the source for each in case you are a wizard and can help just by glowering at the code sternly and telling it to behave:</p> <p>Flash version:</p> <p>blocks.fla</p> <pre><code>import flash.events.Event; import flash.display.MovieClip; stage.addEventListener( Event.ENTER_FRAME, onenter ); stage.addEventListener( MouseEvent.MOUSE_DOWN, onclick ); var main = this; var lastFrame:Number; var Blocks:Array = new Array(); function onenter( e:Event ):void { var time:Number = getTimer(); for( var i = 0; i &lt; Blocks.length; i++ ) { Blocks[ i ].run(); } FrameRate.text = String( Blocks.length ) + "\n" + String( 1000 / ( time - lastFrame ) ); lastFrame = time; } function onclick( e:MouseEvent ):void { var block1 = new Block( Blocks, main, mouseX, mouseY ); } </code></pre> <p>Block.as</p> <pre><code>package { import flash.display.MovieClip; import flash.geom.Vector3D; public class Block extends MovieClip { var velocity:Vector3D = new Vector3D( 0, 0 ); var position:Vector3D = new Vector3D( x, y ); var acceleration:Vector3D = new Vector3D( 0, 0 ); public function Block( Blocks:Array, This, x:Number, y:Number ) { Blocks.push( this ); This.addChild( this ); position.x = x; position.y = y; } public function run() { x = position.x; y = position.y; //position.incrementBy( velocity ); position.x += velocity.x; position.y += velocity.y; acceleration.x = stage.mouseX - position.x; acceleration.y = stage.mouseY - position.y; acceleration.normalize(); //velocity.incrementBy( acceleration ); velocity.x += acceleration.x; velocity.y += acceleration.y; velocity.x *= 0.95; velocity.y *= 0.95; this.graphics.beginFill( 0 ); this.graphics.moveTo( -10, -10 ); this.graphics.lineTo( 10, -10 ); this.graphics.lineTo( 10, 10 ); this.graphics.lineTo( -10, 10 ); this.graphics.lineTo( -10, -10 ); this.graphics.endFill(); } } } </code></pre> <p>Processing version:</p> <p>sketch_mar02b.pde</p> <pre><code>Block[] blocks = new Block[ 0 ]; void setup() { frameRate( 60 ); size( 550, 400 ); textFont( createFont( "Verdana", 20 ) ); } void draw() { background( 255 ); for( int i = 0; i &lt; blocks.length; i++ ) { blocks[ i ].run(); } text( blocks.length + "\n" + frameRate, 0, 20 ); } void mousePressed() { new Block( mouseX, mouseY ); } </code></pre> <p>Block.pde</p> <pre><code>class Block { PVector position = new PVector( 0, 0 ); PVector velocity = new PVector( 0, 0 ); PVector acceleration = new PVector( 0, 0 ); Block( float x, float y ) { position.set( x, y, 0 ); blocks = ( Block[] ) append( blocks, this ); } void run() { position.add( velocity ); acceleration.set( mouseX - position.x, mouseY - position.y, 0 ); acceleration.normalize(); velocity.add( acceleration ); velocity.mult( 0.95 ); pushMatrix(); translate( position.x, position.y ); fill( 0 ); rect( -10, -10, 20, 20 ); popMatrix(); } } </code></pre> <p>Thanks for helping out!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload