Forum Topic: Change font via button.

(155 views • 12 replies)

This topic is 1 page long.

<< < > >>
None

Tivaelydoc

Reply To Post Reply & Quote

Posted at: 9/10/08 03:30 PM

Tivaelydoc EVIL LEVEL 21

Sign-Up: 12/03/04

Posts: 468

I have a couple buttons that generate text but I want each of them to use a different font. This is what I tried:

SWF:

http://tivaelydoc.110mb.com/fontchanger.
swf

FLA:

http://tivaelydoc.110mb.com/fontchanger.
fla

AS:

On the frame:

onEnterFrame = function () {
	var Format:TextFormat = new TextFormat();
	Format.font = (_root.box.getValue());
	YourTextBox.setTextFormat(Format);
};
stop();
a = ["Bob", "Jim", "Dave"];
b = ["went", "ran", "danced"];
c = ["at the fair.", "out in the patio.", "next door."];

Button:

on (release)
{
	var box = new Array("Arial");
	var sentence = new Array(a[random(a.length)], b[random(b.length)], c[random(c.length)]);
	result = "";
	var i = 0;
	while (i < sentence.length)
	{
		result = result + (sentence[i] + " ");
		++i;
	} 
}

None

andy70707

Reply To Post Reply & Quote

Posted at: 9/10/08 03:38 PM

andy70707 LIGHT LEVEL 22

Sign-Up: 09/30/07

Posts: 2,349

Well, the way I do it is like this, using the ComboBox component and this bit of code:

onEnterFrame = function () {
var Format:TextFormat = new TextFormat();
Format.font = (_root.box.getValue());
YourTextBox.setTextFormat(Format);
}

Then, just make a combo box with the var "box", and in the labels field in parameters, type in the fonts you want, as they are written. If you want, I can send you a .fla with it in. I already spend 2 and a half hours manually typing in the fonts, so you can use that if you want.

add me on xbox 360: CoD4 and CoD5 (and sometimes TF2) gamertag: andy70707

BBS Signature

None

Tivaelydoc

Reply To Post Reply & Quote

Posted at: 9/10/08 03:49 PM

Tivaelydoc EVIL LEVEL 21

Sign-Up: 12/03/04

Posts: 468

If you don't mind, I would appreciate that. I've never heard of a combobox component.


None

Tivaelydoc

Reply To Post Reply & Quote

Posted at: 9/10/08 03:58 PM

Tivaelydoc EVIL LEVEL 21

Sign-Up: 12/03/04

Posts: 468


None

liaaaam

Reply To Post Reply & Quote

Posted at: 9/10/08 04:37 PM

liaaaam NEUTRAL LEVEL 22

Sign-Up: 12/11/04

Posts: 14,506

I have no idea what I'm missing, and I haven't used Flash in months so I might be missing something, but this is really confunded me xD It doesn't seem to work, when I know it should :P Anyway.. this is what I got to (I rewrote a lot of the code because your style was very confusing).

link


None

Ben-Fox

Reply To Post Reply & Quote

Posted at: 9/10/08 05:18 PM

Ben-Fox LIGHT LEVEL 07

Sign-Up: 12/27/03

Posts: 571

Try using setNewTextFormat instead of setTextFormat and see if that helps.


None

Tivaelydoc

Reply To Post Reply & Quote

Posted at: 9/10/08 05:37 PM

Tivaelydoc EVIL LEVEL 21

Sign-Up: 12/03/04

Posts: 468

I changed it, but it still didn't work.

So, the only way to do this is by switching fonts via combobox?

Because what I'm trying to do is when you press the button, it changes the dynamic text box font.


None

liaaaam

Reply To Post Reply & Quote

Posted at: 9/10/08 06:05 PM

liaaaam NEUTRAL LEVEL 22

Sign-Up: 12/11/04

Posts: 14,506

At 9/10/08 05:18 PM, Ben-Fox wrote: Try using setNewTextFormat instead of setTextFormat and see if that helps.

LOL

Yeah thats the thing I was missing. It works now >.>

@ topic creator, no, but it's prettier. Similar code, just adapt the code I used to the buttons (or just ignore mine if you wanna keep your bad code xD) and basically...:

var tf:TextFormat = new TextFormat();
	tf.font = f;
	_root.YourTextBox.setNewTextFormat(tf);
	_root.YourTextBox.text = s;

None

Tivaelydoc

Reply To Post Reply & Quote

Posted at: 9/10/08 06:21 PM

Tivaelydoc EVIL LEVEL 21

Sign-Up: 12/03/04

Posts: 468

The only reason I didn't want to do it that way is because I have 10 different buttons that generate a different sentence amount. The first 2 only need 3 different variables, but the rest generate anywhere up to 12 different variables. Pretty much, I need different AS for each button.

So, on each button, I would put the same code as on the combobox, but change "on (change)" to "on (release)" and obviously the setNewTextFormat.

But it still doesn't work.


None

liaaaam

Reply To Post Reply & Quote

Posted at: 9/10/08 06:24 PM

liaaaam NEUTRAL LEVEL 22

Sign-Up: 12/11/04

Posts: 14,506

At 9/10/08 06:21 PM, Tivaelydoc wrote: But it still doesn't work.

Then you're doing it wrong, damn you.

>: [

Oh.. maybe you've not changed the code from checking the combo box value to a seperate variable.. try figure that one on your own, if you can't do that, give up.


None

deadlock32

Reply To Post Reply & Quote

Posted at: 9/10/08 06:38 PM

deadlock32 NEUTRAL LEVEL 18

Sign-Up: 04/28/01

Posts: 2,502

when ever you are going to change a font (or any thing in a textFormat) you need to set the format then the text.

var someTextField:TextField
//////////

changeFontOfText(someTextField, "verdana");
someTextField.text = "some text";

// // // // or if you want the same text
// someTextField.text = someTextfield.text;

function changeFontOfTextField(targetTextField:TextField, someFontString:String):void
{
    var tempFormat:TextFormat = targetTextField.getTextFormat();
    tempFormat.font = someFontString;
    targetTextField.setTextFormat(tempFormat);
}

AS3 textfields are a bit wonky,


None

Tivaelydoc

Reply To Post Reply & Quote

Posted at: 9/10/08 06:41 PM

Tivaelydoc EVIL LEVEL 21

Sign-Up: 12/03/04

Posts: 468

Well, I got it to change fonts, but now the text doesn't show up.

I changed:

var font:String = this.selectedItem.label;

to:

var font:String = "Arial";


None

Ben-Fox

Reply To Post Reply & Quote

Posted at: 9/10/08 09:56 PM

Ben-Fox LIGHT LEVEL 07

Sign-Up: 12/27/03

Posts: 571

At 9/10/08 06:21 PM, Tivaelydoc wrote: The only reason I didn't want to do it that way is because I have 10 different buttons that generate a different sentence amount. The first 2 only need 3 different variables, but the rest generate anywhere up to 12 different variables. Pretty much, I need different AS for each button.

Actually, no you don't. Check this out, you're gonna love it:

function flexibleGenerator(paramObject:Object):String {
  //Never underestimate the power of for...in...
  //To make this work, pass an object with references to as many arrays as you need.
  var returnString:String = "";
  for (var arrayRef in paramObject) {
    returnString += paramObject[arrayRef][Math.floor(Math.random() * paramObject[arrayRef].length)] + " ";
  }
  return returnString;
}

If your arrays are called firstArray, secondArray, thirdArray, etc, you would call it as such:

var s:String = flexibleGenerator({ref1: firstArray, ref2: secondArray, ref3: thirdArray})

And it doesn't matter how many array references you cram in the object, the for...in loop will iterate through them all and choose a random index from each one. This reduces your AS writing to effectively 2 or 3 lines of code; call flexibleGenerator with the appropriate array references, set your text field's text to equal the string returned, and you're done. It doesn't solve the issue you're having with fonts, but at least it will ease the rest of the process.


All times are Eastern Standard Time (GMT -5) | Current Time: 05:22 AM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!