Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

5. Game #4, Chain Reaction > Time for action — creating the atom elements

Time for action — creating the atom elements

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: The new method inside the game class will be named CreateAtomElements. Method CreateAtomElements:Int() 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() 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 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 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) 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. 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) Set the tag field of the atom to grpAtom. tile.SetTag(grpAtom) 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) Set their ID to 0. Later, we will store the current collCount value in it. tile.SetID(0) To touch the first element, we need to set its touch mode to bounding box touch hit check. tile.SetTouchMode(2) 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 Randomly determine the animation frame in a range from 0 to 3. Local f:Int = (Rnd(0,3)+0.4)+1 Set the animation time for each frame to 5 units. tile.SetAnimTime(5) Set the animation frame to the previously determined frame value f. tile.SetAnimFrame(f) 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) 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.

  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint