Stroep

Just a collection of random works – Mark Knol

Tag Archives: class

ChainI created a useful util-class to make delayed function calling easy. You can make a chain of functions, by adding them with a delay in milliseconds. This chain can be executed multiple times, even in reversed order.

Let’s take a look at a simple example of Chain.

var myChain:Chain = new Chain();   
myChain.addEventListener( Event.COMPLETE, onComplete);
myChain.add(one, 2000).add(two, 500).add(three, 1000).play(2);

function one():void { trace("one") }
function two():void { trace("two") }
function three():void { trace("three") }
function onComplete(e:Event):void { trace("done.") }

/* trace output:
one two three one two three done.
*/

 

What is happening here? At the first line we are instantiating the class, and the second line represents what chain does. There is a public function called ‘add’, which is an important part of the class. add(one, 2000) means: execute a function called ‘one’ after 2000 milliseconds. add(two,500).add(three,1000) are functions that are called after one is finished, with other delays. You can create your own rhythm/sequence/pattern. At the end we see play(2), which means: Execute the sequence of functions defined before, and repeat them 2 times. After that, dispatch event COMPLETE.

So that’s basically it. A cool part is you can play it reversed, using playReversed(). I am inspired by jQuery (which has nothing to do with this) to enable the ability to stick functions, but thats optional.

myChain.playReversed(2);

/* trace output:
three two one three two one
*/

 

Of course you can pauze/continue when you are playing, by calling stop() / doContinue(), and you can determine if it is currently playing using the isPlaying-getter.

These are all the public functions.

/// Constructor
public function Chain()

/// Adds a function at a specified interval (in milliseconds).
public function add(func:Function, delay:Number = 0):Chain

/// Start playing the sequence and calling functions
public function play(repeatCount:int = 0):void

/// Start playing the sequence reversed
public function playReversed(repeatCount:int = 0):void

/// Clears sequence list. Data will be removed.
public function clear():Chain

/// Stop playing, use doContinue to play futher from current point
public function stop():void

/// Continue playing after a stop
public function doContinue():Chain

/// Reset indexes
public function reset():void

/// Returns the string representation of the Chain private vars.
public function toString():String

/// Return chain is playing, stopped or completed
public function get isPlaying():Boolean
 

Hope you like it, let me know if you like to see extra/other related features.

Download: Chain (googlecode)

Updates
- Private functions are protected
- add-functions is now add(function, delay), where function is required.
- Class now extends EventDispatcher and play/playReversed dispatches Event.COMPLETE after playing sequence, instead of calling onComplete function
- Cleaned up code

Tagged with , , , .

Code snippetEasily create an image without taking care of loaders and URLRequest etc.
Most simple usage of the Image class:

import nl.stroep.utils.Image

var myImage:Image = new Image("myImage.jpg");
this.addChild(myImage)
 

You can also add the most common eventListeners to the image:

import nl.stroep.utils.Image

var myImage:Image = new Image("myImage.jpg");
this.addChild(myImage);

myImage.addEventListener(Event.COMPLETE, onImageLoaded );
myImage.addEventListener(IOErrorEvent.IO_ERROR, onImageError );
myImage.addEventListener(ProgressEvent.PROGRESS, onImageLoading );

function onImageLoaded (e:Event):void {
   trace("image loaded!");
}

function onImageError (e:IOErrorEvent):void {
   trace("image error", e.text);
}

function onImageLoading (e :P rogressEvent):void {
   trace("loading image.. bytesLoaded=" + e.bytesLoaded + " bytesTotal=" + e.bytesTotal );
}
 

Download
Check out the Image class at googlecode

Tagged with , , , .

Code snippetClass to work/calculate with colors. You can easily abstract the red, green or blue values from a color.

Usage:

import nl.stroep.utils.Color

// create orange color
var myColor:Color = new Color(0xFFCC00);

trace("value", myColor.value);
trace("red", myColor.red);
trace("green ", myColor.green);
trace("blue", myColor.blue);
 

Download
Check out the Color class at googlecode

Tagged with , , .

ImageSaver ClassMy ImageSaver() Class is ready for download.

Download
ยป ImageSaver v1.0

Features

  • Save a MovieClip, Sprite, Shape, Bitmap, Textfield or other kinds of displayObjects.
  • It doesn’t matter where or how your object is positioned on the stage, it gets the right bounds.
  • Never worry about bytearray’s / encoding anymore.
  • Save your displayobject as JPG or PNG (using Adobe’s encoders)
  • Ready-to-install PHP file.



How to use
1. Copy the ’save-my-image.php’ file to your server or testing environment (I use WAMP for testing)
2. Use ImageSaver like this example.

// Create saver instance + point to php file on server
var imageSaver:ImageSaver = new ImageSaver( "http://localhost:8080/save-my-image.php" );       

// additional: Add eventlisteners.
imageSaver.addEventListener ( Event.COMPLETE, onSaveComplete );
imageSaver.addEventListener ( IOErrorEvent.IO_ERROR, onSaveError );

// Save textfield as JPG with red background
imageSaver.save ( myTextField, "myfilename1.jpg", 0xFFFF0000 );

// Save bitmap as JPG with red background in low JPG quality (15)
imageSaver.save ( myBitmap, "myfilename2.jpg", 0xFFFF0000, 15 );

// Save a shape as transparent PNG
imageSaver.save ( myShape, "myfilename3.png" );

// Save a movieclip as half-transparent (red) PNG
imageSaver.save ( myMovieClip, "myfilename4.png", 0xCCFF0000 );

// handle events
       
private function onSaveError( e:IOErrorEvent ):void {
    trace ( "Image save failed. Error while saving: " + e.text );
}

private function onSaveComplete( e:Event ):void {
    trace ( "Image save completed"  );
}

The save-function works like this:

public function save( displayobject:DisplayObject, filename:String, backgroundColor:Number = 0×00FFFFFF, JPGquality:int = 85 ):void
       

I love to hear your suggestions. Feel free to use it too.

Creative Commons License
Creative Commons Licence

Tagged with , , , , .