Create a Button with only code AS3 - actionscript-3

I feel like something like this exists but how would I create a button in only action script 3 code? I mean without making a shape and converting it to a button symbol or something. It would probably make it easier to only work in FlashDevelop.

You can use the SimpleButton class or roll your own using the Sprite class. Then you can draw anything or use any image as the button and it's over, down and default states.
For example a simple button can be like this :
var goButton:SimpleButton = new SimpleButton();
var myButtonSprite:Sprite = new Sprite();
myButtonSprite.graphics.lineStyle(1, 0x555555);
myButtonSprite.graphics.beginFill(0xff000,1);
myButtonSprite.graphics.drawRect(0,0,200,30);
myButtonSprite.graphics.endFill();
goButton.overState = goButton.downState = goButton.upState = goButton.hitTestState = myButtonSprite;
addChild(goButton);
You can have different display objects for each state of the button or you can attach bitmaps instead of sprites.

Related

FloodFill in AS3

I have been making a basic painting application similar to MS-Paint with basic paint, eraser and fill tools. It's this last one that's giving me some trouble.
I'm pretty new to using BitmapData but the idea is that when the user clicks the board, it triggers the startFloodFill method. This is shown below:
public static function startFloodFill(e:MouseEvent):void
{
trace("FLOODFILL");
var boardRef:MovieClip = e.currentTarget.parent.board; //Creates a reference to the board
var boardData:BitmapData = new BitmapData(boardRef.width, boardRef.height); //Creates a new BitmapData with the same size as boardRef
boardData.floodFill(e.localX, e.localY, 0x00CCCCCC); //Applies the FloodFill
boardData.draw(boardRef); //Saves the boardRef as bitmapData
boardRef.bitmapData = boardData; //Updates the board
boardRef.parent.addChild(boardRef);
}
Can anybody tell me what I've done wrong here? When I click, the board does not change. I expected the FloodFill to fill the entire bitmap with the chosen colour as the board is blank when I click.
I also tried replacing the last two lines with:
boardRef.addChild(new Bitmap(boardData) ); //Updates the board
Thanks
The problem is that you first use the floodFill, and then use the draw method, which actually fills the BitmapData with whatever param you give it - in your case it's the boardRef.
You should first draw the boardRef into the boardData, and then use floodFill. At the end, you need to create new Bitmap and display it.
Now you are setting the bitmapData of a MovieClip?! Don't know what you wanted, but you just need to add new child (new Bitmap)

UiApp button rollover setStyleAttribute

In UiApp is there a simple way to change the setStyleAttribute when the mouse is over a button. Basically I would like to create a rollover button.
Yes, this is possible, and it's somewhat simple.
You have to create two client handlers, one to set the styles on hover and another to set it back. Then add them to the button, like this:
var btn = app.createButton('Button').setStyleAttributes({color:'blue'});
var setStyleHover = app.createClientHandler().forEventSource().setStyleAttributes({color:'red'});
var setStyleBack = app.createClientHandler().forEventSource().setStyleAttributes({color:'blue'});
btn.addMouseOverHandler(setStyleHover);
btn.addMouseOutHandler(setStyleBack);
//add button to your interface
//show app

Making a movieclip appear when mousing-over a button

I'm trying to create a disclaimer link that, when moused-over, a movie clip (disclaimer window) will show-up on the swf. How do I do this in AS3, please?
I'm new, and I've only gotten this far:
import flash.events.MouseEvent;
import flash.ui.Mouse;
addEventListener(MouseEvent.MOUSE_OVER,showOptions);
function showOptions (e: MouseEvent): void {
}
You can load your disclaimer window movieclip dynamically from your library like so:
function showOptions(e:MouseEvent):void{
var disclaimer:MovieClip = new disclaimerMC();
disclaimer.name = "disclaimer"; //give disclaimer name in case used later
disclaimer.x = 100; //set coordinates of movieclip
disclaimer.y = 100;
this.addChild(disclaimer); //add the disclaimer to the screen
}
Make sure that you go to your disclaimer movieclip in the library, right-click it, go to properties / linkage, and change the Class field. In my example, I changed the Class field to disclaimerMC.
It might also be nice to let Flash know which movieclips the EventListener should apply to. The way your code is now, mousing over anything would call showOptions. As you probably don't want that, you can say
disclaimerLink.addEventListener(MouseEvent.MOUSE_OVER,showOptions);
So then, only when you mouse over disclaimerLink will disclaimer show.
Alternatively, if the disclaimer window will always appear and disappear in the same place, it is quicker to change its transparency value. Your MOUSE_OVER and MOUSE_OUT functions can call disclaimer.alpha=100; and disclaimer.alpha=0;, respectively.

Frame labels & function parameters

Is it possible to change a frame label within a gotoAndStop('label') with the parameters in a function?
I'm playing around with updating code as I learn more and more techniques, and at the moment the code is a basic click-a-button to select the object shape, and on press the button disappears:
// Change the object into a circle.
circle_btn.addEventListener(MouseEvent.CLICK,function(){changeShape_fun(circle_btn,circle);});
// Change the object into a square.
square_btn.addEventListener(MouseEvent.CLICK,function(){changeShape_fun(square_btn,square);});
// Change the object into a star.
star_btn.addEventListener(MouseEvent.CLICK,function(){changeShape_fun(star_btn,star);});
function changeShape_fun(shape_btn,frame){
shape_btn.visible = false;
main_mc.gotoAndStop('frame');
}
However I can't/don't seem to know how to change a frame label through function parameters, or if what I'm trying to do is even possible.
Also to note, while I'm all ears for any more efficient ways of doing what I'm trying to do, I would still like to know how/if you can change frame labels through function parmeters.
Thanks! :)
You're very close, but you're trying to go to a frame called 'frame' and not the string contained within the frame variable. Try this instead:
// Change the object into a circle.
circle_btn.addEventListener(MouseEvent.CLICK,function(event:MouseEvent){changeShape_fun(circle_btn, 'circle');});
// Change the object into a square.
square_btn.addEventListener(MouseEvent.CLICK,function(event:MouseEvent){changeShape_fun(square_btn, 'square');});
// Change the object into a star.
star_btn.addEventListener(MouseEvent.CLICK,function(event:MouseEvent){changeShape_fun(star_btn, 'star');});
function changeShape_fun(shape_btn,frame){
shape_btn.visible = false;
main_mc.gotoAndStop(frame);
}
This will go to a frame within main_mc called 'circle' when you click the circle or 'square' if you click the square, etc.

AS3 access to properties of movieclip loaded in dynamically

My movieclip clipArt_mc receives movieclips that are loaded dynamically from a listbox selection using:
var myLoader9:Loader = new Loader();
I apply color to clipArt_mc using the following:
var trans3:Transform = new Transform(MovieClip(parent).design_mc.clipArt_mc);
I would like to access the nested or loaded in movieclip inside of clipArt_mc that has in it a movieclip named color_mc so that I can apply color directly to it instead of clipArt_mc.
Can this be done?
Thank you in advance for your time.
Anne
There are a few ways you can try to get the child MovieClip. In your case, it sounds appropriate to try to get the child by name. If the situation is as you say, you should be able to do this:
var child:DisplayObject = clipArt_mc.getChildByName(color_mc);
You can then apply the relevant transform to child.