Skip to content

How is it Consumed?

debreuil edited this page Sep 14, 2010 · 4 revisions

While any symbol can be added to the stage, you will generally want to work with screens or V2dScreens when using Box2D. Screens map to ‘levels’ in a game. The have code to handle players and input, networking, the game loop, and each map to their own World in Box2D.

A screen can be created directly from a named symbol in the library, however named library symbols are always exported. Sometimes it is handy to turn off parts of the program and not have them run through the content pipeline. As an (recommended) alternative, you can layout each screen on its own frame in the root timeline, and give them instance names. This also makes it handy to view the different levels in Flash. The following syntax can be used to add a screen to your program:

stage.AddScreen(new TestScreen(new SymbolImport("SwfName", "TestScreenName"))); 

note this does not add it to the display list. The Stage has some screen management code that will handle which screen is the current one.

Screens can also have attribute to set various properties such as background color:

[V2DScreenAttribute(backgroundColor = 0x000001, gravityX = 0, gravityY = 60, debugDraw = true)]
public class TestScreen : V2DScreen{...}

Notice also that any instances in the screen Movieclip will be automatically mapped by instance name to fields or properties in your class. If the instance names end with digits, they will be mapped to arrays or Lists. So in this example, there are some movieclips called ‘balloon0’, ‘balloon1’ etc. Balloon0 becomes the first element in the balloon List. Also note how properties can be set on the attributes as well.

[V2DSpriteAttribute(friction = 0f, restitution=0f, mass=0f, density=0f, linearDamping=1f, angularDamping=1f)]
public List<V2DSprite> balloon;

This also holds for complex objects. If a movieclip contains multiple objects, and it is declared as a type, an object of that type will be created, and the internal movieclips will be mapped to fields in that class based on their instance names. Joints can also be added inside these movieclips, allowing the creation of reuseable Box2D machines – with specialized behaviour in the specific class or subclasses. Lastly, the same type can be used by various movieclips, as long as the instance names line up with what is expected. Eg a TrapDoor class could have many different graphical representations as long as each one contained a ‘door’ instance and a ‘latch’.

[V2DSpriteAttribute(isStatic=true, allowSleep = false, friction = .5f)]
public Treadmill[] treadmill;
Clone this wiki locally