Papervision3D cubes x240

April 30th, 2008 by Adrian Parr

I’ve just been having a play with Papervision3D 2.0 (Great White) and here are the results of my first test. I create 240 cubes inside a loop, positioning them in rows across the stage. Each cube uses six movieclips from the library as their materials. Each cube then rotates depending on the cursor position on the stage.

Here is the code for the document class.

package
{
    import flash.display.MovieClip;
    import flash.events.Event;

    import flash.display.StageQuality;
   
    import org.papervision3d.cameras.Camera3D;
    import org.papervision3d.render.BasicRenderEngine;
    import org.papervision3d.scenes.Scene3D;
    import org.papervision3d.view.Viewport3D;
   
    import org.papervision3d.materials.*;
    import org.papervision3d.objects.primitives.*;
    import org.papervision3d.materials.utils.*;

    public class Main03 extends MovieClip
    {
       
        private var face1:MovieClip;
        private var face2:MovieClip;
        private var face3:MovieClip;
        private var face4:MovieClip;
        private var face5:MovieClip;
        private var face6:MovieClip;
       
        private var viewport:Viewport3D;
        private var renderer:BasicRenderEngine;
        private var scene:Scene3D;
        private var camera:Camera3D;
        private var mm1:MovieMaterial;
        private var mm2:MovieMaterial;
        private var mm3:MovieMaterial;
        private var mm4:MovieMaterial;
        private var mm5:MovieMaterial;
        private var mm6:MovieMaterial;

        private var cube1:Cube;
        private var cube2:Cube;
        private var cube3:Cube;
        private var cube4:Cube;
        private var cube5:Cube;
        private var cube6:Cube;
        private var cube7:Cube;
        private var cube8:Cube;
        private var cube9:Cube;
        private var cube10:Cube;
       
        private var cube11:Cube;
        private var cube12:Cube;
        private var cube13:Cube;
        private var cube14:Cube;
        private var cube15:Cube;
        private var cube16:Cube;
        private var cube17:Cube;
        private var cube18:Cube;
        private var cube19:Cube;
        private var cube20:Cube;
       
        private var cube21:Cube;
        private var cube22:Cube;
        private var cube23:Cube;
        private var cube24:Cube;
        private var cube25:Cube;
        private var cube26:Cube;
        private var cube27:Cube;
        private var cube28:Cube;
        private var cube29:Cube;
        private var cube30:Cube;
       
        private var cube31:Cube;
        private var cube32:Cube;
        private var cube33:Cube;
        private var cube34:Cube;
        private var cube35:Cube;
        private var cube36:Cube;
        private var cube37:Cube;
        private var cube38:Cube;
        private var cube39:Cube;
        private var cube40:Cube;
       
        private var cube41:Cube;
        private var cube42:Cube;
        private var cube43:Cube;
        private var cube44:Cube;
        private var cube45:Cube;
        private var cube46:Cube;
        private var cube47:Cube;
        private var cube48:Cube;
        private var cube49:Cube;
        private var cube50:Cube;
       
        private var cube51:Cube;
        private var cube52:Cube;
        private var cube53:Cube;
        private var cube54:Cube;
        private var cube55:Cube;
        private var cube56:Cube;
        private var cube57:Cube;
        private var cube58:Cube;
        private var cube59:Cube;
        private var cube60:Cube;
       
        private var cube61:Cube;
        private var cube62:Cube;
        private var cube63:Cube;
        private var cube64:Cube;
        private var cube65:Cube;
        private var cube66:Cube;
        private var cube67:Cube;
        private var cube68:Cube;
        private var cube69:Cube;
        private var cube70:Cube;
       
        private var cube71:Cube;
        private var cube72:Cube;
        private var cube73:Cube;
        private var cube74:Cube;
        private var cube75:Cube;
        private var cube76:Cube;
        private var cube77:Cube;
        private var cube78:Cube;
        private var cube79:Cube;
        private var cube80:Cube;
       
        private var cube81:Cube;
        private var cube82:Cube;
        private var cube83:Cube;
        private var cube84:Cube;
        private var cube85:Cube;
        private var cube86:Cube;
        private var cube87:Cube;
        private var cube88:Cube;
        private var cube89:Cube;
        private var cube90:Cube;
       
        private var cube91:Cube;
        private var cube92:Cube;
        private var cube93:Cube;
        private var cube94:Cube;
        private var cube95:Cube;
        private var cube96:Cube;
        private var cube97:Cube;
        private var cube98:Cube;
        private var cube99:Cube;
        private var cube100:Cube;
       
        private var cube101:Cube;
        private var cube102:Cube;
        private var cube103:Cube;
        private var cube104:Cube;
        private var cube105:Cube;
        private var cube106:Cube;
        private var cube107:Cube;
        private var cube108:Cube;
        private var cube109:Cube;
        private var cube110:Cube;
       
        private var cube111:Cube;
        private var cube112:Cube;
        private var cube113:Cube;
        private var cube114:Cube;
        private var cube115:Cube;
        private var cube116:Cube;
        private var cube117:Cube;
        private var cube118:Cube;
        private var cube119:Cube;
        private var cube120:Cube;

        private var cube121:Cube;
        private var cube122:Cube;
        private var cube123:Cube;
        private var cube124:Cube;
        private var cube125:Cube;
        private var cube126:Cube;
        private var cube127:Cube;
        private var cube128:Cube;
        private var cube129:Cube;
        private var cube130:Cube;
       
        private var cube131:Cube;
        private var cube132:Cube;
        private var cube133:Cube;
        private var cube134:Cube;
        private var cube135:Cube;
        private var cube136:Cube;
        private var cube137:Cube;
        private var cube138:Cube;
        private var cube139:Cube;
        private var cube140:Cube;
       
        private var cube141:Cube;
        private var cube142:Cube;
        private var cube143:Cube;
        private var cube144:Cube;
        private var cube145:Cube;
        private var cube146:Cube;
        private var cube147:Cube;
        private var cube148:Cube;
        private var cube149:Cube;
        private var cube150:Cube;
       
        private var cube151:Cube;
        private var cube152:Cube;
        private var cube153:Cube;
        private var cube154:Cube;
        private var cube155:Cube;
        private var cube156:Cube;
        private var cube157:Cube;
        private var cube158:Cube;
        private var cube159:Cube;
        private var cube160:Cube;
       
        private var cube161:Cube;
        private var cube162:Cube;
        private var cube163:Cube;
        private var cube164:Cube;
        private var cube165:Cube;
        private var cube166:Cube;
        private var cube167:Cube;
        private var cube168:Cube;
        private var cube169:Cube;
        private var cube170:Cube;
       
        private var cube171:Cube;
        private var cube172:Cube;
        private var cube173:Cube;
        private var cube174:Cube;
        private var cube175:Cube;
        private var cube176:Cube;
        private var cube177:Cube;
        private var cube178:Cube;
        private var cube179:Cube;
        private var cube180:Cube;
       
        private var cube181:Cube;
        private var cube182:Cube;
        private var cube183:Cube;
        private var cube184:Cube;
        private var cube185:Cube;
        private var cube186:Cube;
        private var cube187:Cube;
        private var cube188:Cube;
        private var cube189:Cube;
        private var cube190:Cube;
       
       
        private var cube191:Cube;
        private var cube192:Cube;
        private var cube193:Cube;
        private var cube194:Cube;
        private var cube195:Cube;
        private var cube196:Cube;
        private var cube197:Cube;
        private var cube198:Cube;
        private var cube199:Cube;
        private var cube200:Cube;
       
        private var cube201:Cube;
        private var cube202:Cube;
        private var cube203:Cube;
        private var cube204:Cube;
        private var cube205:Cube;
        private var cube206:Cube;
        private var cube207:Cube;
        private var cube208:Cube;
        private var cube209:Cube;
        private var cube210:Cube;
       
        private var cube211:Cube;
        private var cube212:Cube;
        private var cube213:Cube;
        private var cube214:Cube;
        private var cube215:Cube;
        private var cube216:Cube;
        private var cube217:Cube;
        private var cube218:Cube;
        private var cube219:Cube;
        private var cube220:Cube;
       
        private var cube221:Cube;
        private var cube222:Cube;
        private var cube223:Cube;
        private var cube224:Cube;
        private var cube225:Cube;
        private var cube226:Cube;
        private var cube227:Cube;
        private var cube228:Cube;
        private var cube229:Cube;
        private var cube230:Cube;
       
        private var cube231:Cube;
        private var cube232:Cube;
        private var cube233:Cube;
        private var cube234:Cube;
        private var cube235:Cube;
        private var cube236:Cube;
        private var cube237:Cube;
        private var cube238:Cube;
        private var cube239:Cube;
        private var cube240:Cube;

        private var cubeSize:Number;
        private var spacing:Number;
        private var polys:Number;
        private var numCubes:Number;
        private var numCols:Number;
        private var numRows:Number;
       
        public function Main03():void
        {
            var i:Number;
            var j:Number;
           
            cubeSize = 15;
            spacing = 25;
            polys = 1;
            numCols = 20;
            numRows = 12;
            numCubes = numCols * numRows;
           
            stage.quality = StageQuality.LOW;
           
            var offset:Number = spacing/2;
            var startX:Number = ((stage.stageWidth/2) * -1) + offset;
            var startY:Number = ((stage.stageHeight/2)) - offset;
           
            viewport = new Viewport3D(0, 0, true, true);
            addChild(viewport);
            viewport.buttonMode = false;
                                   
            renderer = new BasicRenderEngine();
                                   
            scene = new Scene3D();
                                   
            camera = new Camera3D();
            camera.zoom = 11;
            camera.focus = 100;                 
           
            face1 = new Face1();
            face2 = new Face2();
            face3 = new Face3();
            face4 = new Face4();
            face5 = new Face5();
            face6 = new Face6();
           
            for (i=1; i< =6; i++) {
                addChild(this["face"+i]);
                this["mm"+i] = new MovieMaterial(this["face"+i]);
                this["mm"+i].interactive = false;
                this["mm"+i].smooth = false;
                this["mm"+i].animated = false;
            }

            var counter:Number = 1;
            for (i=1; i<=numCols; i++) {
                for (j=1; j<=numRows; j++) {
                    this["cube"+counter] = new Cube(new MaterialsList({front:mm1, back:mm2, left:mm3, right:mm4,top:mm5, bottom:mm6}), cubeSize, cubeSize, cubeSize, polys, polys, polys);
                    scene.addChild(this["cube"+counter]);
                    this["cube"+counter].x = startX + (spacing * (i - 1));
                    this["cube"+counter].y = startY - (spacing * (j - 1));
                    counter++;
                }
            }
           
            for (i=1; i<=6; i++) {
                removeChild(this["face"+i]);
            }

            stage.addEventListener(Event.ENTER_FRAME, stage_ENTER_FRAME);
        }
       
       
        private function stage_ENTER_FRAME(event:Event):void
        {
            var xDist:Number = mouseX - stage.stageWidth * 0.5;
            var yDist:Number = mouseY - stage.stageHeight * 0.5;
           
            for (var i:Number = 1; i <= numCubes; i++) {
                this["cube"+i].rotationY += xDist * 0.05;
                this["cube"+i].rotationX += -yDist * 0.05;
            }
           
            renderer.renderScene(scene, camera, viewport);
        }
       
    }
}

