Skip to content
nulldesign edited this page Sep 1, 2011 · 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
  • frameBased: Game loop fired by timer or enterFrame (details on the next page)
  • 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: Can be any size
    • SpriteSheet: A simple fixed size spritesheet
  • Grid2D - A Sprite2D that consists of more triangles. Used to deform images.

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

  • Sprite2DCloud - Groups a number of the same sprites together - TBD

  • Sprite2DBatch - TBD

  • ParticleSystem2D - A GPU accelerated, flexible particlesystem, able to render thousands of particles with nearly no CPU load

  • 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

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

Moving things:

If you initialized the world frameBased, the game loop is triggered by an enterFrame. If you set frameBased to false, the loop is triggered with a timer at the speed you defined. 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.

If the loop is triggered via a timer, the timestep is fixed and the timer is triggered 60 times a second, no matter if a frame is being rendered or not. This could be useful for physics based games based on Box2D for example. A basic movement could look like this:

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

If 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. If the load is very high, the flash player just skippes frames. 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 that you can use, defined in ‘Blend-ModePresets’.

Clone this wiki locally