For new games it is recommended to use libGDX. See these tutorials:
How to make games
[URL='http://www.b4x.com/android/forum/threads/32592']Introduction to the libGDX library[/URL]
GameView is a view that allows you to draw hardware accelerated graphics. Compared to software accelerated graphics, hardware accelerated graphics are many times faster. Using hardware accelerated graphics it is possible to create smooth, real-time games.
Note that the acceleration method used by GameView is only available from Android 3.0 and above. This also means that you need to reference android.jar from platform 11 or above (under Tools -> Configure paths).
You should add this line to the manifest editor:
It is important to understand how GameView works.
GameView holds a list of BitmapData objects. Each BitmapData object holds a reference to a bitmap and some parameters that tell GameView how to draw it.
When GameView redraws itself, it goes over the list of BitmapData objects and draws each one of them. The important thing about GameView is that the drawings are hardware accelerated.
In order to make GameView redraw itself you should call GameView.Invalidate.
BitmapData
Each BitmapData object represents a bitmap (or sprite) that will be drawn when GameView redraws itself.
BitmapData properties are:
Typical game structure
Usually your game should consist of a single main timer. All the movements and the logic should happen in this timer's tick event. Eventually you call GameView.Invalidate. This will cause GameView to redraw itself after your code execution completes.
Simple bouncing smiley with a scrolling background
The attached project is a simple example with two BitmapData objects. One is the background and the other is the moving smiley. The background is scrolling to the left each tick. This is done by playing with SrcRect values so each part of the wide background image is draw each time.
The smiley DestRect is modified every tick to make it move.
The second part of this tutorial with a working "Asteroids" game is available here: http://www.b4x.com/forum/basic4andr...gameview-create-2d-android-games-part-ii.html
Another example (jumping smiley): http://www.b4x.com/forum/basic4andr...ngsmiley-gameview-example-iii.html#post151177
How to make games
[URL='http://www.b4x.com/android/forum/threads/32592']Introduction to the libGDX library[/URL]
GameView is a view that allows you to draw hardware accelerated graphics. Compared to software accelerated graphics, hardware accelerated graphics are many times faster. Using hardware accelerated graphics it is possible to create smooth, real-time games.
Note that the acceleration method used by GameView is only available from Android 3.0 and above. This also means that you need to reference android.jar from platform 11 or above (under Tools -> Configure paths).
You should add this line to the manifest editor:
B4X:
SetApplicationAttribute(android:hardwareAccelerated, "true")
It is important to understand how GameView works.
GameView holds a list of BitmapData objects. Each BitmapData object holds a reference to a bitmap and some parameters that tell GameView how to draw it.
When GameView redraws itself, it goes over the list of BitmapData objects and draws each one of them. The important thing about GameView is that the drawings are hardware accelerated.
In order to make GameView redraw itself you should call GameView.Invalidate.
BitmapData
Each BitmapData object represents a bitmap (or sprite) that will be drawn when GameView redraws itself.
BitmapData properties are:
- Bitmap - The bitmap that will be drawn.
- DestRect - A rectangle that defines the location and size of the drawn bitmap. For example to move a sprite you change DestRect values.
- SrcRect - A rectangle that defines the bitmap's region that will be drawn. You can pass an uninitialized rectangle if you want to draw the complete bitmap. SrcRect can be useful for drawing a sprite from a sprite sheet, or to create scrolling effects.
- Delete - A boolean value. When set to True, GameView will remove this BitmapData from the list during the next drawing (the bitmap will not be drawn).
- Rotate - Number of degrees to rotate the bitmap.
- Flip - Flips the bitmap based on one of the FLIP constants.
Typical game structure
Usually your game should consist of a single main timer. All the movements and the logic should happen in this timer's tick event. Eventually you call GameView.Invalidate. This will cause GameView to redraw itself after your code execution completes.
Simple bouncing smiley with a scrolling background
The attached project is a simple example with two BitmapData objects. One is the background and the other is the moving smiley. The background is scrolling to the left each tick. This is done by playing with SrcRect values so each part of the wide background image is draw each time.
The smiley DestRect is modified every tick to make it move.
The second part of this tutorial with a working "Asteroids" game is available here: http://www.b4x.com/forum/basic4andr...gameview-create-2d-android-games-part-ii.html
Another example (jumping smiley): http://www.b4x.com/forum/basic4andr...ngsmiley-gameview-example-iii.html#post151177
Attachments
Last edited: