I made a simple puzzle game in ROBLOX and I've decided to re-make it in AS3.
I've created a grid of buttons with click events, and now I need to change their color when clicked. I've currently got this:
trace("Button clicked:", event.currentTarget.id);
event.currentTarget.graphics.beginFill(0xA00000)
event.currentTarget.graphics.endFill()
the trace prints fine but the color doesn't change (causes no errors either)
The buttons are movie clips with labels inside.
beginFill and endFill are used to control the filled color of drawing commands like drawRect, they don't just change the fill of any arbitrary display object.
To change the color of a display object you can use ColorTransform:
var colorTransform:ColorTransform = new ColorTransform();
colorTransform.color = 0xA00000;
DisplayObject(event.currentTarget).transform.colorTransform = colorTransform;
Related
I've read some tutorials and documentation on scene2d's UI capabilities, but I can't quite figure out how the skins work. I simply want a TextButton that changes color when pressed. I managed to get the background of the TextButton to change, but that's not what I wanted. It should have no background.
I would be very grateful if anyone could provide an example of how this could be done. It seems very simple, so I think I'm missing something obvious here. If a skin is involved, please write it programmatically.
Thank you.
Turns out it was as simple as I thought, it just didn't work when I tried it the first time.
When defining the TextButtonStyle you can assign downFontColor the color you want your text to be while it is being pressed. You can also assign checkedFontColor the color you want the text toggle between when pressed.
Example:
TextButtonStyle textButtonStyle = new TextButtonStyle();
textButtonStyle.font = someBitmapFont;
textButtonStyle.fontColor = Color.WHITE;
textButtonStyle.downFontColor = Color.BLACK;
//Optional color to toggle between when pressed
textButtonStyle.checkedFontColor = Color.GREEN;
final TextButton textButton = new TextButton("Text", textButtonStyle);
This will produce a white TextButton that turns black when it is pressed. When the mouse/touch is released it'll either turn green or white, depending on the state of the toggle.
You can also tint it, but the effect depends on the base color (if it is white, then it works perfectly, else it depends). Anyway, it's done like this:
button.getLabel().setColor(myColor);
I`m trying to build a simple game where you will view a map with 5 clickable objects.
When you click these objects you can navigate trough some pages and upon clicking the last button it returns to the map itself. Now everything is working as it should. Upon clicking this specific last button It has the following code.
jamenext2.addEventListener(MouseEvent.MOUSE_DOWN, gotomap2);
function gotomap2(event:MouseEvent):void {
gotoAndPlay("map2");
}
What I would like to have now is to have a bar on the Map itself, and when you have reached the button that has the above code attached, the bar increments by 20%, and so the same for the 4 other clickable buttons on the map. Upon 100% completion I would like it to play the next animation further on in the timeline.
I have tried the following, but I am not that good in writing code, and I`m not very sure where to place it exactly.
var total1 = 100;
_root.loaded1 = 0;
while(true) {
this.scaleX = _root.loaded1/total1;
}
_root.loaded1 = _root.loaded1 + 20;
gotoAndPlay("map2");
I have no idea on how to do this.
What your going to want to do is create a square or bitmap class for the progress bar. From there, create a square with a MouseEvent.MOUSE_DOWN for the buttons.
There is different ways of doing this. Judging from your sample code your using the timeline for scenes so you might want to put a button in there to scale in each scene.
You could size it at twenty percent and make it fully transparent and become viable on the first click. Then increment by twenty percent until a hundred percent visible. You could then also have another button that would roll back a page.
Mostly pseudo code but something like this:
(Example Reference link: http://www.actionscript.org/resources/articles/792/1/Drawing-Shapes-with-AS3/Page1.html )
import flash.display.Shape;
private var square:Shape;
main_method
{
square = new Shape();
//draw shape
square.graphics.beginFill(0x1111FF, 1);
square.graphics.drawRect(30, 30, 200, 200);
square.graphics.endFill();
square.alpha = .5;
addChild(square);
}
//somewhere in a MouseEvent.MOUSE_DOWN class
{
gotoAndPlay("map1");
}
}
It's also possible rather than creating a square in code to create a symbol instance name on the stage and and in the properties tab give it a name and reference it in code.
I'm trying to create a TextField which will look smooth enough when animated. The only option, when I can get smoothly-animated textfield, is setting antiAliasType property to AntiAliasType.NORMAL; But I'd like to play with text antialiasing to make letters a little bit thicker. So I change the antiAliasType to AntiAliasType.ADVANCED.
After that the textfield looks jumpy when animated (it looks like glyphs are being snapped to pixel-grid). Change of property gridFitType of the textField doesn't make any sense.
Has anyone achieved smooth text-field animation when antiAliasType is switched to AntiAliasType.ADVANCED? (The jumpy text occurs when switching to TLF text fields either)
Here is my short code:
var p:TextField = new TextField();
var font:Font = new Font1XXX();
// font is embedded int the library and exported as Font1XXX class
var tfor:TextFormat = new TextFormat();
tfor.font = font.fontName;
tfor.size = 15;
tfor.color = 0xFFFFFF;
p.defaultTextFormat = tfor;
p.autoSize = TextFieldAutoSize.LEFT;
p.antiAliasType = AntiAliasType.ADVANCED;
p.gridFitType = GridFitType.NONE;
// change to GridFitType.NONE does not make any sense;
p.selectable = false;
p.embedFonts = true;
p.text = "HELLO WORLD";
addChild(p);
If you don't need selectable text, draw the textfield into a bitmap and add the bitmap to the stage instead of the textfield.
Every text in flash is snapped to whole pixels, resulting in any animation in regards to them will feel "jagged".
Workarounds are to convert any "static text" into a shape and place inside movieclip or to "copy" the text into a bitmap and animate that bitmap. Depending on what type of animation you want to do, different solutions are required for best visual experience.
For instance, scrolling text, it would probably be best to cache it as bitmap and then move it along the axis.
Scaling up/down a text (if it is scaled a lot). This sucks, I haven't found a good way to resolve this without visual artifacts. Best solution is usually to copy the text into another clip, hide the text and then animate quickly or try to cover it with other effects so focus is taken away from the text itself.
How can I resize the radio button component that comes with Flash CS5.5?
The default look is quite small. The same applies to the radio button label which is also quite small.
Thanks in advance
You can find the graphical assets in the library. The folder will be "Component Assets" -> "RadioButtonSkins". Take note that there are many movie clips that comprise the various states of the radio button
As for the text/label portion of the radio button, you have to do that through code. Here is an example function you could have:
function styleRadioButton(rb:RadioButton, myTextFormat:TextFormat):void {
rb.setStyle("embedFonts", true); //if you want to use an embedded font
rb.setStyle("textFormat", myTextFormat); //set the text format
rb.setStyle("antiAliasType", AntiAliasType.ADVANCED); //if you want smoother looking fonts
rb.textField.autoSize = TextFieldAutoSize.LEFT; //if you want the label to automatically grow
}
So if you have an instance of a checkbox called myRadio, you could do this:
var textFormat:TextFormat = new TextFormat("Arial", 24, 0xFF0000); //make a red, 24px Arial text format
styleCheckBox(myRadio,textFormat);
Now, one easier thing you could also do is just place your radio button inside of a MovieClip or Sprite container and scale said container. To do this:
1 - place an instance of a radio button on the stage
2 - with the newly made instance selected, press F8 (or Modify -> Convert To Symbol)
3 - Scale the newly made movie clip
I have defined a gradient color spectrum that I would like to use to change the color of a text selection in an input textfield. This is expected to act just like the colorpicker.
See this one Best way to get the color where a mouse was clicked in AS3 for getting the selected color.
When you have it, simply apply yourColor to the text selection with a TextFormat:
var myFormat = new TextFormat();
myFormat.color = yourColor;
yourTextField.setTextFormat(myFormat,
yourTextField.selectionBeginIndex,
yourTextField.selectionEndIndex);