The wall commands within AGK provide a convenient mechanism for stopping physics entities from leaving the screen. This may not be suitable dependent on the kind of game being developed, for example, a scrolling game may not require any physics boundaries.
This example examines the wall commands and demonstrates how it's possible to control boundaries to the top, bottom, left and right of the screen. By default these boundaries are switched on, so in this example we'll turn the boundary off for the bottom of the screen and see how it affects our program.
The process involved is as follows:
An image is loaded, followed by a call to create a sprite, which is then positioned and finally has physics turned on setting its state to be dynamic:
LoadImage ( 1, "gold.png" )
CreateSprite ( 1, 1 )
SetSpritePosition ( 1, 150, 50 ) SetSpritePhysicsOn ( 1, 2 )
By default boundaries are provided for our scene, therefore at this particular point if we ran our program the result would be the crate falling to the bottom of the screen and hitting an invisible wall. To stop this behaviour and instead allow the crate to continue falling past the extents of the screen you can call the command SetPhysicsWallBottom. This command allows you to disable the invisible boundary at the bottom of the screen. It takes one parameter that controls whether the wall is on or off. A value of 0 switches the wall off, while a value of 1 switches it on. This line of code switches the bottom wall off:
SetPhysicsWallBottom ( 0 )
It's also possible to set the state of walls on other sides of the screen using these commands:
Given that our bottom wall has been switched off, running our program at this point will result in our crate falling to the bottom of the scene and straight off the screen.
For the example only the bottom wall has been turned off leaving the top, left and right walls in place. As a test we'll apply an impulse to our crate and force it over to the right of the screen. As it moves towards the right it will hit the invisible wall on this part of the screen and rebound. The crate will then fall downwards and as we have disabled the wall at the bottom of the screen it will continue falling downwards and off the screen. To achieve this the command SetSpritePhysicsImpulse is called, with the impulse being applied at the position of the sprite and on the X axis:
SetSpritePhysicsImpulse ( 1, 150, 50, 10000, 0 )
The main loop in this example does not take any action. All it needs to do is make a call to the command Sync to ensure the screen is updated.
do Sync ( ) loop
Everything is now in place. Here's the final code for our program:
SetVirtualResolution ( 320, 480 )
CreateSprite ( LoadImage ( "background2.jpg" ) )
LoadImage ( 1, "gold.png" )
CreateSprite ( 1, 1 )
SetSpritePosition ( 1, 150, 50 ) SetSpritePhysicsOn ( 1, 2 )
SetPhysicsWallBottom ( 0 )
SetSpritePhysicsImpulse ( 1, 150, 50, 10000, 0 )
do Sync ( ) loop
This example has helped us to understand the impact of switching the invisible physics walls off. Understanding how these boundaries work is important, as they may play a crucial part in your game.