View Full Version : altering textfields
inquizard
06-12-2008, 12:47 AM
Hi,
i thought this was going to be easy but turns out as usual it's the simple things that get me...
i have a class which creates a plain button with a textfield attached. it was going to be the basic button for my site, which could then have it's name altered from another class.
i got this as my plain button:
plainBtn = new subMenu();
addChild(plainBtn);
plainBtn.addEventListener(MouseEvent.MOUSE_OVER, onrollOver);
plainBtn.addEventListener(MouseEvent.MOUSE_OUT, onrollout);
myFormat = new TextFormat;
myFormat.font = "Cordia New";
myFormat.color = 0x990000;
myFormat.size = 20;
myText = new TextField();
addChild(myText);
myText.x = 30;
myText.y = -15;
myText.text = "HELLO";
myText.autoSize = TextFieldAutoSize.LEFT;
myText.setTextFormat(myFormat);
i then created a sample button in another class (which imports the plain button class) and tried "sample1.myText.text = "SAMPLE_1";" to try and change the textfield value but didnt work.
anyone know where i'm going wrong?
bloodstyle
06-12-2008, 07:32 AM
Did you create getters/setters or make the variable public? If not, I think that may be what's causing it not to work:P
inquizard
06-12-2008, 09:43 AM
Did you create getters/setters or make the variable public? If not, I think that may be what's causing it not to work:P
Hi, thanks for the reply. didnt know about getters/setters so had a quick look and they seem interesting but would like to avoid them really.
it was late last night when i was looking at this and did think about the var status. it was private, so when i changed it to public i thought the text just disappeared. checked again today and it hasnt disappeared, it justed changed format and i couldnt see it.
the format for the text is set in the plain button class, so why would it change format just because i changed the value? would i have to reset the format in the main class as well or is there a way i could keep the same format as difined in the plain button class?
bloodstyle
06-12-2008, 11:06 AM
I think you'll have to use defaultTextFormat instead of setTextFormat for it to work when you change the text, or call setTextFormat again. Also, though it's not my call of course, using getters/setters will save you a lot of pain in the ass later on, if you plan on making this stuff reusable/extendable :P
inquizard
06-12-2008, 02:36 PM
Hi,
gave me plenty to think about :)
2 things i cant figure out - i have the file working as i want it to. i did this by just taking the formatting script out of the plain button class and putting it in the other class. each of the buttons now just "setTextFormat" when they are created, but your suggestion about default text format sounds promising. (anything which cuts the amount of scripting used is fine by me)
i assumed it did as it suggested, so changed the text formatting script back to the plain button class and changed the set text script to default text script. The myText var in the plain button class is public so the other class shouldnt have any problems accessing it, but i now get this:
1178: Attempted access of inaccessible property myText through a reference with static type plainTxtBtn. sample1.myText.text = "SAMPLE_1";
i've no idea what it's telling me?
secondly, the getter/setter stuff - do you know any links i could read up on it? all the stuff i've found suggests getter/setter is more common in as2 and as3 doesnt really need it as you can simple change the status from private to public. i could decide for myself if i knew more about what they do...?
thanks
bloodstyle
06-12-2008, 04:47 PM
To be honest, I have no idea why you're getting that error - out from the code you've posted everything seems fine. I even tested it myself, and I don't get any errors:
package {
import flash.display.*;
import flash.events.*;
import flash.text.*;
public class plainBtn extends MovieClip {
public var myText:TextField;
private var myFormat:TextFormat;
private var btn:subMenu;
public function plainBtn() {
btn = new subMenu();
addChild(btn);
btn.addEventListener(MouseEvent.MOUSE_OVER, onrollOver);
btn.addEventListener(MouseEvent.MOUSE_OUT, onrollout);
myFormat = new TextFormat;
myFormat.font = "Cordia New";
myFormat.color = 0x990000;
myFormat.size = 20;
myText = new TextField();
addChild(myText);
myText.x = 30;
myText.y = 15;
myText.text = "HELLO";
myText.autoSize = TextFieldAutoSize.LEFT;
myText.defaultTextFormat = myFormat;
}
private function onrollOver(e:MouseEvent) {
}
private function onrollout(e:MouseEvent) {
}
}
}
and
var b:plainBtn = new plainBtn();
addChild(b);
b.myText.text = "hi";
on the timeline. Maybe I can help if you post more code(and maybe not ! :P)
As for getters&setters, Mazoonist wrote a tutorial which I enjoyed, it think it's on one of the last pages where he writes about them: get/set (http://www.actionscript.org/resources/articles/708/5/Another-Useful-Reusable-AS3-Class-Expander/Page5.html)
inquizard
06-12-2008, 05:05 PM
thanks for your help bloodstyle, bit strange how mine's got the error...
cant see anything i missed mentioning but i have previous on this type of thing so thought it would be better to post the files!
bloodstyle
06-12-2008, 05:31 PM
3 things:
You made the wrong variable public, myText is the one you want public and not myFormat :P
Also, I think you'll have to specify your plainBtn: private var plainBtn:subMenu;
And, syntax for defaultTextFormat is like this: myText.defaultTextFormat = myFormat;
Now it should work!
inquizard
06-12-2008, 06:04 PM
genius!
would never of found those on my own anytime this month... now onwards to the next issue i'm bound to create for myself
thanks for your help bloodstyle :)
|
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.