If anyone knows how you can declare the private variables for the cubes in a loop then I’d love to know how you do it.

Remember to download and install the Papervision 2.0 (Great White) classes from GoogleCode and modify the class path in the publish settings dialogue box.

You can download the zipped up source files here.

Posted in Flash, Papervision 3D | 6 Comments »

Installing Papervision3D on Flash and Flex

April 24th, 2008 by Adrian Parr

Papervision3D, Flex and Flash Logos

Fabian Vercuiel has posted a useful entry on his blog titled ‘Installing Papervision3D on Flash and Flex‘ which outlines how to get up and running with Papervision3D using Flash and Flex.

He covers, downloading and installing a subversion client such as TortoiseSVN, getting the Great White Papervision3D files from GoogleCode, downloading and installing the Flash Extension (mxp file), adding the Papervision3D class-path, adding the SWC file in Flex Builder.

This is all pretty useful information if you haven’t done it before.

Posted in Flash, Flex, Papervision 3D | 1 Comment »

101 Adobe AIR Resources

April 24th, 2008 by Adrian Parr

Adobe AIR Logo

Jason Bartholme has posted a good list of 101 Adobe AIR Resources on his blog. He has broken the links down in to the following categories …

  • Getting Started
  • Application Collections
  • Articles
  • Bloggers
  • Language-Specific Integration
  • Popular Applications
  • Resources
  • Third Party Integration
  • Tutorials

