Archive for Flash Archives - Purple Squirrels
Here is another function that can be added to my ColourUtil class. This one gets a specified number of colour steps between two blended colours.
I thought it would be much harder to achieve than it is, luckily the AS3 Color class has the static method interpolateColor which calculates a single colour at a specified position using a ratio value.
The function below is a simplified version of the function found at AS3 Adventure. This blog post also has a much better explanation of the interpolateColor method, and a great demo of the function.
var arr = ,
for (i = 1; i < steps; i++)
ratio = i / steps;
In an AIR project I am currently working on I wanted to have native alert boxes in my app rather than building custom pop up boxes. There is no built in way to get native alerts in AIR and I remembered reading this interview over at leebrimelow.com a while back where a developer at Illume used StageWebView to trigger an alert from a HTML page. So I decided to make a class called NativeAlert that uses AIR’s HTMLLoader to trigger alerts.
I’ve been trying to find ways to improve the performance of paint mixing with Flash and I though I could try using Stage3D for hardware accelerated graphics. But then I realised that Stage 3D is optimised for polygons and 3D models so it was probably not the best solution. I wanted to see if it was possible anyway and thanks to the Starling Framework and a bit of help from Thibault Imberts book it turns out its possible using the RenderTexture on an Image object, but it’s not really possible to do any complex drawing. Starling’s Image object is the equivalent of the Bitmap class, which is using to display any BitmapData. I made an example which you can see below.
By creating a RenderTexture for an Image object, you can use the RenderTexture’s draw() method to draw another Image’s texture onto it. This is similar to using BitmapData and draw() to draw one bitmap onto another. But Stage3 doesn’t use pixels, it uses textures mapped to triangles instead therefore at this stage it’s not possible to use something like getPixel() for get colour data from the ‘canvas’.
Well, here we are with version 2 of my Flash spray paint stencils. I’ve been meaning to do a post on this for a while now! I created this version during the development of PixelPerfect - a digital graffiti wall and and photo booth. This time the paint is rendered much quicker by doing away with an embedded for loop that was in V1. This time instead of looping through each pixel on the canvas, I am drawing using copyPixels utilising an alpha bitmap as an alpha channel and matrices to transform them correctly based on the scale, rotation and position of the stencil movieclip. Another cool feature I added was a ‘muck’ layer on the stencil. Now paint that was on top of the stencil stays on the stencil – just like in real life!
Well I’ve been pretty busy latey and not much has happened on here for over month. I decided I should post some work and experiments I have been doing with building iOS apps with AIR for iOS. So below is a series of video demos of some experiments and actual projects I have been doing recently. All apps were compiled using Flash CS5. Note that none of these apps are available in the public app store.
Adventure Golf is a simple mini golf game I worked on for a client last year which was written in AS2. I decided to see how easy it was to build existing games for iOS so I converted all the AS2 to AS3 with minimal code changes/optimisations and Published for iOS. All graphics are pulled from the Flash library, with levels laid out on the stage on different frames. The hardest thing I had to do was resize the stage. Everything else just worked and I was surprised at how well it did considering it was converted from old AS2 code. I would like build this into a full app one day when I have lots of spare time!
I was going to record a full demo of my upcoming multitouch painting application ‘Paint Maestro 2′, but I had some technical difficulties so here is a small demo of the stencils. The application is written in AS3 and uses Adobe AIR. I posted this video over at NUI Group a couple weeks ago and got a pretty good response. But I never posted it here so here it is below. More info on Paint Maestro 2 will be coming soon so stay tuned…
If your building an AIR application and you want to have your own native custom file type association for your app, it is surprisingly easy to to with ActionScript 3 in air as I found out. I was unable to find any information on reading and writing custom files so I decided to post my experience here. I remembered watching a tutorial by Lee Brimelow at gotoAndLearn a while back where he showed how to decode a BMP file using ByteArrays. So I revised the tutorial and used the ideas from this to create my own file.
For this example I will create a simple graphic file containing some metadata and some bitmap data.
1. Write out the specification
The first thing you should do is map out a spec sheet for it, outlining all the data and datatypes you want to store and the order it will be written. This is important so you don’t forget later on, and if you want others to be able to use it as well. Your custom file must be read and written in the same order. I just did it in Notepad like so:
image name – String
image author – String
image width – int
image height – int
bitmapdata length – int
bitmapdata – bytearray
Note that this is a very basic file, so you could save all different data types such as Boolean, Number, String, int, uint etc.
2. Get your data into a ByteArray
To write the file first we need to create a bytearray and then using the write methods of the ByteArray class write the bits of data to it:
var name:String = "a new picture";
var author:String = "Ted Mosby";
var bitmap:Bitmap = new Bitmap(new BitmapData(300,300,false,0xFF0000));
// convert bitmap to byte array
var bitmapBA:ByteArray = bitmap.bitmapData.getPixels();
// create a bytearray to save into file
var bytearray:ByteArray = new ByteArray();
// write the image name
// write author
// write image width and height
// write bitmap data length
// write the bitmap
NiceComps is a set of ActionScript 3 UI components that I have been slowly working on over about the last six months. I got tired of the ugly built in components that come with Flash and I don’t particularly like the look of MinimalComps. I wanted UI components that looked nice and were easy to use so I set out to create my own. The design of NiceComps was inspired by the Windows Phone 7 UI and one unique feature I wanted to include was the ability to easily and dynamically change the colour of all components being used at once at runtime. Since I started them they have evolved quite a lot as I’ve used them in projects, and therefore they are still not 100% finished. But I have decided to release a ‘Beta’ version which you can try out. There is a demo below of all the components so far (I think it’s all). The code is quite messy at the moment but I plan to tidy it all up in the near future. And please let me know if you find any bugs. Currently the most stable components are the DisplayScroller, CheckBox, InputBox, Button and DropDown as these are the ones I use most. I’ve got a lot of optimising to do because there is quite a bit of duplicate and unused code.
I found a Jackson Pollock style painting example written in AS2 called ‘Splatter’ over at stamen.com. I wanted to include a Pollock style brush in my painting application so I converted it into AS3. Just copy and past the following code into a new AS3 document in Flash.
//set up some vars
var new_size_influence:Number = 0.5;
var mid_point_push:Number = 0.75;
var max_line_width:Number = (Math.random() * 50) + 50;
//vars for line colour and alpha
var _colour:uint = 0x000000;
var _alpha:Number = 1;
//create var to hold all mouse position data
var obj:Object = new Object();
obj.x = 0;
obj.y = 0;
obj.start_x = obj.mid_x = obj.end_x = (stage.stageWidth / 2);
obj.start_y = obj.mid_y = obj.end_y = (stage.stageHeight / 2);
obj.size = 0;
//add mouse down event listener
//create random colour
_colour = Math.random()*0xFFFFFF;
//set all line data to current mouse position
obj.start_x = obj.mid_x = obj.end_x = mouseX;
obj.start_y = obj.mid_y = obj.end_y = mouseY;
//add mouse up and mouse move event listeners
//remove mouse up and mouse move event listeners
//find the mid point between current and last mouse position
obj.mid_x = ((obj.end_x - obj.start_x) * (1 + mid_point_push)) + obj.start_x;
obj.mid_y = ((obj.end_y - obj.start_y) * (1 + mid_point_push)) + obj.start_y;
//set start to last point
obj.start_x = obj.end_x;
obj.start_y = obj.end_y;
//set end to current point
obj.end_x = mouseX;
obj.end_y = mouseY;
//get distance between start and end point
var distance:Number = Math.sqrt(Math.pow((obj.end_x - obj.start_x), 2) + Math.pow((obj.end_y - obj.start_y), 2));
//set size depending on distance
var new_size:Number = max_line_width / distance;
obj.size = (new_size_influence * new_size) + ((1 - new_size_influence) * obj.size);
//draw the line
graphics.curveTo(obj.mid_x, obj.mid_y, obj.end_x, obj.end_y);
var dd:Number = Math.sqrt(Math.pow((obj.end_x - obj.start_x), 2) + Math.pow((obj.end_y - obj.start_y), 2));
for (var i:int = 0; i<Math.floor(5*Math.pow(Math.random(), 4)); i++)
// positioning of splotch varies between ±4dd, tending towards 0
var splat_range:int = 1;
var x4:Number = dd * 1 * (Math.pow(Math.random(), splat_range) - (splat_range/2));
var y4:Number = dd * 1 * (Math.pow(Math.random(), splat_range) - (splat_range/2));
// direction of splotch varies between ±0.5
var x5:Number = Math.random() - 0.5;
var y5:Number = Math.random() - 0.5;
var d_:Number = obj.size*(0.5+Math.random());
//draw the splotches
graphics.lineStyle(d_, _colour, _alpha);
After seeing the Wii Spray demo videos a year or so ago I have since been wondering how they did the stencils. I couldn’t find any information on how to achieve an effect like paint stenciling so today I decided to give it a shot myself. Below is a small demo where you can have a go and spraying some stencils. Instructions are in the bottom left corner. Here is how it works:
Layers (top to bottom):
- Draw Layer Bitmap – contains BitmapData where the spray paint is drawn
- Stencil MovieClip – the stencil you see on screen containing artwork with transparency
- Stencil BitmapData – stencil is drawn to this bitmap data (not added to display list)
- Canvas Bitmap – where final artwork is drawn to
- when stencil is moved and placed somewhere, the Stencil BitmapData is filled with red, then the Stencil MovieClip is drawn on top.
- you then paint with the mouse – paint is drawn to Draw Layer Bitmap
- on mouse up a Temporary BitmapData is created
- it then loops through all pixels in Stencil BitmapData – if pixel is Red: copy corresponding pixel from Draw Layer Bitmap to Temporary BitmapData
- when the loop is complete draw the Temporary BitmapData to the Canvas BitmapData
- Draw Layer Bitmap is then cleared