所以我逐渐痛苦地从 Processing 转向 Flash,希望能够为更广泛的受众开发游戏。终于,我得到了一个在 Flash 中制作的工作应用程序,它只允许用户单击以创建随后被鼠标吸引的块。我在处理中也做了同样的事情,只是为了比较速度。但是,当我运行 Flash 版本并添加大约 15-20 个块时,帧率下降到 5-10 FPS。在处理版本中,我可以添加 ~60 而不会明显减速。怎么了,闪电侠?
每个版本的源链接:
如果您是一个向导并且可以通过严厉地盯着代码并告诉它行为来提供帮助,那么这里是每个来源的来源:
闪版:
块.fla
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 < 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 );
}
块.as
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();
}
}
}
处理版本:
sketch_mar02b.pde
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 < blocks.length; i++ )
{
blocks[ i ].run();
}
text( blocks.length + "\n" + frameRate, 0, 20 );
}
void mousePressed()
{
new Block( mouseX, mouseY );
}
块.pde
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();
}
}
感谢您的帮助!