Tech Blog
from
Full TechBlog List
<< previous entries
Search:
next entries >>

AIDS (1)  |  BIOS (1)  |  drive letter (1)  |  drivers (1)  |  Flash (19)  |  Google Maps (1)  |  hard drive (5)  |  hardware (3)  |  IDE/PATA (0)  |  Javascript (3)  |  memory (1)  |  motherboard (3)  |  MySQL (1)  |  networking (1)  |  optimization (6)  |  PHP (2)  |  RAM (1)  |  ribanc (4)  |  SATA (1)  |  SEO (0)  |  software (2)  |  software installation (1)  |  spyware (1)  |  trojan (1)  |  virus (1)  |  webprogramming (23)  |  Windows Vista (1)  |  Windows XP (6)  |  XP Antivirus (1)

Balázs
Flash, Java and related technologies
 
Flash Cookbook - Appendix - Global functions
2010.08.03.

Previously we wrote about global variables.

Going further, sometimes it's really useful to reach a certain function from a child of the child of the grandchild's child. Especially if we want to reach different functions from different objects. Imagine you have a url request in a certain object, and a sharedobject request in another, but somehow you need to reach both in a completely innocent small object somewhere in the depth of the jungle of the objects.

Therefore we add a new static variable to our global class, let's say we want to have a global message function:

public static var showMessage:Function;

Then at the initialization time, we assign the specific object's function to this variable:

Vars.showMessage = this.showMessage;

Done. Now, if this is done, you can reach this message function from anywhere (after importing your global class).

For understanding the practical reason to do this, let's see this message function. If we would like to show a message from an object and our gui handler is on the top of all objects, then we should dispatch a bubbling event, catch this event on the top and then get a messagecode from the invoker object to recognize the message. Event dispatching - listening - reading a property. Instead, we directly call the function with a messagecode.

Not even talking about an interesting phenomenon: if our class is extending a MovieClip, dispatching an event only works if the object is added to a displayobject. Horrible.

Now comes the question: why on earth should we use events? Good question, Young Padawan. Good question. Consider that firing and catching an event is practically the same as written above: you define an event and pair a function to this event. Then, whenever the event occurs, the function is called. In Flash mostly an event's target is the object itself, so you can reach it's properties.

But when you want to "cross-link" objects and share the same function among different objects, the method above might be useful.

Brilliant.

(reference tag: singleton)


Balázs
Flash, Java and related technologies
 
Flash Cookbook - Appendix - Global variables
2010.08.03.

Sometimes it's useful to use global variables. In AS3 it's quite easy: you create a class and add static variables to it.

 

package
{
    public class Vars
    {
        public static var value:int = 0;
    }
}

 

To reach it from anywhere import the class (import Vars;) if needed and done.

trace(Vars.value);
Vars.value= 10;
trace(Vars.value);

Output:

0

10

(reference tag: singleton)


Balázs
Flash, Java and related technologies
 
Flash Cookbook - Appendix - Throwing errors
2010.03.25.

Why is it good for me? - asked the young padawan (YP).

See the reason of the problem, you will - answered the wise master (WM).

WM: Count 16 errors, we can. Easy to use, it is. Example, here is:

 

var error:Error = new Error('This was an error, my young padawan.');

and to call for this you do:

throw error;

YP: but what does this do, master?

WM: pops up a window with an errormessage, this.

YP: I see.


Balázs
Flash, Java and related technologies
 
Flash Cookbook - Appendix - Number types used for loops
2010.02.01.

In this section we will examine the speed of loops with the three number types.

The code is simple:

var a:Number = 0;
var i:int = 0;

for(i = 0; i < 10000000; i++) a = 1;

we just set a number to be 1. Okay, let's run the test above in three cases:

i is an int, uint and Number.

The results:

int 78 ms

uint 94 ms

Number 140 ms

Amazing.


Balázs
Flash, Java and related technologies
 
Flash Cookbook - Fine tuning Point class Part 3. Angle.
2010.01.29.

"Dear Master,

if I can create a Point using the Point.polar() method, why on earth can't I reuqest the angle of a Point? Please answer me, you are my only hope.

