View Full Version : Embedded assets and Classes

04-20-2008, 07:50 AM
Hey everybody!

I'm somewhat new to Flex, coming from Flash CS3, so I'm most familiar with the Library paradigm and trying to port that knowledge to Flex. Here's a specific problem that I'm having...

I know you can attach embedded asset symbols to a full fledged class, similar to defining Linkage in Flash CS3. I'll be using a typical login dialog box to demonstrate this problem. Imagine that the Artist gave me an assets.swf that contained a LoginModule movieclip with a textfield for username input, a textfield for password input, and a button movieclip for clicking. I attach the LoginModule asset like so:

package ui.modules{
import flash.display.Sprite;

public class LoginModule extends Sprite{

public var usernameTxtbox:TextField;
public var passwordTxtbox:TextField;
public var loginBtn:MovieClip;

public function LoginModule(){}

Works great like this! However.... I would like to have that particular login button behave like yet another class I have written, called CustomButton. All CustomButton is is a MovieClip with 2 frames, stopped at the first frame, but will move to the second upon mouseover:

package ui.controls{
import flash.display.MovieClip;
import flash.events.MouseEvent;

public class CustomButton extends MovieClip{
public function CustomButton(){
addEventListener(MouseEvent.MOUSE_OVER,onMouseOver );
private function onMouseOver(e:MouseEvent):void{
private function onMouseOut(e:MouseEvent):void{

This is where I'm stumped.

If the Artist gave me the asset.swf with that LoginModule clip with a button already attached, trying to force loginBtn in LoginModule to be a CustomButton using loginBtn:CustomButton would throw an error. In Flash, all I had to do was set the Button's Base class to ui.controls.CustomButton, but at this time it seems my only option is to ask the Artist not to include the button in the LoginModule, and allow me to manually include the loginBtn manually as a CustomButton. This would greatly hamper the Artist's duties, so I'd like to solicit some advice from the gurus out there: How do you handle situations like these?

Thanks for any and all help!

04-21-2008, 12:57 PM
Is there any reason why you can't use Flex's built in Button component, then implement upSkin, overSkin and downSkin pointing them at your custom graphics? Try not to reinvent the wheel if it's already there for you!

A tutorial of skinning buttons is here: http://blog.preinvent.com/node/2

04-21-2008, 10:56 PM
Thanks for the reply :)

Well, this is actually a simplified case scenario to solve a more complex problem I'm having. I'm actually building a game where levels are created by the artist and handed to me. These game levels have prepositioned NPC enemy movieclips, which require custom logic (much much more complex than simple buttons) to move around.

Basically, what I'm trying to ask is: Is it possible to attach custom logic to these NPC's when importing the entire level in flex, if so, how? If not, does that mean I must ask the artist to give me the level movieclip with nothing on it, and let me embed and instantiate the enemy objects myself?

In the case of the button scenario I gave earlier, the same thing applies... what if the artist wanted to hand me the entire Login dialog with the button, because she likes to take full control of the design and layout? Am I out of luck?

04-22-2008, 02:46 PM
I've actually never tried to do this. What I have done successfully is access library elements via linkage ID using gskinner's EmbeddedLibrary class:


That way you get a MovieClip that you can successfully call gotoAndStop to manipulate its timeline. Depending on how complex your logic is you may just have to call the basic timeline functions in that manner. I'll try have a play with it later, see if I can get something working.