Posted in AIR | 1 Comment »

BugZap Game in Flash IDE Panel

April 22nd, 2008 by Adrian Parr

Today I created a little game that can be played in the Flash CS3 IDE. It is an extension for Flash CS3 (mxp file) that adds a new panel to the interface. You can access the panel by going to Window > Other Panels > BugZap.

BugZap Panel

The game pretty simple but quite addictive. You move the gun at the bottom left and right, the object being to shoot down the alien before he lands. The alien moves around (slowly at first and then getting quicker) and you have to keep track of him. The more you shoot and miss, the lower he gets.

If you shoot him, you move on to the next level, where he starts from the top again but he moves quicker. There is a maximum of 30 levels. The game is over either when he lands or you get to the end of level 30 (which ever comes first).

The lower he is when you kill him, the more points you get. The level number act as a score multiplyer, so more points are awarded the further you get through the levels (and the faster he becomes in both directions).

Your highest score is recorded and maintained between sessions, giving you a personal best to beat. My current high score is 25,628.

BugZap Extension Package

You can download the Flash CS3 Extension here.

Posted in ActionScript 3.0, Flash, Game | 3 Comments »

XHTML Video Embed - WordPress Plugin

April 21st, 2008 by Adrian Parr

I have just installed the XHTML Video Embed plugin for WordPress, written by SaltwaterC.

