15 March 2013

Random Array AS3 function

For my upcoming Memory variation game I needed a function to generate known length Array of random numbers out of given 0-n integers Array. For example when we call this function with this call:

randomArray(3, 10)

result is array with 3 random numbers from range 0-10, so it can be [0, 6, 3] or [9, 3, 2] or [0, 2, 1] but numbers cannot repeat. We can also call randomArray(n, n) in which case [0, 1, ..., n] is returned, but first function parameter must be less or equal to second parameter.

Here is how function might look like, comments are for testing purposes:

function randomArray(number:uint, total:uint):Array {

    var omega:Array = new Array();

    if (number <= total) {
        var alfa:Array = new Array();
        var pick:uint;
        var index:uint;
        for (var i:uint = 0; i<total; i++) {
        // trace("alfa = "+alfa);
        while (omega.length < number) {
            index = Math.floor(Math.random()*alfa.length);
            // trace("index = "+index);
            pick = alfa[index];
            // trace("pick = "+pick);
            // trace("omega = "+omega);
            // trace("alfa = "+alfa);
    return omega;

// function test
var om:Array = randomArray(5, 10);

If you are interested to read more about generating random stuff with ActionScript check these articles:
- Fastest Random Array Generator
- Simple Flash Snowflake Generator
- ActionScript Random Number Generator With Blur Effect
or type "random" in search box :)


11 March 2013

Reverse display list objects in AS3

Some simple tricks must be included to reverse display list objects in AS3 and that trick uses name MovieClip property, getChildByName and swapChildrenAt methods. Let's see code first.

import flash.display.MovieClip;
import flash.events.*;
import flash.events.MouseEvent;

var temp:MovieClip = new box();

for (var i:Number = 0; i < 50; i++) {
    temp.x = Math.random()*500;
    temp.y = Math.random()*350;
    temp.name = String(i);
    temp.alpha = i/50;
    temp = new box();

stage.addEventListener(MouseEvent.CLICK, onStageClick);
function onStageClick(evt:MouseEvent):void {
    for (var j:Number = 0; j < numChildren/2; j++) {
        swapChildrenAt(j, numChildren-j-1);
        getChildByName(String(j)).name = String(numChildren-j-1);
        getChildByName(String(numChildren-j-1)).name = String(j);
        getChildByName(String(j)).alpha = j/50;
        getChildByName(String(numChildren-j-1)).alpha = (numChildren-j-1)/50;        

For this example I will use MovieClip alpha property for visual representation of change after list reversing. First simple shape (sqaure in my case) has box class as linkage name.

Using for loop place on stage 50 of those boxes on random positions, but inside stage width and height dimensions. Using temp.name = String(i) line every box is named after its depth value, meaning first box added to stage will have name "0", second name "1", etc. Last one will have name "49".

Then, with temp.alpha = i/50 line we make every new box have slightly higher alpha value than previous one. We will reverse those alpha values later.

Function onStageClick will have single for loop, but with index going only half of number of boxes. In this way using swapChildrenAt we replace depth of box "0" with depth of box "49", depth of box "1" with depth of box "48", etc while entire list is reversed.

All that is left is to change names and alphas of new order using getChildByName method.

Image shows two different states, before and after the reverse.


08 March 2013

Learning Multimedia Development Online for Free

I just can't ignore some subjects here and have to touch them just a bit. One of those is for sure huge raise in MOOC in recents times (which stands for massive open online course).

There are many platforms right now that offer different types of online courses from free to paid ones, from beginners video tutorials to full-scale high class Universities courses.

I won't mention them all, instead I will only provide this link, which is place to find Top 50 Sites to Get Educated for Free.

I will however say that me personaly have used Coursera to learn Python, some Astrobiology and Sustainability :) and my experience is positive every step of the way. If you are in pursuit for knowledge Coursera is great, however if you need certifications, try some other place (for now, but it seems like that will change in the future). Coursera also won Best New Startup Of 2012.

Since I'm always hungry for more, my next project will be Diploma in Multimedia Development on Alison platform. It is free course where you can learn few multimedia tools like Adobe Photoshop, Windows Movie Maker, Adobe Flash, Audacity, Director MX and Adobe After Effects. I'm already familiar with some of those tools and I'm interested to see what new tricks can I learn.

Alison also offers free individual courses on PhotoShop, Flash, After Effects and Adobe Connect.



template by blogger templates