Game Development Reference
In-Depth Information
This force will then translate to the puck when they collide. The
final method we
ll look at in this class is the reset function. This
will reset all of the moving bodies to their original coordinates:
exactly what you
'
'
d want to do after a player scores.
public function reset():void
{
mPlayer1.SetPosition( mPlayer1Origin );
mPlayer1Joint.SetTarget( mPlayer1Origin );
mPlayer2.SetPosition( mPlayer2Origin );
mPlayer2Joint.SetTarget( mPlayer2Origin );
mPuck.SetPosition( mPuckOrigin );
mPuck.SetLinearVelocity( new b2Vec2() ); // Stop puck in
case it is in motion
mConfig.mPlayer1.x = mPlayer1.GetPosition().x *
DISTANCE_RATIO;
mConfig.mPlayer1.y = mPlayer1.GetPosition().y *
DISTANCE_RATIO;
mConfig.mPlayer2.x = mPlayer2.GetPosition().x *
DISTANCE_RATIO;
mConfig.mPlayer2.y = mPlayer2.GetPosition().y *
DISTANCE_RATIO;
mConfig.mPuck.x = mPuck.GetPosition().x * DISTANCE_RATIO;
mConfig.mPuck.y = mPuck.GetPosition().y * DISTANCE_RATIO;
}
We use the origin positions we stored during setup to reset the
paddles and puck. We also force the velocity of the puck to 0 in
case it was moving when reset was called. Finally, we simply adjust
the display objects exactly the same way we did in the update
method.
Ready to Build
We
ve covered all of the code that is in this example. If
you have an Android device setup for debugging, you should be
able to plug it into your computer and publish this game directly
to it. You should notice very smooth motion and reaction to touch
input. There are definitely optimizations that would need to be
made for mobile if you were to start using lots of dynamic objects.
In fact, for mobile development Adobe recommends the use of a
different version of Box2D. It is actually the original C++ version of
the library, run through a piece of Adobe software called Alchemy.
Alchemy turns libraries of C and C++ code into Flash SWC files.
These SWC libraries can then be included in your projects and
tend to perform much faster than traditional ports. There is a
link to the alchemy port of Box2D on www.flashgamebook.com .
'
re done! We
'
Search Nedrilad ::




Custom Search