It allows you to embed Flash videos from sites like YouTube and Google Video in to your WordPress blog easily, and the resulting HTML code will be XHTML compliant.The syntax is as follows …

[youtube]http://www.youtube.com/watch?v=SVm5NW__sxk[/youtube]

Here is a test, using Philip Kerman’s Why ActionScript 3.0? video.


Posted in WordPress | 6 Comments »

ActionScript 3.0 Quiz

April 21st, 2008 by Adrian Parr

Learning ActionScript 3.0

I’ve just come across a little online quiz where you can test your ActionScript 3.0 knowledge. It’s a site that accompanies the Learning ActionScript 3.0 book from O’Reilly. It’s not a particulary comprehensive quiz, more of a bit of fun.

I managed to get full marks on most sections, but it looks like I need to gem up a bit on Sound and Design Patterns …

Quiz results

Give it a whirl and see how you get on.

Posted in ActionScript 3.0, Flash | 1 Comment »

Flash on the Beach 08

April 21st, 2008 by Adrian Parr

Flash on the Beach 08

Yipee! Ordered my Flash on the Beach 08 ticket this morning. The tickets went on sale this weekend (just gone) and already 150 tickets have been sold. The first 250 tickets can be purchased at the reduced Super Early Bird price of £199 for the 3-day conference pass. They are also holding two pre-conference workshops on the Sunday (28th Sept), I have booked for the Papervision3D session.

Currently, there are 94 Super Early Bird tickets left. So get in there quick if you want to save some money.

At the time of writing, the confirmed speaker list (with links to their personal websites) includes …

I’m really looking forward to it. I went to both the 2006 and the 2007 conference and it is a fantastic event. We’re so lucky to have an event like this held in the UK. I always come away from it inspired, fired up and wanting to learn more. Many thanks to John Davey for organising this awesome event every year.

If you haven’t been before, get your tickets and see for yourself. I’ll see you there …

Posted in AIR, ActionScript 3.0, Adobe, Conferences and Events, Flash, Flex, Papervision 3D | No Comments »

Amazing Physics Game - Gymnast (not in Flash)

April 18th, 2008 by Adrian Parr

I just had to blog about this freeware game I have come across. It is called Gymnast, and it is built my a guy called Walaber who does this in his spare time. He has a blog here. I haven’t played it yet, just watching the trailer was enough to make me want to tell people about it. He’s actually got some pretty awesome stuff on his site.

Anyway, here is the trailer for Gymnast.

I used to do gymnastics, many years ago, so this game has special appeal for me. It’s a shame I haven’t got any Xbox360 controllers.

Posted in Game | 2 Comments »

Flash Media Encoder 2.5 Released with H.264 Encoding

