Programa AS3 sin morir en el intento

Anda te invito a echarle un ojo a esto, yo sé que comenzar con un lenguaje puede ser frustrante y hasta se pierde uno que otro ligue por eso.

Comienza desde cero pero comienza bien, aquí habrá un poco de ayuda, después podrás saltar a sitios más especializados.

Búsqueda en éste blog solamente

Seguidores y Fans

lunes, 29 de septiembre de 2008

Global Variables Over AS3 (The Definitive Code and Solution)

Hello There. You may read all this or pass to the end of this entrance.

Years have been passed before I was in close cercano with flash4, passing across AS1, AS2, reaching AS3 the past year (2008).

This new languaje (AS3) comes harder, faster, stronger than the last ones, but I still to short for many things, like mathematics. The hell how I would learn such things at high levels.

Anyway, there is a little thing I can give you. How to make globals in ActionScript3 and not die on the try.

A question: it is possible? Yes! its little different from the past and little the same from the past...

Can I use this "globals" in my Class objects? Yes!, in this try Ill give you 2 options but thad is possible!.

Im kidding you? Nope. I spent my hours in this and wasnt kidding.

All those who worked with flash 5 may understand this easy and those who know that all objects have something to do with the super duper class Object have the solution in hands!.

Sorry about my english, is too bad and I dont care at all so Im switching to spanglis but you keep on:

Échenle un ojo a esto:
var myObject=new Object()
... the Object class have something called "prototype". The prototype is... at the end the soul of every object. So the class Object have a soul. And the instance myObject have something to do with such soul. My good im not even religious... Anyway.

Lets thing on a method. Methods are something to do with the super duper class Function. But the Function class have something to do with...? the Object class. Because the Object class is the Adan and Eva of the human specie (doesnt arent the monkies? dont know).

// in your code
var myObject=new Object()
function method(){
trace("myObject: "+myObject)
}
method()
// end of code
Now, this is the very basic of a method call and scoping. "myObject" will be accessible by the method "method".

Now, suppose you want to acces myObject from any where at any time in any place. Remember the prototype? the soul of all? well, prototype is the referer of the superior properties and methods. Being the Adan&Eva of all, every child is conected to it. Every instance have something of this Object.

You can access prototype doing this:

Object.prototype

And you can print new properties and functions to the prototype with...? you know

// code
Object.prototype.myObject=String("Hola cabrones!") //Wich being String something of Object, Im sure this would cause some catastrophyc recursion.

// endofcode
// code, somewhere else before you declared "myObject"
trace("Sirve: "+this.myObject) // Sirve: Hola cabrones!
// endofcode

Now. 2 differences between

trace("Sirve: "+this.myObject)
and
trace("Sirve: "+myObject)

The seccond option will tell a undeclared variable error. In fact, you should not try to declare nothing global, I didnt pass this point yet... but "this" will call the prototype and return the value.

In this lab job, there is a thing, every new Object instance or subclass will copy the old values of Object but will adquire independence with the adquired values. May be my recomedation is to not to declare properties but methods that may re-read the new values.

// code
Object.prototype.myObject="Hola cabrones!"


trace("Sirve: "+this.myObject) // Sirve: Hola cabrones!

var myObjectInstance=new Object()

myObjectInstance.myObject="puta madre!"

trace("Sirve: "+this.myObject) // Sirve: Hola cabrones!
trace("No Sirve: "+myObjectInstance.myObject) // Sirve: puta madre!

// endofcode
So we can see differences wich we dont want.

but... Fuckind did it, I fucking did it right away! stop crying ladies and clean you tears, daddy is here:


In your very first frame (or if your swf is instance of some class, in the class constructor)

// ******* SubstanceMX *******
// CREATED BY SubstanceMX FOR THE WORLD, SEPT 2008.
// MADE IN MEKSYK
// THIS CODE IS FOR PUBLIC USE AND IF YOU HAVE IT,
// ALSO SHOULD SHARE WITH EVERY ONE WHO ASK! ALL
// IMPROVEMENTS YOU DO, LET ME KNOW AND MAKE THEM PUBLIC!
//
// globals initializer
Object.prototype.getGlobal=function (g){return Object.prototype[g]}
Object.prototype.setGlobal=function (g,v){Object.prototype[g]=v}
//
// ++++++++
// ********
// somewhere else
trace(this.getGlobal("gMyName")) // undefined
this.setGlobal("gMyName", "SubstanceMX") // void
trace(this.getGlobal("gMyName"))// SubstanceMX
this.setGlobal("gMyFun", function (){trace("Hola cabrones!")}) // void
this.getGlobal("gMyFun")() // Hola cabrones!"
//