Contents |
|
Creating a Game Loop | Extending the HTML |
This tutorial demonstrates how to create responsive layouts using JavaScript. Add two boxes to the library. Make one red and one green. These boxes are just examples. Any shapes or objects could be used.
Add the boxes to the editor.
Arrange the boxes, so they fill the left and right-hand sides. Leave a border around them.
This is now an example of a web page with content on the left and right-hand sides. It will look fine on a desktop web browser or a phone in landscape mode. Unfortunately, this layout won't work well in portrait mode. Either, the two boxes must be shrunk small to fit, or expanded to fill the screen, and the sides of the boxes cut off.
This situation is where responsive layouts are needed. In portrait mode, one box should move above the other. To do this, we need to move the boxes using JavaScript. This means turning off 'Use Timeline' for both boxes and setting a 'Script ID'.
In order to move the boxes outside the bounds of the movie, we need to turn off clip in the movie properties.
The best place to arrange the boxes is in the 'On Resize' movie event.
The functions we need are GetLeft, GetTop, GetWidth and GetHeight. These functions give the bounds of the web browser or device using the coordinates of the movie. When clip is turned on, these values return the bounds of the movie. If you are using the 'None' movie scale, GetLeft and GetTop will always return 0 and can be ignored.
First lets arrange the two boxes so one fills the left-hand side and the other fills the right-hand side.
Next, we will add a border of size 10. This value of 10 is not necessarily in pixels, it is in the same scale of the movie. Use the movie scale 'None' for everything to be in pixels.
Finally we will test to see if the width of the web browser or device is less than the height. When that happens, rearrange the boxes vertically.
Now you can see, if you resize the player, the two boxes will arrange themselves automatically. These two boxes can easily be replaced with other shapes and objects. This example can be expanded to handle more complex layouts.
Creating a Game Loop | Extending the HTML |