April 18th, 2008 by Adrian Parr

Flash Media Encoder 2.5

Adobe have released version 2.5 of Flash Media Encoder. They have added support for the H.264 video codec and the Advanced Audio Coding (AAC) audio codec. The AAC support is via a plug-in which can be purchased from MainConcept ($249).

It’s great to see support for H.264 being rolled out across the Adobe product range.

H.264 codec selected

Posted in Adobe, Flash | No Comments »

Flex app causing weird browser crash

April 16th, 2008 by Adrian Parr

This afternoon I stumbled across Kaushik Datta’s blog post ‘Toyota’s Model Selector in Flex‘ where he mentions a new Flex site for Toyota. So I thought I’d take a look.

Initially it looks very nice, but after a short play with the sliders my Firefox browser crashed and displayed a weird error message. It was not an error message I have ever seen before.

Windows XP, Firefox 2.0.0.13 Error Message: Illegal Operation in Plugin

Illegal Operation in Plugin

I restarted Firefox and tried it again, but the same thing happened. It is 100% repeatable.

So then I thought I’d give it give it a whirl in Internet Explorer (IE 7.0.5730.11) and, bingo, another new error message.

Windows XP, IE Error Message

Internet Explorer Error Message

The version of the Flash Player I have installed is WIN 9,0,115,0 (Debug player).

You can check your player version here.

And then finally the whole browser would die.

Toyota Model Selector

Take a look at the site and see if it crashes your browser too!

It also crashed Opera 9.24 on my Windows XP machine.

NOTE: It doesn’t crash in Firefox or Safari on my MacBook.

Posted in Bug, Flex | 3 Comments »

AS3 KeyPoll class

April 15th, 2008 by Adrian Parr

Richard Lord over at Flash Game Code has released a few useful AS3 classes. One of which is the KeyPoll class. This is really useful because the Key.isDown event no longer exists in ActionScript 3.

Here is a quick example I knocked up …

The Flash source code for this can be downloaded here.

Posted in ActionScript 3.0, Flash | 2 Comments »

Flex SDK coding conventions and best practices

April 15th, 2008 by Adrian Parr

I just came across this page which I thought was useful and would share with you all.

http://opensource.adobe.com/wiki/display/flexsdk/Coding+Conventions

It covers topics such as Naming, Language Usage, File Organization, Formatting & ASDoc.

Posted in Adobe, Flex | No Comments »

Adobe AIR Stopwatch

April 15th, 2008 by Adrian Parr

Stopwatch

I’ve just built a handy stopwatch Adobe AIR app. Basically, it is a simple timer with Hrs:Mins:Secs.Ms, Start, Pause, Continue and Reset. The number of times I have needed a simple timer like this and havn’t had one, so I decided to build one myself. I’ve used Adobe AIR so that it installs on your system like any other application and is also cross-platform.

Here is the Flash movie of it …

And you can install the AIR file from here and here is a version that uses SWFObject to embed the install badge.

The source files are available here.

Adobe AIR Marketplace

It is also available for download from the Adobe AIR Marketplace.

UPDATE 26/08/2008: Simple Stopwatch 1.0 has been selected by Softpedia for inclusion on their website.

Posted in AIR, ActionScript 3.0 | 3 Comments »

Custom Context Menu in AS3

April 15th, 2008 by Adrian Parr

I’ve just been having a play with creating a custom context menu in Flash using AS3. Here is the movie …

And here is the ActionScript …

package {
    import flash.display.Sprite;
    import flash.ui.ContextMenu;
    import flash.ui.ContextMenuItem;
    import flash.events.ContextMenuEvent;
    import flash.net.navigateToURL;
    import flash.net.URLRequest;

    public class Main extends Sprite
    {
        private var menuItemLabel:String = "© Adrian Parr";
        private var url:String = "http://www.adrianparr.com";
        private var cm:ContextMenu;

        public function Main()
        {
            cm = new ContextMenu();
            cm.hideBuiltInItems();
            var cmi:ContextMenuItem = new ContextMenuItem(menuItemLabel);
            cmi.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, menuItemSelected);
            cm.customItems.push(cmi);
            this.contextMenu = cm;
        }
       
        private function menuItemSelected(evt:ContextMenuEvent):void
        {
            var req:URLRequest = new URLRequest(url);
            navigateToURL(req, '_blank');
        }
    }
}