The Little Grasshopper"

Good question, I could have thought about it myselft. I guess the actual answer is that a Point is not a vector therefore there is no need to calculate any angle as there is no angle for one point. But, come on, why not to think about a point as a vector originated from (0, 0)? We are using a graphical program in the end, so a point is a vector.

public function get angle():Number
{
     var angle:Number = x >= 0 ? Math.atan(y / x) : Math.atan(y / x) - Math.PI;
     if(x == 0 && y == 0) return 0;

     return (angle + PI_2) % PI_2;
}

public function set angle(angle:Number):void
{
     var length:Number = Math.sqrt(x * x + y * y);
           
     x = Math.cos(angle) * length;
     y = Math.sin(angle) * length;
}

AND! It is also good to know in which quarter (of the unit-radius circle) is our point. Also to set this.

public function get quarter():int
{
     if(angle >= 0 && angle < PI_1_2) return 1;
     if(angle >= PI_1_2 && angle < PI) return 2;
     if(angle >= PI && angle < PI + PI_1_2) return 3;
     if(angle >= PI + PI_1_2 && angle < PI_2) return 4;

     return 0;
}
       
public function set quarter(newquarter:int):void
{
     angle = angle + (newquarter - quarter) * PI_1_2;
}


Balázs
Flash, Java and related technologies
 
Flash Cookbook - Fine tuning Point class Part 2. Length.
2010.01.29.

Okay. Here we are with the second question. Can we fasten the request for length? - asked the little grasshopper.

Yes, we can. The result is a 15% speedup.

override public function get length():Number
{
     return Math.sqrt(x * x + y * y);
}

We have to mark the getter for override. That is all. By the way, try not to use the built-in Math.pow() for powering if you know the exponent.

"Dear Master,

Why is length a read-only property? I wanna set the length. Please make it possible for me.

Thank you for you time

The Little Grasshopper"

We got this letter earlier and wan't to respond to it. We don't want to have suffering flash coders around our household.

And for your information here is the trick:

public function set length(length:Number):void
{
     var angle:Number = x >= 0 ? Math.atan(y / x) : Math.atan(y / x) - Math.PI;

     if(x == 0 && y == 0) angle = 0;

     x = Math.cos(angle) * length;
     y = Math.sin(angle) * length;
}

First we get the angle of the point:

var angle:Number = x >= 0 ? Math.atan(y / x) : Math.atan(y / x) - Math.PI;

and keeping this angle we set the new coordinates:

x = Math.cos(angle) * length;
y = Math.sin(angle) * length;

for more information see the definition of sine and cosine.

Isn't this awesome? We expanded the Point class!

Of course, as it is written in Little Grasshopper's second letter (1), requesting the length is a dynamic metho, meaning that it calculates the length instead of using a constant.

But. Since we don't want to make a setter for the coordinates (optimizing issues - just think about it), we can't catch the moment of chaning such a value. Therefore we can't use a constant for the lenght (what otherwise could be an optimising step).

It has to be considered when to use it.


Balázs
Flash, Java and related technologies
 
Flash Cookbook - Fine tuning Point class Part 1. Distance.
2010.01.29.

Point class is not a final class therefore we can extend it. To do this is quite useful because the main functions of points are in the package so you don't have to rewrite them. But still there are some extra things what you can override to get better results concerning speed.

Let's see a simple test with the distance method. The distance method is a static type which calculates the distance between to give points. Quite easy and simple to use:

var distance:Number = Point.distance(p1, p2);

This is very useful in several calculations, we won't say anything more because it's that fundamental.

But. Here comes the interesting part. I use the method and my program is really clean, but what do I get for my efforts? Somehow these built in packages run slowly if we run our optimum-searching code. Try it for yourself: on my machine it needed around 117 ms to run a 100.000 times. BUT THIS IS AWESOME!!! - could the little grasshopper say. I don't need to know maths and this function is superfast!

What do WE say? : HA-HA (slow motion).

Since we already have the skeleton of the extended Point class (remember to call super() with default x and y coordinates in the constructor) we override the SLOW distance method.

