Ich just read the book OOP with Actionscript from Branden Hall and Samuel Wan. They explain OOP with AS in a simple and not to long way so that everyone can understand it. It also offers I summery of tricks from advanced flash developers. But that isn’t part of that thread. I came across the part about textfields and thought it would be handy if you could just use a textfield like a button e.g. myTextField_txt.onRelease = function … . So I extended the TextField class a little bit. It also returns a path to the recently created textfield.
MovieClip.prototype.createTextField2 = MovieClip.prototype.createTextField;
MovieClip.prototype.createTextField = function(name, depth, x, y, w, h){
this.createTextField2(name, depth, x, y, w, h);
return this[name];
}
TextField.prototype.$hitsMouse = function(){
return this._x<=_root._xmouse
&& this._x+this._width>=_root._xmouse
&& this._y<=_root._ymouse
&& this._y+this._height>=_root._ymouse
}
TextField.prototype.$onMouseMove = function(){
if(!this.$over && this.$hitsMouse()){
if(this.$onRollOver_func != undefined)
this.$onRollOver_func.apply(this, null);
this.$over = true;
} else if(this.$over && !this.$hitsMouse()){
if(this.$onRollOut_func != undefined)
this.$onRollOut_func.apply(this, null);
this.$over = false;
}
}
TextField.prototype.$onMouseUp = function(){
if(this.$onRelease_func != undefined
&& this.$hitsMouse()){
this.$onRelease_func.apply(this, null);
} else if(this.$onReleaseOutside_func != undefined
&& this.$pressed
&& !this.$hitsMouse()){
this.$onReleaseOutside_func.apply(this, null);
}
this.$pressed = false;
}
TextField.prototype.$onMouseDown = function(){
if(this.$onPress_func != undefined
&& this.$hitsMouse())
this.$onPress_func.apply(this, null);
this.$pressed = true;
}
// onPress
var onPressGetter = function(){
return this.onRelease;
}
var onPressSetter = function(func){
if(typeof func == “function“){
this.$onPress_func = func;
ASSetPropFlags(t,[”$onPress_func“],1,1);
if(this.onMouseDown == undefined)
this.onMouseDown = this.$onMouseDown;
}
}
// onRelease
var onReleaseGetter = function(){
return this.onRelease;
}
var onReleaseSetter = function(func){
if(typeof func == “function“){
this.$onRelease_func = func;
ASSetPropFlags(t,[”$onRelease_func“],1,1);
if(this.onMouseUp == undefined)
this.onMouseUp = this.$onMouseUp;
}
}
// onRollOver
var onRollOverGetter = function(){
return this.onRollOver;
}
var onRollOverSetter = function(func){
if(typeof func == “function“){
this.$onRollOver_func = func;
ASSetPropFlags(t,[”$onRollOver_func“],1,1);
if(this.$over == undefined){
this.$over = false;
ASSetPropFlags(t,[”$over“],1,1);
}
if(this.onMouseMove == undefined)
this.onMouseMove = this.$onMouseMove;
}
}
// onRollOut
var onRollOutGetter = function(){
return this.onRollOut;
}
var onRollOutSetter = function(func){
if(typeof func == “function“){
this.$onRollOut_func = func;
ASSetPropFlags(t,[”$onRollOut_func“],1,1);
if(this.$over == undefined){
this.$over = false;
ASSetPropFlags(t,[”$over“],1,1);
}
if(this.onMouseMove == undefined)
this.onMouseMove = this.$onMouseMove;
}
}
// onReleaseOutside
var onReleaseOutsideGetter = function(){
return this.onReleaseOutside;
}
var onReleaseOutsideSetter = function(func){
if(typeof func == “function“){
this.$onReleaseOutside_func = func;
ASSetPropFlags(t,[”$onReleaseOutside_func“],1,1);
if(this.onMouseUp == undefined)
this.onMouseUp = this.$onMouseUp;
if(this.onMouseDown == undefined)
this.onMouseDown = this.$onMouseDown;
}
}
TextField.prototype.addProperty(”onPress“, onPressGetter, onPressSetter);
TextField.prototype.addProperty(”onRelease“, onReleaseGetter, onReleaseSetter);
TextField.prototype.addProperty(”onReleaseOutside“, onReleaseOutsideGetter, onReleaseOutsideSetter);
TextField.prototype.addProperty(”onRollOver“, onRollOverGetter, onRollOverSetter);
TextField.prototype.addProperty(”onRollOut“, onRollOutGetter, onRollOutSetter);
ASSetPropFlags(TextField.prototype,[”$onMouseMove“,”$onMouseUp“, “$onMouseDown“],1,1);
// example
var txt = this.createTextField(”myTextField_txt“, 1, 10, 10, 1, 1);
Mouse.addListener(txt);
txt.autoSize = “left“;
txt.selectable = false;
txt.text = “A simple textfield that acts like a button!“;
txt.onPress = function(){
trace(”onPress“);
}
txt.onRelease = function(){
trace(”onRelease“)
}
txt.onReleaseOutside = function(){
trace(”onReleaseOutside“);
}
txt.onRollOver = function(){
trace(”onRollOver“);
this.background = true;
this.backgroundColor = 0×000000;
this.textColor = 0xFFFFFF;
}
txt.onRollOut = function(){
trace(”onRollOut“);
this.background = false;
this.textColor = 0×000000;
}
