Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 08-10-2007, 07:56 PM   #1
DiscGolfer5000
Registered User
 
Join Date: Aug 2007
Posts: 2
Default too many rectangles make the player slow

Hello, World! I am new to AS3 and OOP and am using the command line compiler with Notepad and this is my first forum post.

This code uses drawRect() over and over, creating 900 rectangles of random colors in a grid, 3 times per second. Within 10 seconds I notice a big slow down and a few seconds later, no more color changes or screen refresh ever happens. The rectangle count continues to increase though.

I don't really have a goal with this code, other than to understand what's going on. Why can't it just continue drawing new rectangles forever at the same initial speed? I feel like there is something fundamental I'm not understanding yet. I have attached the swf file also. Thank you for any explanations.

ActionScript Code:
package {     import flash.display.*;     import flash.text.*;     import flash.events.*;     import flash.utils.*;     public class Sample1 extends Sprite {         private static const GRID_SIZE_X:uint = 30;         private static const GRID_SIZE_Y:uint = 30;         private static const BLOCK_SIZE:uint = 10;         private var block:Shape = new Shape();         private var xLoc:uint, yLoc:uint;         private var drawRectCount:uint = 0;         private var textDebug:TextField = new TextField();         private var timer:Timer = new Timer(333, 0);         public function Sample1() {             displayGrid();             addChild(block);             displayDebug();             addChild(textDebug);             timer.start();             timer.addEventListener(TimerEvent.TIMER, timerListener);         }         private function displayGrid():void {             // cycle through the grid one blcok at a time             for (yLoc = 0 ; yLoc < GRID_SIZE_Y ; yLoc++ ) {                 for (xLoc = 0 ; xLoc < GRID_SIZE_X ; xLoc++ ) {                     displayBlock();                 }             }         }         private function displayBlock():void {             block.cacheAsBitmap = true;             block.graphics.lineStyle(1, 0x000000);             block.graphics.beginFill(Math.floor(Math.random() * 0xFFFFFF))// use a random color             block.graphics.drawRect(xLoc * BLOCK_SIZE, yLoc * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE);             drawRectCount++;    // I've drawn another rectangle!         }         private function displayDebug():void {             textDebug.background = true;             textDebug.border = true;             textDebug.width = 200;             textDebug.height = 50;             textDebug.x = BLOCK_SIZE * GRID_SIZE_X;             textDebug.y = BLOCK_SIZE;         }         private function timerListener(e:TimerEvent):void {             displayGrid();             textDebug.text = "Rectangles drawn: " + drawRectCount;         }     } }
Attached Files
File Type: zip Sample1.zip (1.3 KB, 102 views)
DiscGolfer5000 is offline   Reply With Quote
Old 08-10-2007, 08:49 PM   #2
senocular
six eyes
 
senocular's Avatar
 
Join Date: Jan 2003
Location: San Francisco, CA (USA)
Posts: 7,875
Send a message via ICQ to senocular Send a message via AIM to senocular Send a message via MSN to senocular Send a message via Yahoo to senocular
Default

its just a lot of vector paths for Flash to keep up with. If you need to constantly be drawing squares like that, the best thing to do would be to draw it in a BitmapData instance being shown on the screen instead. Then you are just reusing the same collection of pixels over and over again and not adding additional vector paths to be rendered.
__________________
(6)
senocular is offline   Reply With Quote
Old 08-13-2007, 01:42 PM   #3
DiscGolfer5000
Registered User
 
Join Date: Aug 2007
Posts: 2
Default

So in Sample1, why does it start fast, then slow and quit? Is each pass of drawing 900 rectangles overlaying the previous 900 that were there before, and they accumulate?

I tried your suggestion and it seems to run at the same rate as the initial rate without slowing! Thanks! I'll attach the swf file too.

ActionScript Code:
package {     import flash.display.*;     import flash.geom.*;     import flash.text.*;     import flash.events.*;     import flash.utils.*;     public class Sample2 extends Sprite {         private static const GRID_SIZE_X:uint = 30;         private static const GRID_SIZE_Y:uint = 30;         private static const BLOCK_SIZE:uint = 10;         private var xLoc:uint, yLoc:uint;         private var drawRectCount:uint = 0;         private var textDebug:TextField = new TextField();         private var timer:Timer = new Timer(333, 0);         private var imgData:BitmapData = new BitmapData(GRID_SIZE_X * BLOCK_SIZE, GRID_SIZE_Y * BLOCK_SIZE, false, 0xFF000000);         private var bmp:Bitmap = new Bitmap(imgData);         public function Sample2() {             displayGrid();             addChild(bmp);             displayDebug();             addChild(textDebug);             timer.start();             timer.addEventListener(TimerEvent.TIMER, timerListener);         }         private function displayGrid():void {             // cycle through the grid one blcok at a time             for (yLoc = 0 ; yLoc < GRID_SIZE_Y ; yLoc++ ) {                 for (xLoc = 0 ; xLoc < GRID_SIZE_X ; xLoc++ ) {                     displayBlock();                 }             }         }         private function displayBlock():void {             imgData.fillRect(new Rectangle(xLoc * BLOCK_SIZE, yLoc * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE), 0xFF000000 + Math.floor(Math.random() * 0xFFFFFF))// use a random color             drawRectCount++;    // I've drawn another rectangle!         }         private function displayDebug():void {             textDebug.background = true;             textDebug.border = true;             textDebug.width = 200;             textDebug.height = 50;             textDebug.x = BLOCK_SIZE * GRID_SIZE_X;             textDebug.y = BLOCK_SIZE;         }         private function timerListener(e:TimerEvent):void {             displayGrid();             textDebug.text = "Rectangles drawn: " + drawRectCount;         }     } }
Attached Files
File Type: zip Sample2.zip (1.3 KB, 96 views)
DiscGolfer5000 is offline   Reply With Quote
Old 08-13-2007, 02:12 PM   #4
senocular
six eyes
 
senocular's Avatar
 
Join Date: Jan 2003
Location: San Francisco, CA (USA)
Posts: 7,875
Send a message via ICQ to senocular Send a message via AIM to senocular Send a message via MSN to senocular Send a message via Yahoo to senocular
Default

yes, they accumulate
__________________
(6)
senocular is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with animations in FlashLite Player (i-mode Simulator) mapvnfx FlashLite / Portable Devices Development 0 07-24-2006 09:46 AM
How can I make a mp3 player automatically stop itself, automatically plays Bready ActionScript 2.0 0 01-11-2005 07:07 PM
How Can I Make Platforms sNaKe Simple Stuff (Newbies) 3 12-22-2004 09:48 PM
detecting the flash player version Mathieu_BSL ActionScript 1.0 (and below) 0 10-22-2002 07:12 PM
How to make Flash player run faster? vantran ActionScript 1.0 (and below) 4 01-06-2001 01:57 PM


All times are GMT. The time now is 07:49 PM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.