public static function distance(p1:flash.geom.Point, p2:flash.geom.Point):Number
{
     return Math.sqrt((p2.x - p1.x) * (p2.x - p1.x) + (p2.y - p1.y) * (p2.y - p1.y));
}

There you go. And, my little padawan, yes. We came to see where the rabbit hole goes. Actually, we are in the rabbit hole. The result of the same test concerning the speed is: 41 ms. Let me say it again FORTYONE milliseconds.

Let's stop for a minute.

Little Grasshoper: Is this really 3 times faster than the built-in code?

We: Obviously.

We don't say that the errorchecking is the same for our own function and the built-in one. We don't check the type of object passed to the function and blabla, but that is so expensive! You have to be careful as there is no errorchecking but this is still really profitable.

One more thing to know. We have the extended Point class now, let's say the package is org.geometry.

How do you make a difference between this new one and the original class? Here is the trick:

var p1:flash.geom.Point = new flash.geom.Point(10, 10);
var p2:org.geometry.Point = new org.geometry.Point(10, 10);

Note that the classes are prefixed with their packages.

And this is our first step to optimize the Point class.

The code:

package org.geometry
{
    import flash.geom.Point;
   
    public class Point extends flash.geom.Point
    {
        public function Point(x:Number = 0, y:Number = 0)
        {
            super(x, y);
        }
       
        public static function distance(p1:flash.geom.Point, p2:flash.geom.Point):Number
        {
            return Math.sqrt((p2.x - p1.x) * (p2.x - p1.x) + (p2.y - p1.y) * (p2.y - p1.y));
        }
    }
}


Balázs
Flash, Java and related technologies
 
Flash Cookbook - Appendix - SharedObject with expiration
2010.01.11.

So to say SharedObject is flash's cookie. To reach it simply do:

var _so:SharedObject = SharedObject.getLocal(name);

to reach key-value pairs use the data property

_so.data[key] for both read and write.

SharedObject stores an object, so we can use it for e.g. setting expiration times. To get the actual time use the Date classes getTime() method:

var time:Number = (new Date).getTime();

Knowing this we can easily put it together:

_so.data[key] = {value: value, time: time, expiry: 60000};

checking if our record has expired (in expiry: 60000 millisec = 60 sec = 1 minute):

var temp:Object = _so.data[key];

var expired:Boolean = temp.expiry == '-1' ? false : (((new Date()).getTime() - temp.time) > temp.expiry ? true : false);

Default value for expiry is -1 when it's not set for expiry.

There you go, fast and clean and useful for local operations.


Balázs
Flash, Java and related technologies
 
Flash Cookbook - Part I. Verse 4. - brightness of movieclip
2009.12.02.

Now we learn how to set the brightness of a movieclip. It's quite easy and it will be our first step into colormatrixfilter.

function setBrightness(target:MovieClip, value:Number = 0):void
{
    var element:Number = value;
    var matrix:Array = [1, 0, 0, 0, element,
            0, 1, 0, 0, element,
            0, 0, 1, 0, element,
            0, 0, 0, 1, 0];
    var colorfilter:ColorMatrixFilter = new ColorMatrixFilter(matrix);

    target.filters = [colorfilter];
}

The colormatrix filter is constructed with a matrix. The value for brightness can go from -255 to 255.


Balázs
Flash, Java and related technologies
 
Flash Cookbook - Appendix - Additive inverse if origo is not zero
2009.11.19.

The additive inverse is the number, what completes another one to give the additive result zero.

The additive inverse of 5 is -5, as 5 + (-5)  = 0.

From another point of view these numbers are symmetrical.

Right. But what if our origo is not zero itself? Let's say, I have 5 as the origo and wanna get the symmetrical number of 7. That is 3. Or I have -90 as the origo and wanna get the symmetrical pair of -87. That is -93.

For this we do the easy math of:

inverse = 2 * origo - number

Check our equation:

2 * 0 - 5 = 0 - 5 = -5, true

2 * 5 - 7 = 10 - 7 = 3, true

2 * (-90) - (-87) = -180 + 87 = -93, true.


Home