You can download the Flash source files here.

Posted in ActionScript 3.0, Flash, Sample Code | 20 Comments »

Adobe AIR Eboy Poster

April 10th, 2008 by Adrian Parr

At the Adobe on AIR Tour in London yesterday, everyone was given a cool postcard created by Eboy. Mike Chambers also gave away a couple of posters to people. You can view a high-res version of the poster here.

Posted in AIR, Adobe, Conferences and Events | 5 Comments »

Search for images using Firefox Add-on PicLens

April 3rd, 2008 by Adrian Parr

I recently discovered a Firefox Add-on called PicLens (created by a company called Cooliris) that nicely displays image search results in a seamless scrolling 3D space, and then allows you to view them as a slidshow. See screengrabs below …

It runs very smoothly and I am impressed with it’s ease of use. You can search Google Images, Flickr, Picasa, Yahoo, Facebook and many more.

They also have a WordPress plugin that I haven’t tried yet, but will check out soon.

I recommend you take a look at it.

Posted in Websites | No Comments »

Jing Project and screencast.com

April 3rd, 2008 by Adrian Parr

I have just come across a really handy and well executed application and website that allows you to easily capture (image or video) and share what you are doing on screen.

The application is called the Jing Project, and is freely available for both Mac and Windows. It has been created by TechSmith who have many years of experience in this field (they produce Camtasia Studio as a commercial product).

Once you have captured your screen as either an image or video it is automatically encoded to Flash video and you then have the option of uploading it to your own area on screencast.com, embedding it on your own page or blog, uploading it to Flickr (images only), saving it as a SWF file locally, or uploading it to your FTP area.

It is very easy to use and it is FREE! I highly recommend you check it out.

Posted in Websites | No Comments »

Setting data types to int, uint and Number

April 3rd, 2008 by Adrian Parr

Recently I have been having a play around with using different data types such as int and uint (rather than using Number all the time). To help me see which data types accept which values, I have put together a little chart that displays some example vaules and shows if they are valid or not (and if not, the Compiler Errors). Some of the results are pretty obvious, others not so.

Green = All fine.
Amber = A rounded value is stored and NO error is thrown.
Red = Compiler warning/error is thrown.

Data types chart

The Adobe LiveDocs have some more information on the subject.

There has also been some discussion in the past regarding the performance of of int and uint from Grant Skinner and Sho Kuwamoto, but I think Tink takes a balanced view of the subject and I will be following him (as much as I can).

Posted in ActionScript 3.0 | No Comments »

Colour Line Drawing - Part 2

April 2nd, 2008 by Adrian Parr

Following on from yesterday’s post, I’ve had chance to update the Colour Line Drawing demo to include gradual colour changing (instead of random) and fixed the saving of the image to a jpeg file.

The saving of the image is done using the as3corelib library (thanks to Henry Jones for providing an example of this). Thanks also to Ryan Taylor for his color spectrum blog post.

Colour Line Drawing Screengrab

Click here to launch the demo.

You can download the source files here.

Here is the code in the Main document class …

