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

As this is the first time, we will work with the resources files and will need to set up the folder of our project in a slightly different manner:

  1. Create a new folder for the game, and give it the same name as your main source file.
  2. Then inside that folder, create another folder with the name of your main source file and the extension .data.
  3. Copy all the resource files from the chapter's cometcrusher.data folder into your .data folder.
  4. Copy the source file of fantomEngine from the chapter's folder into your project folder. Its name is fantomEngine.monkey.
  5. Create a new, empty script file and save it under the name you have chosen.

    Inside the new script add the following lines; by now you will know them pretty well.

    Strict
    #rem
    Script: cometcrusher.monkey
    Description: Sample script from chapter #4 of the book
    "Monkey Game Development Beginners guide" by PacktPub
    Author: Michael Hartlef
    #end
    Import fantomEngine
    
  6. As you can see, we didn't import the Mojo framework; it is already imported by fantomEngine.
  7. Next, create a global variable called g, which holds an instance of our main class called game.
    Global g:game
    

    Then add the game class to our source file.

  8. Create a new class called game with 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
    End
    

    The last thing that is needed for a valid Monkey app is the Main function.

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

    fantomEngine supports different object, layer, and timer-related event methods. To fill them with life, and of course use fantomEngine, we need to create an instance of the ftEngine class.

  11. Create a new class called engine, as an instance of the ftEngine class. Inside it, create methods for its OnObjectCollision, OnObjectTimer, OnObjectUpdate, and OnLayerUpdate methods.
    Class engine Extends ftEngine
    '------------------------------------------
    Method OnObjectCollision:Int(obj:ftObject, obj2:ftObject)
    Return 0
    End
    Method OnObjectTimer:Int(timerId:Int, obj:ftObject)
    Return 0
    End
    Method OnObjectUpdate:Int(obj:ftObject)
    Return 0
    End
    Method OnLayerUpdate:Int(layer:ftLayer)
    Return 0
    end
    End
    

That's it. Save your file and if you like, let it build to see if any error shows up. Remember, save save save!

What just happened?

We have seen some basic stuff that repeats itself in every Monkey app; plus interfacing with the fantomEngine. These methods are there, so we can act on different engine events.

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

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