Android Tutorial GameView - Create 2D Android games - Part II

Discussion in 'Tutorials & Examples' started by Erel, Aug 1, 2012.

  1. Erel

    Erel Administrator Staff Member Licensed User

    The first part of this tutorial can be found here:

    GameView features were described in the first part. GameView handles only the drawings. The logic and sprite management are done in Basic4android code.

    The attached project includes several classes that you can use in your game to help with common tasks:

    SpriteAnimator - Every sprite should have a SpriteAnimator... SpriteAnimator is responsible for moving the sprite and changing the current frame.

    CollisionDetector - A class that is added to a sprite that needs to detect collisions with other sprites. For example in the asteroids game the ship and missile classes each use a CollisionDetector object to detect collisions with the asteroids.

    Both SpriteAnimator and CollisionDetector have a Tick method which you should call on each tick.

    GamePad - A multitouch game pad implementation made of two joysticks. This is an improved version of a previous GamePad implementation. It works with GameView and provides better performance.

    GameUtils - This is a code module with currently two features: scale calculation and a method to load sprite sheets.

    These classes should be a good base for further customizations as needed.


    The asteroids game example demonstrates the usage of these classes.
    The game is quite simple. You move the ship with the left joystick and shoot the asteroids with the right joystick. Hitting an asteroid will crash the ship.

    This project depends on three libraries: Audio, GameView and Gestures.
    It should scale correctly on all devices.
    As it is based on GameView is works on Android 3.0 or above. It can work on lower versions without hardware acceleration. The minimum target version in the manifest editor was set to Android 3.0.

    Please feel free to ask any question about the code.

    Asteroids sprites - XNA Development: Game Development for the masses
    Sounds - Crash Sounds | Free Sound Effects | Crash Sound Clips | Sound Bites

    Attached Files:

    Last edited: Mar 16, 2014
  2. andymc

    andymc Well-Known Member Licensed User

    This is brilliant work, thanks Erel!

    Another good step in the direction for B4A to be used for fast moving games as well as apps.
    Kwame Twum likes this.
  3. Erel

    Erel Administrator Staff Member Licensed User

    Thank you andymc. I recommend all developers who are interested in building 2d games to download this one. The performance of GameView is really great. The movement is very smooth.
  4. Vabzboy

    Vabzboy Member Licensed User

    How to end Gameview

    How do I end this Gameview.

    What is the Code for it??
  5. Erel

    Erel Administrator Staff Member Licensed User

    What do you mean with "end this Gameview"? Nothing will happen if you won't call GameView.Invalidate.
  6. CapReed

    CapReed Member Licensed User

    Differents routes for differents sprites

    Hi all,

    I'm dissecting this example to learn how to manage the sprites. I would be very useful to have controlled each of the sprites that are "alive", for example, to give one of the asteroids and a distinct personality that moved in a continuous motion elliptical. Actually in this case would not make much sense, but it would be very useful for example to control small groups of soldiers shaped sprites or warehouses. Do you remember the Galaxian and as alien ships fell with a certain path?

    I think that would be necessary for both. First have controlled what the sprite is going to have a given path, and second, to know how to tell the program the "equation" of the trajectory.

    How could indicate different paths to each piece of asteroid? Could you put a small example that illustrates this?

    Thank you!
  7. Erel

    Erel Administrator Staff Member Licensed User

    SpriteAnimator is responsible for the sprites movement. You can create a copy of this class that moves the sprites in a different way and assign it to one of the asteroids.
  8. CapReed

    CapReed Member Licensed User

    Ok, I will try that.

  9. CapReed

    CapReed Member Licensed User

    Sorry to insist, but how could have identified each of the live objects? Could at all times know that spriteanimator sprite is being processed? It may be a silly question ... but do not see it.
  10. TheJinJ

    TheJinJ Active Member Licensed User

    From memory the asteroids in the demo are held in a list so you have asteroids 0-4. Each one is passed to the sprite animator that adjusts its position. You could pass another value to the animator and use that with a select to have different movement for objects.

    Sent from my GT-I9300
  11. Erel

    Erel Administrator Staff Member Licensed User

    There all kinds of possible solutions. You can add a "name" field to the Asteroids class or you can use a Map instead of list.
  12. CapReed

    CapReed Member Licensed User

    Thank you both, with your advice I have made ​​progress in the game I'm programming.

    Thank you!
  13. CapReed

    CapReed Member Licensed User

    TextLabel in GameView


    How could I put a small text beside each asteroid that could put, for example, the X and Y position in which he is, or anything else, for example the number of impacts that have received?

    I think I have no trouble controlling both the X and the Y or the number of impacts, but do not know how to put a text in a specific position within a GameView and it can move freely.

    Thank you.
  14. Erel

    Erel Administrator Staff Member Licensed User

    You can add additional sprites that will be made of bitmaps that hold the text.
  15. Armoured

    Armoured Member Licensed User

    I have this error when I try to compile this example:

    Compiling code. Error
    Error parsing program.
    Error description: Unknown type: gestures
    Are you missing a library reference?
    Occurred on line: 3
    Private ges As Gestures

    The Gesture library is correctly installed.
    How I can fix this error?
  16. Erel

    Erel Administrator Staff Member Licensed User

    Make sure that the Gestures library is checked in the libraries tab.
  17. Armoured

    Armoured Member Licensed User

  18. Peter Simpson

    Peter Simpson Expert Licensed User

    Good stuff

    I'm going to try and implement the Game View library into a game shortly. I just need to improve on my gfx making skills in Photoshop and away we go.

    I promise to try and not ask too many questions.

    Thanks for the library Erel, and to everyone for the support in advance...
  19. andymc

    andymc Well-Known Member Licensed User

    Hi Erel,

    do you have plans to expand these classes? Such as adding new layouts to the gamepad class for having say one joystick and then some firebuttons, instead of just two sticks?
  20. Erel

    Erel Administrator Staff Member Licensed User

    It shouldn't be too difficult to modify the GamePad class code and change one Joystick to 4 buttons. Try to modify it and if you need any assistance then start a new thread and I'll help you.
    Kwame Twum likes this.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice