Wordpress widget: TagPosts

I’ve written a small wordpress widget, to show in the sidebar all the posts from a given tag, or from the tag archive page you are visiting, or both.

You can see in action in this blog, if you select any tag from the sidebar in homepage.

I’ve taken inspiration from the Category Posts Widget 3.0 by James Lao.

You can download the plugin from the wordpress repository.

Intense Minimalism

One of the most promising guy I’m pleased to know, Davide ‘Folletto’ Casali, has just updated his domain and his blog, to intenseminimalism.com. To describe what he’s into he uses a nice formula: Hybrid Interaction Designer = Design × Psychology × Technology /  Simplicity × Complexity.

His work is really inspiring: simplicity and minimalism together are really a great tools to lay down and explain complex thoughts.

Sealed and dynamic classes

Back in the days of actionscript 2, you could add any property to pretty much any object, just calling it and setting a value, like this:

 myMovieClip.thePropertyILikeWithAStrangeName = "some string"

Actionscript 3.0 instead, being a more serious language, defines the basic class as sealed. That means that you can’t add any property on a object. You have to extends that class, and adding public properties.

public class MyClass extends MovieClip {
public thePropertyILikeWithAStrangeName:String
public function MyClass() {
super();
}
}

That class is sealed. You can’t

var foo:MyClass = new MyClass();
foo.anotherProperty = 42;

The dynamic keyword, permits to ‘unseal’ a class, giving back the possibility to add any property on runtime.

public dynamic class MyClass extends MovieClip {
public thePropertyILikeWithAStrangeName:String
public function MyClass() {
super();
}
}

Now you can:

var foo:MyClass = new MyClass();
foo.anotherProperty = 42;

There are speed and cleaness drawbacks on using dynamic classes. The rule is that if you use it, you should be able to seriously motivate it.

Note that there are few dynamic classes in the default Actionscript framework; the most known is of course, Object .

Read more:
livedocs
ActionScript 3: Dynamic Classes

Basic random functions

Here are some really basic (but useful) functions that returns random stuff…

The really basic one, is built in the Flash api and it returns a Number between 0 and 1:

var n:Number = Math.random();

A number in  a range

Sometime is more useful to have a random number in a different range, let’s say between 15 and 45, instead of 0 and 1.

public static function randNumber(max:Number = 1, min:Number = 0):Number {
    return Math.random() * (max - min) + min;
}

Random int

and what if we don’t need decimals? Round’em!

public static function randInt(max:Number = 1, min:Number = 0):int {
    return Math.round(Math.random() * (max - min) + min);
}

Random boolean

True or false? Flip a coin.

public static function bool():Boolean {
    return (Math.round(Math.random()) == 0);
}

Random sign

-1 or 1; Useful when you have to random select random rotation verse, for example, -1*45 or 1*45, rotates counterclockwise or clockwise.

public static function sign():Number {
    return (Math.round(Math.random()) == 0):-1:1;
}

Random color

Really useful when drawing debug shapes on screen..

public static function color():uint {
    return Math.random() * 0xFFFFFF;
}

Random point

Inside an area. I like to use the flash.geom.Rectangle struct to describe an area, more than using 4 parameters (Flash api are quite inconsistent about this choice)

public static function point(area:Rectangle):Point {
    return new Point(randInt(area.x, area.x + area.width), randInt(area.y, area.y + area.height));
}

Quickreference: display elements in a circle

How to display elements in a circle? A small function to do that is:

public static function circle(items:Array, center:Point, distance:Number = -1, initialAngle:Number = 0):void {
var step:Number = (2* Math.PI) / items.length;
var angle:Number = initialAngle;
for each (var d:DisplayObject in items) {
if (distance < 0) distance = d.height+10 / Math.sin(step);
d.x = center.x + distance * Math.cos(angle);
d.y = center.y + distance * Math.sin(angle);
angle += step;
}
}

Parameters are:

  • items: the items to be displayed a a circle
  • center: the center of the circle
  • distance: the distance from the center
  • initialAngle: the angle of the first item. Zero means that the item wil be aligned orizontally with the center, on his right.

Here’s an example: