Time for action — creating the main source file and its folders

At the time of writing this book, Monk didn't create the needed folder structure for you. So, you need to manually set up the folder for our project:

  1. First, create a new folder for the game; name it chainreaction.
  2. Inside this folder, create a data folder for all the resources with the name chainreaction.data.
  3. Next, copy all the resource files from the chapter's chainreaction.data folder into your .data folder.
  4. Now, also copy the source file of fantomEngine from the book's chapter folder into your project folder. As before, the name is fantomEngine.monkey.
  5. All that is left is to create a new empty script file and save it under the name chainreaction.monkey.
  6. An empty script doesn't do much; it won't even compile. Let's add a few lines.
    Strict
    #rem
    Script: chainreaction.monkey
    Description: Sample script from chapter #5 of the book
    "Monkey Game Development Beginners guide" by PacktPub
    Author: Michael Hartlef
    #end
    Import fantomEngine
    Global g:game
    

    It looks familiar, right? If not, then please study Chapter 4, Game# 3 Comet Crusher, where we did the same. What's next? Adding the game class to our file.

  7. Add a new class called game with the OnCreate, OnUpdate, and OnRender methods.
    Class game Extends App
    Method OnCreate:Int()
    Return 0
    End
    Method OnUpdate:Int()
    Return 0
    End
    Method OnRender:Int()
    Return 0
    End
    

    Remember, we want to utilize the OnSuspend and OnResume events.

  8. Add new methods for OnSuspend and OnResume, and close the class.
    Method OnResume:Int()
    Return 0
    End
    Method OnSuspend:Int()
    Return 0
    End
    End
    

    And like with every Monkey app, we need to add the Main function.

  9. Insert the Main function header.
    Function Main:Int()
    
  10. Create an instance of the game class, store it inside the variable g, and then close the function.
    g = New game
    Return 0
    End
    

    Note

    Your game class is there to set up the game and handle, depending on the game mode, the general update, and render process.

    To interface with fantomEngine, we will now create an instance of its ftEngine class.

  11. Add a new class called engine, which is an instance of the ftEngine class. Next, insert methods for its OnObjectTouch, OnObjectTransition, OnObjectCollision, and OnObjectTimer methods.
    Class engine Extends ftEngine
    Method OnObjectTouch:Int(obj:ftObject, touchId:Int)
    Return 0
    End
    Method OnObjectTransition:Int(transId:Int, obj:ftObject)
    Return 0
    End
    Method OnObjectCollision:Int(obj:ftObject, obj2:ftObject)
    Return 0
    End
    Method OnLayerTimer:Int(timerId:Int, obj:ftObject)
    Return 0
    end
    End
    

This is the basic structure of our game. Save the file and build it to see if any errors appear.

What just happened?

They say, just like history, Monkey game development repeats itself. We are once again interfacing with the fantomEngine; this is a huge timesaver.

Note

The data folder (which stores the images, sounds, and data files of your game) inside your source code folder has to be always named as follows:

yoursourcename.data

So, if your main source code file is named furryballs.monkey, then the data folder has to be named furryballs.data.

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

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