Time for action — creating the atom elements

At the start of a game, or when you want to reset the game field, you will call this method to set up a new tile set:

  1. The new method inside the game class will be named CreateAtomElements.
    Method CreateAtomElements:Int()
    
  2. We want to randomize the grid setup. For this we will seed the random number generator with the value of a call to Millisecs.
    Seed = Millisecs()
    
  3. The distance from a collision circle to the center of an atom will be 23 pixels and stored inside a local variable.
    Local cd:Int = 23
    
  4. Now, we need a grid of 8 x 11 elements. Start two For loops for this purpose.
    For Local y:Int = 1 To 11
    For Local x:Int = 1 To 8
    
  5. Next, we determine randomly in the range of 0 to 3 the kind of element which represents the different kinds of atom connectors.
    Local ek:Int = (Rnd(0,3)+0.4)
    
  6. As we do not want the game to go on forever, we modify the ek value to a different one. This depends on another random number call with a range from 0 to 100.
    If ek = 2 And Rnd(0,100) > 20 Then ek = 3
    If ek = 1 And Rnd(0,100) > 80 Then ek = 0
    

    For a little bit of eye candy, we want to add some kind of sprite animation. Here, we will use the CreateAnimImage method of fantomEngine.

  7. Create a local anim object. The Y location on the sprite sheet depends on the ek value. For each sprite, we have four different anim images.
    Local tile:ftObject = eng.CreateAnimImage(atlas, 0 ,ek*32 ,32,32, 4, x*48+24, y*48+88)
    
  8. Set the tag field of the atom to grpAtom.
    tile.SetTag(grpAtom)
    
  9. As the actual images are a little bit small, scale them to 150 percent of their actual size. Then set the layer to layerGame.
    tile.SetScale(1.5)
    tile.SetLayer(layerGame)
    
  10. Set their ID to 0. Later, we will store the current collCount value in it.
    tile.SetID(0)
    
  11. To touch the first element, we need to set its touch mode to bounding box touch hit check.
    tile.SetTouchMode(2)
    
  12. Now create, depending on the element type (ek), some collision cycles at the positions of the atom element connectors:
    Select ek
    Case 0
    CreateColCircle(tile,x*48+24, y*48+88-cd)
    Case 1
    CreateColCircle(tile,x*48+24, y*48+88-cd)
    CreateColCircle(tile,x*48+24, y*48+88+cd)
    Case 2
    CreateColCircle(tile,x*48+24, y*48+88-cd)
    CreateColCircle(tile,x*48+24+cd, y*48+88)
    CreateColCircle(tile,x*48+24-cd, y*48+88)
    Case 3
    CreateColCircle(tile,x*48+24, y*48+88-cd)
    CreateColCircle(tile,x*48+24+cd, y*48+88)
    End
    
  13. Randomly determine the animation frame in a range from 0 to 3.
    Local f:Int = (Rnd(0,3)+0.4)+1
    
  14. Set the animation time for each frame to 5 units.
    tile.SetAnimTime(5)
    
  15. Set the animation frame to the previously determined frame value f.
    tile.SetAnimFrame(f)
    
  16. Again, calculate the random starting angle (0, 90, 180, and 270 degrees) and set it.
    Local a:Int = (Rnd(0,3)+0.4)
    tile.SetAngle(a*90.0)
    
  17. Close both For loops and the method.
    Next
    Next
    Return 0
    End
    

What just happened?

We just created two methods that let us dynamically build a grid of atom elements and their corresponding child collision detectors.

Starting a new game

At one or two points in the game, we may want to (re)start the game—when you hit the PLAY button on the title screen and when you hit the RESET button on the game screen.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.136.17.12