ActionScript 3: How to move a TextField smoothly? - actionscript-3

In my program I have a feature of text scrolling across the screen. This works fine, except for the unbelievable laggy movement. I'm simply adding the speed to the x-position of the textfield, and the movement animation works fine for all other objects (movieclips, bitmaps etc).
EDIT:
I now tried to convert the text to a BitMap, and then move it. Unfortunately this resulted in the same "laggy" movement with lots of sudden "jumps".
bmd = new BitmapData (event_field.width, event_field.height, true, 0);
bmd.draw (event_field);
bm = new Bitmap (bmd);
bm.x = event_field.x;
bm.y = event_field.y;
bm.cacheAsBitmap = true;
bm.smoothing = true;
this.addChild(bm);
In my enter-frame-function:
bm.x-=3
Does anyone have a solution for this?

Not smoothing the textfield may improve performance, but using the native TextField class in my experience, has performance limitations that can't really be overcome. Real-time rendering of text is expensive. Real-time rendering that text to bitmapData, can also be expensive and is really only beneficial if the text doesn't change much. In any case that's something cacheAsBitmap should already be doing automatically.
To overcome both of these issues, you should consider bitmap fonts. These are pre-rendered, solving both of the above issues. However, the native API doesn't support it.
The two options that I'm aware of:
BMFontRenderer. Some classes that supposedly provide support
for bitmap font rendering. I've not tried this so I can't vouch for
it.
Use Starling for your whole project, which includes
excellent support for bitmap fonts and distance field fonts
(scalable bitmap fonts). It's a 2D hardware accelerated framework,
so this is just the tip of the iceberg in terms of what it can
offer. I use it for everything now, but for your project it may be too late to be switching from the native framework to another.
To generate your bitmap fonts, these are some tools recommended by the Starling Manual. I personally use Littera and BMFont:
Littera
BMFont
Glyph Designer
bmGlyph

You can try embedding font or animate bitmap not textfield
create snapshot of textfield before animation starts,
hide textfield
animate snapshot
at animation finish move invisible textfield into destination, destroy snapshot and show textfield

Related

How can I optimize a large DisplayObject for tweening?

I'm writing a small AIR application that loads a presentation from a server and transitions through the slides using TweenMax. Each Slide object within the loaded presentation contains basic information about the slide, telling the Renderer class how to draw it (things like where a TextField should be, what its text should be, any filters that should be applied to the TextField, etc).
The Renderer then creates a Bitmap using the provided specifications, and that Bitmap is then given an alpha of 0 and tweened in using TweenMax.to.
I have a relatively powerful machine, one that should be able to handle such a task no problem. However, when I tween the Bitmap in, the framerate drops from a capped 60fps to around 11fps. The output resolution (the size of the generated Bitmap) is only 1436x1076.
My question is: is there anything else I can do to improve the performance of my application?
All tips are appreciated.
Thanks.

Canvas performance in Dart

