Reputation: 2840
I'm just getting started with Flash/ActionScript and it seems to be the general consensus to create Sprite
s, Bitmap
s, MovieClip
s, etc for various objects in order to represent pictures and other graphics.
However, the way I'm used to writing games and whatnot in other languages is to just loop repeatedly and each frame use something similar to the Graphics
object to redraw the scene on the main Sprite
. Is this how it's also done in Flash, and is it good practice? I can do it this way, but I'm wondering if there's some Flash ecosystem standard instead.
Here's an example of the way I'm used to:
public class MyApp extends Sprite
{
public function MyApp()
{
var t:Timer = new Timer(20);
t.addEventListener(TimerEvent.TIMER, update);
t.start();
}
public function update(e:TimerEvent)
{
this.graphics.clear();
//Rendering code and updating of objects.
}
}
Is this acceptable?
Upvotes: 0
Views: 538
Reputation: 52213
Well, it depends.
In Flash, you have the option of relying on the Flash Player's vector rasterizer and rendering system, which will figure out all the redrawing for you. For instance, you can draw once to a Sprite
then simply apply transforms to the sprite (set x
, y
, width
, height
, rotation
, scaleX
, scaleY
, transform.matrix
, transform.colorTransform
, etc). Any of these objects could be a vector shape or a bitmap, and you can also use cacheAsBitmap
and cacheAsBitmapMatrix
for even more redraw optimization. The Flash Player will only redraw areas that change, on the frame that they change. I would consider this the traditional "Flash way".
Using the Graphics
API is just a programmatic way to create vector shape data. Think of it as a code alternative to drawing in the Flash IDE. You could draw using Graphics
once when the object is created, or if you needed to change the actual shape (ie not just the transform) you are correct that you would clear()
and redraw it. However, ideally you would not be doing that a lot. If you find yourself redrawing the shape a lot, you might want to move to a pre-rendered sprite-sheet approach. In that case you use BitmapData
to more quickly copy pre-drawn pixel data to a Bitmap
object. This is generally faster than relying on the vector rasterizer to render your Graphics
commands, as long as you use the fast pixel methods like copyPixels()
. This is probably closer to the sort of rendering systems you are used to in other platforms that don't have a vector rasterizer built in.
Lastly, it's worth noting that the newest (and fastest) way to render objects in Flash is completely different than all that. It's called Stage3D and it uses a completely different rendering pipeline than the vector rasterizer. It's powered by GPU rendering APIs, so it's blazing fast (great for games) but has no vector rasterizing abilities. It can be used for both 3D and 2D. It's a bit more involved to work with, but there are some useful frameworks to make it easier, most notably the Starling 2D framework.
Hope that helps.
Upvotes: 1
Reputation: 5265
It works the same way in actionscript.
public class App extends Sprite // adding "my" to identifier names doesn't add any information, so there's no real point in doing it
{
public function App()
{
addEventListener(Event.ENTER_FRAME, update); // "each frame"
}
private function update(e:Event):void //not just parameters of functions have a type, but also their return value
{
graphics.clear(); // no need for "this" here
//Rendering code and updating of objects.
}
}
Keep in mind that the Graphics API is vector based and as such will only draw so many things before dropping performance.
Sprite is a general purpose container, not to be confused with what the term "sprite" stands for in a sprite sheet.
What you are probably referring to when saying "main Sprite
" is some rectangular region of pixels that you can manipulate.In this case, a BitmapData
is what you want, which is displayed with a Bitmap
object.
BitmapData
does not offer a graphics property. Essentially, drawing vectors and manipulating pixels are treated separately in As3. If you want to draw a line in a BitmapData object, you'd have to first draw the line as a vector into a Sprite
(or better Shape
, if all you want to do is draw on it) using its graphics
property, then use draw()
of BitmapData
to set its pixels according to the drawn line.
Upvotes: 0
Reputation: 1438
The "Flash way" is to use EnterFrame event instead of using timer to draw. You must make your calculation whenever you want but let flash draw you scene.
Upvotes: 0