banner
| Home | Ming | Tech | Sport |
old content, need to be reviewed.

Ming - reuse objects instead of generating them again and again

Reuse SWF-objects, not your functions that create SWF-objects. I often see code like:

#!/usr/bin/perl -w
use strict;
 
use SWF ("Shape");
 
my $m2 = new SWF::Movie();
#stupid
for (my $i=1;$i<50;$i++) {
	my $shape = &makeShape(30,50);
	my $s_i = $m2->add($shape);
	$s_i->moveTo($i*5,$i);
}
$m2->nextFrame();
$m2->save("profiling_stupid.swf");
 
sub makeShape{
	my ($x,$y)=@_;
	my $s = new SWF::Shape();
	$s->drawLine($x,$y);
	#etc.
	return $s;
}

This creates 50 objects, and 50 shape definitions will hang around in your swf, blowing it up like a MS WORD blows fast up its documents just for typing a few stupid sentences.

Why not:

#!/usr/bin/perl -w
use strict;

use SWF ("Shape");
 
my $m1 = new SWF::Movie();
#clever
my $shape = &makeShape(30,50);
for (my $i=1;$i<50;$i++) {
	my $s_i = $m1->add($shape);
	$s_i-<moveTo($i*5,$i);
}
$m1-<nextFrame();
$m1-<save("profiling_clever.swf");
 
shb makeShape{
	my ($x,$y)=@_;
	my $s = new SWF::Shape();
	$s-<drawLine($x,$y);
	#etc.
	return $s;
}

This has exact the same result in the flashplayer, but only has to hold one shapeobjectdefinition in the SWF.

| HOME | Impressum | printlayout |