This problem is related to my framework VilTAGE (https://github.com/ViliX64/VilTAGE). It's an ASCII web game framework, that renders to Canvas. The problem is, that the drawing time is twice as long as the computation time.
The way the rendering works now is that there is a HashMap of each CharNode (an object, with a text character, color, etc..) and CanvasElement, that has the text drawn onto it. When the loop wants to draw a CharNode (15-30x a second), it goes through the HashMap, find required CanvasElement and renders it to the main CanvasElement. (See https://github.com/ViliX64/VilTAGE/blob/master/lib/etc/utility.dart).
The individual CanvasElement snaps are small (usually about 10x10). They are drawn using drawImage(..);
The problem is, that in a game with 70x40 CharNodes, the game runs (unsuprisingly) very slow and even more, when compiled to JavaScript.
Is there any obvious mistake I am making or could you please give me any tips of improving the performance?
EDIT 1: It runs the slowest on Firefox and IE. Performance on Google Chrome is better.
If it works like it does in javascript, your issue is probably with the fact that you're cacheing a CanvasElement and that the drawImage have to retrieve the bitmapData from it each time it is called. This is a really expensive operation.
You could try to cache the imageData with getImageData and draw it with putImageData (both are CanvasRenderingContext2D methods)

Taking advantage of Flash hardware acceleration

My goal is to test Flash 3D environment performance by generating lots of 3D cubes, rotating them and reading FPS.
I know that I can rotate 2D objects in 3D space. For example, I can construct a cube with 6 movie clips, rotating them and putting them together accordingly. Then I can generate lots of these cubes in random x,y,z locations (predefined area in front of viewport), rotate them and read fps.
But then I read about this hardware acceleration and it's unclear to me, when it is activated/used. Certain conditions must be met. I know that it must be allowed by the user (right click->settings->enable hardware acceleration) or if it is embedded through object tag, wmode=direct must be set.
That's from viewers side, but what from developers?
If I draw a simple red rectangle on stage, and user has enabled hardware acceleration, does that mean that graphic information will get rendered on GPU?
I'm reading various sources and "Adobe Flash 11 Stage3D Game Programming" book, and from what I gather, in order to render graphical information on GPU, I have to explicitly call Stage3D class in AS3, then I can draw my 2d/3d objects there.
Which is it - if I want my app to be run in hardware acceleration mode (and its enabled from viewers side), does it happen automatically no matter what the content in my flash file? Or do I have to add the Stage3D class there.
Thank you.
Will be waiting for the answer.
First off: Don't mix up 2 distinct concepts in Flash:
General Hardware Acceleration
This was introduced mainly for video playback in Flash Player 10. I am not sure if it used for graphics at all. It has nothing to do with rendering 3D Graphics directly on the GPU.
GPU Support
With Flash Player 11, Adobe introduced Stage3D. This is an interface through which Flash can render graphics utilizing the GPU, if available. On Windows it uses Direct3D, on Mac OpenGl. The classic Flash DisplayList Graphics API does not support this. You have to go through the Stage3D Api, as explained in the book you have mentioned. To use GPU Support in the browser the embed tag needs the attribute wmode set to "direct". Working directly with the Stage3D Api is not easy and you have to learn the concepts of 3D Programming, Shader Programming, etc. for doing this. There are some libraries which help working with Stage3D. For 3D there is i.e Away3D. If you just need 2D graphics with gpu support, Starling is a very popular framework these days.
Here's a function that converts a movieclip into a bitmap. I use normal flash to create what is going to be shown on screen, then convert the movieclips to bitmaps and delete the movieclips:
private function fConvertClip(pClip: MovieClip, pWidth: int, pHeight: int): Bitmap {
var bd: BitmapData = new BitmapData(pWidth, pHeight, true, 0x00000000);
bd.draw(pClip, null, null, null, null, true);
aBitmaps.push(bd);
var bmp: Bitmap = new Bitmap(bd, "auto", false);
bmp.smoothing = true;
return bmp;
}
Don't turn on the hardware acceleration setting. In general, you control what's on the gpu with code. Bitmapdata is on the gpu so you convert everything that goes on the screen to bitmaps.
gotoandlearn.com has great tutorial videos on this.
You can create anything in normal flash - shapes, gradients, dropshadows, dynamic text, programmed coloring, etc. - and convert it all to bitmaps with programming while your game is running. You don't need to import images/spritesheets/animations. Have artists give you everything in vector. So when the user wants to change the color of a car, you simply convert the code instead of loading/importing a bitmap of the car in red.

Bigger stage vs scrolling background

I'm making a flash game, and I can't decide to use a bigger stage or a smaller one, with scrolling background to make it run smoother. I's going to be some kind of strategy game if it matters.
Thanks
One option is to have a bitmap object the size of your stage for example 800x600, then draw your objects into the bitmapdata of that bitmap using copyPixels this is known as blitting and is pretty fast.
Another option is to just use the flash display list and add every object as sprites onto the stage.
Second method is easier to handle in terms of mouse events and stuff.
My advice is to start with the second option and see if performance is enough, if not go with option 1.
There are many, many variables that determine the performance of your application.
To answer your question, a smaller stage area will make your program run faster. The amount of difference will depend on the way your application deals with display objects. Flash will not render things that are completely outside the stage bounds. So keeping only those objects that are needed at any given time on the stage is a good practice.
using flash player 11's new stage3D features (even for 2D stuff) will likely make your game very smooth. Lots of good frameworks out there that can take care of the low-level stuff if you don't want to get into it. For 2D, starling is very easy to get started with, and seems to be Adobe's favored framework.
At a bare minimum, make sure you use GPU acceleration in your output/compiler options.
There are LOTS of other tips for optimization people could get into, but that is better suited for google searches as Stackoverflow is about specific questions.

Actionscript 3.0 Tween Question

I am currently working on a website for a construction company and I am trying to build a very simple flash intro. I have two images in the falsh intro: a tractor and a picture of clouds in the background.
I am trying to have the clouds appear to float by in the background. It is a very wide image that I am trying to loop through very slowly. It looks pretty cool :) IMO.
For tweening, I have always used TweenLite. The problem I am having is that while the image is moving, it 'blips' and looks pretty choppy. I have adjusted fps and about every other thing I can think of. The only thing that fixes it is when I use Hardware Acceleration (Level 2 - GPU). But this creates all sorts of problems for the HTML page.
My Question Is:
I am just a terrible coder, or would I be better off using Tweener (or something of the sort) or using the native tween engine in flash cs4? Any advice would be great!
Try these:
If the image is in your Library, make sure "Allow smoothing" is checked in your Bitmap Properties (select the image in the Library and go to "Properties" in the Library menu).
Or, if you are loading the bitmap dynamically and using the BitmapData class, make sure you've set the "smoothing" property to "true" on your BitmapData.