Skip to content
nulldesign edited this page Apr 12, 2012 · 36 revisions

Getting started:

Everthing in ND2D starts with a World2D. The World2D is the only object that derives from a traditional flash display object, the sprite. All other objects are independent of flash’s displaylist. In every ND2D object you’ll have access to the flash stage object, if you want to use it to overlay ND2D content with flash content. To get the engine running, you create an instance of World2D. The constructor takes these arguments:

  • renderMode: Indicates if the engine should use hardware acceleration
  • frameRate: Set it to 15, 30 or 60 fps, because most displays will run at this speeds
  • bounds: Set the size and position of your game world here, optional
  • stageID: You can have multiple independent Worlds side by side, this is their ID

After your created your world, it’s time to create a Scene2D. A scene is just a container for all your game objects. It can contain your entire game for example or a game-over screen. A world can only have a single active scene, but you can switch between the scenes fast by calling setActiveScene().

So you created the World, added a Scene, it’s time to add some moving objects to the scene by calling addChild():

Available display objects:

Sprite2D - Acts similar to a flash sprite, except the registration point is centered. You can feed the sprite with the following objects:

  • BitmapData: Standard flash BitmapData

  • SpriteSheet: A simple spritesheet with fixed size sprites

  • TextureAtlas: TexturePacker (Cocos2D format) compatible TextureAtlas. See http://www.texturepacker.com

Also the Sprite2D is maskable. You can set a mask via the setMask() method. Note that all color channels of the mask are used and you can even set the alpha of the mask to specify how much the Sprite2D is affected by your mask.

Grid2D - A Sprite2D that consists of more triangles. Use it to deform textures.

Font2D - A bitmap font object. Create fancy retro fonts with it.

Sprite2DCloud - Groups a number of the same sprites together, that share the same BitmapData, SpriteSheet or TextureAtlas. It's fast, you'll be able to display thousands of objects at a high framerate this way, but there are limitations: Object hierarchies won't work. The cloud just displays it's own children.

Sprite2DBatch - Similar to a Sprite2DCloud, but works different internally. In some cases slower than the cloud, in other cases (if you remove or add childs very often) faster and it's supporting object hierarchies.

ParticleSystem2D - A GPU accelerated, flexible particlesystem, able to render thousands of particles with nearly no CPU load. You cannot control each individual particle in the system, instead they're moving with settings you define. Use it to display high performance smoke, fire or water effects. If you want to have control over each particle, try to use a Sprite2DCloud or a Sprite2DBatch instead.

Camera2D - Every scene contains a reference to the worlds camera. The camera can be used to zoom and pan over your scene.

TextureRenderer - Renders a node and all subnodes in your scene into a texture. This way you can duplicate parts of your scene, add effects such as distortion, etc.

After you added some objects, get the world running by calling: start()

Moving things:

The game loop is triggered by an enterFrame. Each object in ND2D has a step() method that is called every time a frame is redrawn. The property ‘elapsed’ indicates how much time has passed since the last frame has been drawn. Since the loop is triggered with an enterFrame, you’ll have a variable timestep. This means that even if you set the framerate to 60, you can’t be sure that the step() method is called 60 times a second. For example if the CPU load is very high, the flash player will skip frames to make sure your system is still stable. That’s why the time between the last and the current call of step() will differ. Your movement should be based on “pixels per second” instead of “pixels per step” then. This way only the movement of the frames, that are displayed are being calculated, which could save you a bit of processor time.

override protected function step(elapsed:Number):void
{
    x += 10.0 * elapsed; // The object will move 10 pixel per second
}

So if 0.1 seconds have passed since the last step() call, your object will move just one pixel (10.0 * 0.1).

Blendmodes:

Blending on the GPU works a bit different, than in traditional flash. There are blendfunctions, that specify how a pixel that is already on the screen is blended against the pixel that should be drawn:

pixelColor = (sourcePixel * srcBlend) + (destinationPixel * dstBlend) 

For example we set the srcBlend to Context3DBlendFactor.ONE, the dstBlend to Context3DBlendFactor.ONE_MINUS_SOURCE_ALPHA, our source pixel is plain red and the destination pixel plain blue, which will result in a nice purple. Just what we expect when drawing a 0.5 alpha red over a blue pixel:

(1.0, 0.0, 0.0, 0.5) * 1.0 + (0.0, 0.0, 1.0, 1.0) * (1.0 - 0.5) = (0.5, 0.0, 0.5, 1.0)

In ND2D there are several built in blendmodes defined in ‘Blend-ModePresets’ that you can use.

Disposing / Memory Management:

Every ND2D object has a dispose() method. If you call this method, all Stage3D resources (GPU Textures, Shaders, etc.) that were allocated for this object are getting released. So if you need to free up some memory you can dispose() a whole scene if you don't need it anymore. Note that a Sprite2D becomes unusable in a scene after disposing, so be sure to remove it from the scene.

Extending the engine:

If you want to extend the engine and write your own effects and shaders go here: Writing-new-Materials-&-Shaders