Best way to be able to pick multiple colors/designs of symbols dynamically from flash - actionscript-3

Sorry the title's so convoluted... I must've tried for ten minutes to get a good, descriptive title! Basically, here's the scenario.
Let's say a user can pick fifty different hat colors and styles to put on an avatar. The avatar can move his head around, so we'd need the same types of movements in the symbol for when that happens.
Additionally, it gets which hat should be on the 'avatar' from a database. The problem is that we can't just make 50 different frames with a different hat on each. And each hat symbol will have the same movements, it'll just be different styles, colors and sizes.
So how can I make one variable that is the HAT, that way we can just put the appropriate hat symbol into the variable and always be able to call Hat.gotoAndplay('tip_hat') or any other generic functions.... Does that make sense?
Hope that's not too confusing. Sorry, I'm not great at the visual Flash stuff, but it's gotta be done! Thanks!

debu's suggestion about a hat container makes sense in order to separate out control of the hat movement.
You could take this further by separating out different aspects of the appearance of each hat (not just the colours, but also style, pattern, size, orientation etc) - this would allow you produce a wide variety of different hats from just a few parameters.
So for example 6 styles x 4 patterns x 8 colours = 192 different hats (without having to draw each one!)
(source: webfactional.com)

You could do that a number of ways; firstly you could have each different hat as a different symbol in the Flash Library (if you're using the IDE), and then in their properties tick to 'Export for Actionscript', and choose some appropriate name. It'll tell you that there's no definition for the class path, and one will be created automatically (or something), but that's no problem as you don't need to create a class file for these objects - they're simply MovieClip extensions with some specific data in them.
So if you do that with each hat, let's say you name them Hat_1, Hat_2, etc; then you need to create a 'hat' object inside your avatar's head object. Whenever the hat is changed, you call a new instance of that specific hat object, and put it on the stage:
//when user chooses a hat, however this is done:
var newHat:Hat_1 = new Hat_1();
avatarBody.avatarHead.hat.addChild(newHat);
Then that hat symbol gets added to the hat object of your avatar, and will move with the head object as you'd expect. You can change up the hat on the fly, by simply calling a different hat type and removing the previous one.
Alternatively you could do it by loading in the hat symbols from external images, and storing them in variables for when they need to be added to the avatar object. You'd do this using XML; if you don't know how that's done, I can explain.

Related

is it possible to create a fixture for a body that could be broken by some other body?

I really want to know, is it possible to create a fixture for a body that could be broken by some other body?
There is the example:
a body with its fixture divided into small figures:
and what happens after it is hit by another body:
P.s. Are there any programs that could help the process of creating such fixture?
yes you can do this using Breakable spotted at :
net.dermetfan.gdx.physics.box2d.Breakable
The Breakable allows to easily make whole bodies or single fixtures breakable, which means they will be destroyed if a certain force or friction is applied to them.
How to use
A Breakable is meant to be put in a body's, fixture's or joint's user
data. A single Breakable instance can be put in the user data of
multiple bodies, fixtures and joints. Since this may collide with the
Box2DSprite or other classes using the user data, the
Breakable$Manager uses a Function to access the Breakable in the
user data of a body, fixture or joint.Do not forget to set a Manager
instance as ContactListener to the world and to call destroy() after
every timestep. If the field is already in use, check out the
ContactMultiplexer. The Manager does the actual work, the Breakables
are just passive data holders.
A Breakable consists of a normal resistance, tangent resistance, an
option to destroy its body in case its last fixture was destroyed and
an option setting if the body should be destroyed no matter the amount
of remaining fixtures.
The normalResistance is the force that can be applied to the
Breakable before it breaks (inclusive). The tangentResistance is the
friction the Breakable can bear (also inclusive). The
reactionForceRestiance specifies the reaction force a joint can bear
on each axis. The reactionForceLength2Resistance is the max squared
length of the joint's reaction force the Breakable can bear.
referred to libgdx-utils
some other good references with good examples here and here
for the question (Are there any programs that could help the process
of creating such fixture)
yes you can easily use box2d-editor which allows you to create complex polygons and you can also create your bodies and shapes from your images or sprites check the official documentation in the same page there are several video who explain the way box2d-editor works :
Features:
Automatically decomposes concave shapes into convex polygons,
Automatically traces your images if needed,
Supports multiple outlines for a single body,
Supports polygon and circle shapes,
Reference point location can be changed,
Visual configurable grid with snap-to-grid option,
Built-in collision tester! Throw balls at your body to test it,
Loader provided for LibGDX game framework (written in Java),
Simple export format (JSON), to let you easily create your own loader for any framework in any language.

AS3 How to communicate between frames

I have been writing a game in timeline code. I want the different frames (rooms) in the game to be able to share information between each other. Of course, timeline code is limited to the frame it is written in.
After doing quite a bit of reading ("Foundation Game Design with Flash" and a number of articles, tutorials, forums etc) I decided to employ a document class. This did not work either. It seems it only works for frame one but not the rest of the frames (I have four).
How can I have frame four respond to something that happpened in frame one? For example, if the player achieves something in frame one, I want a movie clip in frame four to be visible.
If You are writing your code on the timeline, My suggestion would be to create two layers in the timeline, one for 'frame-actions' - in this layer you insert the code specific to a single frame (will work when the movieclip is stopped on that particular frame).. And also create one more layer called global-actions (for the entire timeline). Only the first frame will be a key frame and there should be empty frames till the end of the timeline.
In this layer actions write the code that you want to access from any keyframe in the same timeline.
If you define a variable in the actions which are written for the whole timeline (global-actions) then that will be available on all the frames.
Now if you want to go to a different frame based on some action, just write some functions in the layer which contains global actions and call that particular function through the frame actions. To go to a different frame use the 'gotoAndStop(frameNumber)' function of flash.
I want to tell you that while it will work, I would not recommend using it in this way.
HTH.
You can use static variables - these are variables which are linked to a class, rather than an instance of it.
Suppose your document class was called Document.as, and you wanted a variable, playerLives, to be visible from any part of the program.
Declare it inside Document.as:
public static var playerLives:int = 3;
You can then reference this directly from anywhere else in your code with:
Document.playerLives
(note that the variable is a member of the class itself, not an instance of it).
You could use a dedicated Statics class to hold these variables if you want to keep your document neat, or attach them to the relevant classes (eg Player.lives)
I've not used timeline/frames for some years but I believe this is how I used to do it!
NB Statics will be fine for your purposes but they are, in some ways, an equivalent to the _global variable in AS2 (at least, they can be used in the same manner) - many would not approve of their use, or over-use, as they are freely accessible from anywhere in your program (thus anathema to the OO concept of encapsulation), but personally I try not to worry about it in small cases - the most important thing to know about the rules of any design pattern is when they can be broken!
They are also slightly slower to access than instance members, but you won't notice this unless you are constantly accessing/changing them (making things like player velocity, which will need to be referenced/changed every frame, static, is not a good idea).
Hope this helps.
You may find the simplest way to link everything with the document class is to move your four frames into a movieclip together and have that on the first frame, then interact with that movieclip.
E.g. in the document class, where the movieclip instance on the timeline is called 'game'.
game.gotoAndStop(4);
game.objectToDisplay.visible = true;
If you encounter reference errors in the IDE then you can avoid these by using [] notation to refer to the properties of game, e.g. game["objectToDisplay"].visible = true;
Note that it's not really best practice to do this, but it will at least help you to finish that first game which is really more important at this stage in your learning. Afterwards, if you want to learn more then I'd recommend "The Essential Guide to Flash Games" by Jeff Fulton from 8bitrocket.com - it will teach you how to use the document class effectively.

Workflow for Large Flash/AS3 Projects

I am currently working on a rather large, UI-heavy Flash game. Our team has been working on this for about 9 months now. None of us had any previous experience with Flash, so we have continually improved our workflows during this time. However, we still feel that what we are doing now is not optimal, especially the interface between coders and artists, so I am wondering how other teams are working.
The ideal workflow should satisfy the following requirements:
1. Reused UI elements are defined only once
This means, if we want to change a font or a button style, we do not want to go trough all of our menus and change them manually. We want them defined in one central place and only referenced from there. Bonus points if the assets are shared not only at edit time but also at runtime, i.e. they are downloaded only once.
2. Everything is loaded on demand
Currently, we have two different loading steps: First, we load the menu libraries. When this is done, the players can already interact with all the menus. Then, we start loading the actual gameplay data. The initial loading time is still too long, though, and causes us to lose many potential players. What we really want to do is to load only the bare minimum required for the main menu and then load everything else only when the player tries to actually open the respective menus. Zuma Blitz does this really well.
3. Artists can perform minor changes without help from coders
If a menu should be re-designed without changing the actual functionality, it should be possible for artists to do that on their own in Flash CS6. This requires a clear interface between art and code, and it should also be possible for artists to test and debug their changes before sending them to the coders.
-
Our current workflow looks like this: The artist build the screens as MovieClips in Flash CS6 and export them as SWFs. On the code side, load the MovieClips from the screen SWFs and use them as the View classes in our PureMVC-based system. The Mediators access the elements like text fields in the Views by their instance names.
This is error-prone because there is no central place to define the interface (i.e. the instance names). A lot of communication overhead between coder and artist is required. Also, it creates a dependency between the code and the internal structure of the movieclip. The artists cannot attach the text field to a different sub-movieclip when they want to apply some effects to it.
We are experimenting with an event-based interface that requires the artist to add a few lines of code to the movieclip. This is less error-prone and interdependent than before, but it still does not completely satisfy (3) unless we write additional tools for testing and debugging. This must be a common problem and I can hardly imagine that there is no easier way.
For (2), we also started building a home-brewed solution but again, this is such a common task, there has to be something out there already that we can use.
So, how do experienced Flash developers manage such large projects?
I have some thoughts, but they are based on my coding style, which is unique to me.
1. Reused UI elements are defined only once
Depending on what you're reusing, this can be as simple as defining a library symbol and just using it. Fonts can be replaced without digging with a search and replace, and you can also simply swap out the font in the Font Embedding menu.
For sharing across xfl's, you can use a Flash Pro Project. Keep in mind that there's a certain amount of time overhead involved in this (files will want to update when you open them or save them, Flash crashes more with Projects, and it can be a bad idea to try to work on two files from the same project at once).
Some people use swcs, but doing so requires that you instantiate things in it in code, which might not work for your workflow. I use them for audio only, and I find that the objects in it have to be compiled on or before the frame you designate as the AS compile frame, or the sound can't be properly instantiated. I suspect this is going to be the case for anything instantiated from a swc.
2. Everything is loaded on demand
One of the best-kept secrets of Flash is that this is trivially easy to accomplish using the timeline and educated use of the complier. Here's how it works:
If your ActionScript compile frame is a frame greater than 1, then here is how things will compile:
Before Frame 1:
Any visual assets and embedded sounds used on frame 1
Your main Document Class, plus any Classes directly referenced from the Document Class (which is a good reason to code to Interfaces)
Before your AS compile frame (N):
Your AS Classes (the code, not necessarily the visual/audio assets)
The visual and audio assets for any library symbols set to Export for AS in frame N (even if they are not used in the swf)
Before the frame where the asset is first used on the timeline:
The visual/audio assets in all library symbols where Export for AS in frame N is not checked.
If you put a spinner loading graphic on frame 1 and you have selected frame 10 as your export frame, then if you just let the movie play until it hits frame 10, here is how it will load:
If you have any heavy assets in your spinner or directly referenced in your main Document Class, users will see a blank screen while this stuff downloads
The spinner will become visible and spin
Once your AS Classes have loaded, along with the Library Symbols set to Export in Frame 10 and the assets that are actually on Frame 10, you'll see those assets, and everything you need to use them will be ready.
The rest of the swf will continue to load in the background, updating framesLoaded.
I actually use a combination of a setter for the object that's on frame 10, plus an ENTER_FRAME handler to check to see if we're on frame 10 yet. There are certain things that I have to do that are easier based on one and others that work better to do the other way.
3. Artists can perform minor changes without help from coders
If the code is all in the Base Class for the library symbol, artists don't need to understand it, as long as they don't remove or change a needed instance name. I try to minimize dependence on instance names by watching ADDED_TO_STAGE (capture phase) and watching for Display Objects by type. Once I have a reference to an object of the appropriate type, it's easy enough to watch for REMOVED_FROM_STAGE on that object to dereference it. This is similar to how frameworks such as RobotLegs and Swiz work.
Further, I use a concept I call "Semantic Flash," where I do a lot based on labels. I have a base Class, FrameLabelCip, which has built-in nextLabel() and previousLabel() functionality, as well as dispatching FRAME_LABEL_CONSTRUCTED events. It's really easy to go from storyboard event name to Flash label name and just build out the graphics bang-bang-bang.
I make heavy use of Graphic Symbols for synchronizing graphics across multiple labels (for example, bulleted lists), instead of relying on code. This animator's trick makes these things both robust and approachable to less-technical teammates.

Can two buttons with the same class instance act differently in Flash?

Say I have this specific button class in Flash called cont_button and it's supposed to be used to break out of a loop, but I want to use the class more than once. Is there a way to give every instance of this class some kind of parameter so that it knows which frame it nees to go to?
Example:
I have an instance of cont_button on frame 200 and there's a loop between 200 and 210. This cont_button executes a gotoAndPlay(211). But later on I have another instance of the button on frame 315 and a loop between 315 and 325. Is there a way to make it so each instance knows which frame it specifically needs to go to via the use of a variable? Or am I going to have to make an actionscript file for each individual one?
Pretty new to ActionScript so I appreciate the help and if there are good coding references to AS3 you guys recommend, I'll gladly look those over.
Sure, this is possible. One way you can do this is make the frame numbers class variables and when the button is clicked, they reference whatever value is stored in them, rather then hardcoded numbers. To get a better idea, can you post the relevant parts of your button class?
As you say, you need to pass a parameter to each instance of the button. There are lots of different ways you could do this, but I'd be tempted to just do it via the instance names.
You could name each button loopBreakTo211, loopBreakTo326 and so on, then in your button's class have:
var breakFrame:Number = Number(name.replace("loopBreakTo", ""));
(parent as MovieClip).gotoAndPlay(breakFrame);
Admittedly that's not a very robust way of doing it (for example, it will break if a button is named incorrectly and breakFrame ends up as NaN, so you might want to add a check for that), but it keeps the parameter together with the instance instead of in the timeline somewhere.

Add text to Alternativa3d?

I'm creating an app with Alternativa3d (8.17.0), and would like to add labels to some cubes. But it doesn't seem like the Alternativa API provides a way to do it...
Now I know I can either:
Add a TextField to the Flash display list in the normal manner
Render the text as a bitmap beforehand and upload as a resource to the GPU
Render the text to a bitmap at run time, and upload as a resource to the GPU
BUT I need the content to be added directly to the Alternativa cube (which precludes the first option) and the text is dynamic (i.e. not known at compile time - precludes the second) and the third just feels hacky.
Is there a clean way to do this?
Depending on how you want the text displayed you could do a number of different things..
You could use a Sprite3D and add the text rendered as a bitmap. You can place the sprite3d near the cube and it will always face the camera so as you move around the cubes they will appear in 3d space but facing you at all times.
Another option is to add it to the actual cube as suggested by danii using a movieclip. However the link to that MovieClipMaterial is no longer valid and is actually for a previous version of Alternativa not version 8. I have created my own movieclipmaterial for version 8 here ( http://davidejones.com/blog/1392-moviematerial-alternativa3d-8/ ) should you want to do that.
Lastly you could render the text to bitmap and merge this with the cubes bitmap so the text is layered on top and then set this material onto the cube.
Personallt i'd use the sprite3d method its alot easier and i think will give a better effect. Take a look at this example which uses the same effect ( http://gkb40.ur.ru/web/map40a3d.swf )
Just put your TextField inside a MovieClip, and use the class MovieClipMaterial to set that MovieClip as a material for one of the sides of the cube.