(see also "How to use Auto Zoom in Grid and Pixel Games") . How to use Auto Zoom in Android Card Games Applications on smartphones (Apps) must display well on very different devices from small handheld phones to large tablets. This is a special challenge that can cause lot of work for the App programmer. Devices running Android Version 3 and up can use a build-in auto zoom feature. But often the graphics becomes blurred when zoomed by the OS, especially on these new big tablets with their superb screen resolution. JGameGrid provides an auto zoom feature that adapts sprite images automatically to the current screen resolution when the sprite image is loaded from the disk. JCardGame goes still a step further: Card and hand positions are also affected by the zoom factor which simplifies the layout of the card game considerably. During development a virtual coordinate system can be used that will be transformed to real (screen) coordinates depending on the current screen size at runtime. For many card games a square or rectangular game board is used that fits the screen. Therefore the constructor of a CardGame instances takes a layout enumeration that defines how the screen is used. The following BoardTypes are supported:
.
All card images and locations are automatically zoomed by a zoom factor calculated from the current screen size. Collision and touch areas are displaced and zoomed accordingly. The zoom factor may also be set by setZoomFactor(). A zoom factor 1 disables auto zoom.
In the following example we show how to use the HORZ_SPLIT type. (You could also use HORZ_FULL with a transparent board and border color and draw the background using standard GGBackground methods, eventually showing a nice logo on the game carpet.) A card talon is created at the left side of the card board. In the control area a button "Take" is displayed. When the button is clicked, the card on top of the talon is moved to the pile.. When the talon is empty, a status text is displayed. We locate the button in the center of the control area. We don't want the button size to be zoomed automatically, so it maintains its size even on small screens. To achieve this, we set the zoom factor manually to 1 before loading the button sprite. We save the old zoom factor and restore it after to avoid any side effects. For the locations of the button and the status text, we use virtual coordinates. The center is at vertical coordinate 300, but the horizontal coordinate must be calculated from getVirtualPgWidth() which returns the current virtual playground width. The virtual locations are transformed to real locations in the addActor() call. Remember that in JGameGrid for Android you should never instantiate an actor (a deck, a hand, etc.) before main() is started, because the internal initializing process is not yet executed at this moment. Of course you may declare them as instance variables and instantiate them in main().
(Enjoy how it is easy to animiate a card moving from one hand to another.) Execute the program locally using WebStart.
| |||||||||||||||||||||||||