package {
    import flash.display.MovieClip;
    import flash.display.Graphics;
    import flash.display.Stage;
    import flash.display.BitmapData;
   
    import flash.events.MouseEvent;
    import flash.events.KeyboardEvent;
   
    import flash.ui.Mouse;
    import flash.ui.Keyboard;
   
    import flash.utils.ByteArray;
   
    import flash.net.navigateToURL;
    import flash.net.URLRequestHeader;
    import flash.net.URLRequest;
    import flash.net.URLLoader;
    import flash.net.URLRequestMethod;
   
    import com.adobe.images.JPGEncoder;

    public class Main extends MovieClip
    {
        private var canvas:MovieClip;
        private var serverPath:String = "";
        private var colourIncrement:int = 0;
       
        public function Main():void
        {
            Mouse.hide();
            stage.showDefaultContextMenu = false;
            stage.addEventListener(KeyboardEvent.KEY_DOWN, reportKeyDown);
           
            canvas = new MovieClip();
            canvas.graphics.beginFill(0x000000);
            canvas.graphics.drawRect(0, 0, 500, 375);
            canvas.graphics.endFill();
            canvas.graphics.moveTo(mouseX, mouseY);
            canvas.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoved);
            canvas.addEventListener(MouseEvent.CLICK, clearScreen);
            addChild(canvas);
        }
       
        private function mouseMoved(evt:MouseEvent):void
        {
            var nColorPercent:Number = colourIncrement / 200;
            colourIncrement += 1;
            var nRadians:Number = (-360 * nColorPercent) * (Math.PI / 180);
            var nRed:Number = Math.cos(nRadians) * 127 + 128 < < 16;
            var nGreen:Number = Math.cos(nRadians + 2 * Math.PI / 3) * 127 + 128 << 8;
            var nBlue:Number = Math.cos(nRadians + 4 * Math.PI / 3) * 127 + 128;
            var nColour:Number = nRed | nGreen | nBlue;

            canvas.graphics.lineStyle(60, nColour, 1);
            canvas.graphics.lineTo(mouseX, mouseY);
            evt.updateAfterEvent();
        }
       
        private function clearScreen(evt:MouseEvent):void
        {
            canvas.graphics.clear();
            canvas.graphics.beginFill(0x000000);
            canvas.graphics.drawRect(0, 0, 500, 375);
            canvas.graphics.endFill();
            canvas.graphics.moveTo(mouseX, mouseY);
        }
       
       
        private function reportKeyDown(evt:KeyboardEvent):void
        {
            if (evt.keyCode == Keyboard.SPACE) {
                createJPG(canvas, 90, "colour_line_drawing");
            } else if (evt.keyCode == Keyboard.ESCAPE) {
                if (stage.displayState == "normal") {
                    stage.displayState = "fullScreen";
                }
            }
        }
       
        function createJPG(m:MovieClip, q:Number, fileName:String)
        {
            var jpgSource:BitmapData = new BitmapData (m.width, m.height);
            jpgSource.draw(m);
            var jpgEncoder:JPGEncoder = new JPGEncoder(q);
            var jpgStream:ByteArray = jpgEncoder.encode(jpgSource);
           
            var header:URLRequestHeader = new URLRequestHeader ("Content-type", "application/octet-stream");
                       
            var jpgURLRequest:URLRequest = new URLRequest ( serverPath+"jpg_encoder_download.php?name=" + fileName + ".jpg");      
            jpgURLRequest.requestHeaders.push(header);       
            jpgURLRequest.method = URLRequestMethod.POST;            
            jpgURLRequest.data = jpgStream;
           
            var jpgURLLoader:URLLoader = new URLLoader();      
            navigateToURL(jpgURLRequest, "_blank");
        }
       
    }
}

Posted in ActionScript 3.0, Flash, Sample Code | 4 Comments »

Setting the framerate dynamically using AS3

April 2nd, 2008 by Adrian Parr

Here is a little example to show you how to dynamically set the framerate of your Flash movie using AS3. In this example I have also included a slider component to allow you to change the framerate, to prove that it is doing it on-the-fly.

You can download the source files here.

Here is the AS3 code in the Main document class …

package
{
    import flash.display.MovieClip;
    import flash.events.Event;
    import fl.events.SliderEvent;

    public class Main extends MovieClip
    {

        private var myBall:MovieClip;
        private var sliderValue:Number = 1;
       
        public function Main():void
        {
            stage.frameRate = sliderValue;
           
            myBall = new Ball();
            myBall.x = 0 - myBall.width/2;
            myBall.y = stage.stageHeight/2;
            addChild(myBall);
           
            sliderLabel.text = sliderValue+" fps";
           
            myBall.addEventListener(Event.ENTER_FRAME, frameLoop);
            slider.addEventListener(SliderEvent.THUMB_DRAG, sliderDrag);
        }
       
        private function frameLoop(evt:Event):void {
            myBall.x += 10;
            if (myBall.x > stage.stageWidth + myBall.width) {
                myBall.x = 0 - myBall.width/2;
            }
        }

        private function sliderDrag(e:SliderEvent):void {
            sliderValue = e.target.value;
            stage.frameRate = sliderValue;
            sliderLabel.text = sliderValue+" fps";
        }
       
    }
}

Posted in ActionScript 3.0, Flash, Sample Code | 2 Comments »

« Previous Entries