Is there a way to add text to sprite or movieclip not using TextField class? - actionscript-3

Is there a way to add text programmatically in as3 to a Sprite or a MovieClip without using the class TextField
TextField inherits from InteractiveObject which is kind of heavy for what I want to do: just display text (i.e. I don't want to interact with the text).
Note: I'm aware that there is a property selectable to make the textfield not selectable. This is not the point.
Thank you

There are two ways that come to mind. The first being this whereby a string is written to bitmap data: How to draw a string on BitmapData
Second, you could try this fast font rendering library (although I have not tried it myself) lab.polygonal.de/2009/12/15/font-rendering-with-the-fp10-drawing-api/
Both these solutions seem to bypass the need for creating a textfield (except the first where it gets used to create but then is discarded).

Related

Accessing List and TextInput inside a movieClip

I have a problem accessing a List and TextInput fields placed inside an MC.
What I'm trying to do is an animation for a menu. So I placed buttons and List and a TextInput inside a MC which I want to animate.
I'm trying to access them like this:
string_variable = mc_name.textInput_Name.text;
Everything worked fine before I placed those controls inside an MC. The code was, of course, without mc_name. prefix before that.
What am I doing wrong and how can I fix this? My goal is to make a complex keyframe animation of a clip with buttons and other controls inside it, specifically List and TextInput ones.
Thanks.
Most animations, especially UI, like menu blocks, lists, could be done with tweening engines. You will have more control over animated elements. MovieClip isn't a nice choice to use as mechanism to animate UI components that should be accessed.
Anyway, If you want MovieClips in your project, give same names to the components on the timeline on every keyframe. It will help, a bit...

as3 how can i prevent that a new instance is created by entering a frame?

i am working with several nested movieclip objects in a project. but i get into trouble with the buttons i created and implemented in the nested movieclips:
to describe it in a simple way:
I have a main movieclip with five frames, including two buttons with listeners to browse between the frames. Then inside of one Frame I have another movieclip with its own buttons. i instanciated it by hand not through code and gave it a specific name like "nestedMc".
Now I dont want to build the Listeners for those buttons inside the class of the nested movieclip class but in its parent class, which works fine until i then goto another frame in the main movieclips timeline and come back.
obviously every time flash enters a frame its contents get created anew (and therefore get new instance names). I could now try solve this through filling the frames via code.
But maybe there is another way to make sure the frame contains the same instance everytime i enter?
Timeline scripting is a dirty business, and really, a carry-over compatibility layer for Actionscript 2 projects. Whenever possible, I highly recommend not doing it, and simply keeping all of your code in your document class. As you're experiencing, timeline code causes headaches.
Consider instead just creating both states of your Stage (it sounds like that's what your two buttons are jumping between) and simply hiding them offstage or setting their alpha to zero and their mouseEnabled state to false. Furthermore, if the purpose of your frames is to play animation (a tween), consider instead switching to a much more powerful suite such as TweenLite. Moving an object over a hundred pixels (smoothly) can be as easy as:
TweenLite.to(redBall, 3, {x:100});
Now, if you're manually adding these items to the stage, as long as the object is a dynamic one, you can assign an instance name to it which will be saved between frame loads. Be aware the object name is not the same as the instanced name. For example:
var redBall:Ball = new Ball();
redBall.name = "bubbles";
The object's name is Ball, but it's represented as a variable called redBall. Its actual DisplayList name will likely be ambiguous (such as "Instance71"), and I can manually define it as "bubbles". 3 different names for the same object, all very different and necessary.
Even if you give the object a displayList name, you may not be able to reference it through code unless you enable Automatically declare stage instances, which basically creates on each object a pointer to the displayList object.
That said, you can always fetch the object by other means. Obviously, your buttons are always appearing, but you're trying to find a very specific object on the stage. At this point, we can use getChildByName() or getChildAt().
Hope that helps.
-Cheers

Problems with contentPane in mx.flex.container eating mouse input

I'm working on a game that uses mx canvases (each in their own mxml file) to wrap different aspects of the application. So the UI is wrapped in one canvas, the main game screen in another, and so on. I am having an issue where mouse input (specifically MouseEvent.CLICK, but it seems to apply to all mouse input) that I want to go to a movieClip in the GameScreen.mxml is being caught by an mx.core.FlexSprite object called "contentPane" that is a child of the GameUI.mxml.
This contentPane sprite doesn't exist when the GameUI object is instantiated, and in fact doesn't seem to exist until I set the text of some textFields contained by the GameUI. These textFields do overlap the movieClip that I want to receive the mouse input, but the textFields themselves are set to mouseEnabled = false, and are not catching the mouse input.
None of my code is directly creating this contentPane sprite, and some elementary Googling tells me that this contentPane sprite is created by the mx.flex.container internally. However, I can't seem to find any documentation on how this actually works, and what causes this sprite to be created.
This functionality has worked previously, and the only significant recent change I'm aware of is moving the swfs loaded into the GameUI into their own application domain to fix a namespace collision. I'm entirely prepared to believe that is the issue.
Ideally, I'd like to know why this contentPane is suddenly catching mouse input. Failing that, I'd at least like to find some documentation on how contentPane works, how I can manipulate it, and what causes its instantiation.
Thank you in advance for your help.
Edit:
I've done some additional digging and wanted to share what I've learned:
The contentPane variable is instantiated in mx.core.container objects for scrolling and clipping purposes. If the content of an mx.core.container object exceeds the size of that object, the container will create a contentPane and move its contents into that pane. However, if scrolling is disabled (verticalScrollPolicy="off" & horizontalScrollPolicy="off") and clipping is disabled (clipContent="false") then the container will not instantiate the contentPane. This solved my specific problem as I did not need either scrolling or clipping behavior in this container.
I would still like to know if there's a way to disable mouse input for an mx.core.container contentPane. It seems like there should be.

How do I approach rendering text in a fully sprite based game? (ActionScript 3)

I've created a Canvas class which has an Array of multiple instances of CanvasEntity. Each of these has a skin property which is a reference to a previously created instance of BitmapData, representing some graphics that can be used.
The Canvas runs through this list each frame to perform a render:
lock() the canvas
Clear the canvas using fillRect().
Loop through the aforementioned Array and use copyPixels() on each of the instances' skin properties.
unlock() the canvas.
This is all fine, but now I'm a bit unsure of the best way to approach rendering text in the same fashion.
Do I need to make a sprite sheet with all of my glyphs on it, just like I do for all the frames of any other object? Or is there a simpler way to create a piece of BitmapData that will represent these?
Making a sprite sheets seems painful as there would need to be individual objects representing each glyph to be rendered.
So far I have this as a sprite sheet:
You don't need an individual object for every character. If you want a low-cost text display, have a single class that extends CanvasEntity and accepts a string in the constructor, copies relevant data from the spritesheet to the skin.
The average game doesn't contain that many text. It's generally not a performance critical part that requires to much optimization. You could just create a normal sprite, add a TextField with the text and style you need and copy that graphic to canvas. Maybe chache the text graphics if you need them more often.
If you want some animation (character-by-character-appears-with-annoying-sound-of-typewriter-effect), you probably should go with the sprite sheet. But you can create that dynamically just once, using the same technique. Loop through every possible character of your font, draw it to your sheet.

How to copy Movieclip display properties as a vector into another movieclip

For my project I need to copy the graphics of a Movieclip with all of its children without copying anything else into a second Movieclip. I cannot use the Bitmap classes because I need the graphics to be displayed as a vector and I cannot use a method that simply copies the clip by calling the instructor ie:
var copy:MovieClip = clip.constructor
Is there any way to copy only the display portions of a clip into another Movieclip without turning it into a bitmap?
Thanks
Cloning display objects out of the box in as3 is no easy task. Here is a tutorial explaining the several approaches that have been attempted over the years and what works and what does not:
http://www.dannyburbol.com/2009/01/movieclip-clone-flash-as3/