tag:blogger.com,1999:blog-86109838818395773802018-09-06T00:27:19.161-07:00HaXe Game DevWilliam Thompsonnoreply@blogger.comBlogger2125tag:blogger.com,1999:blog-8610983881839577380.post-59547969347526364242014-02-13T12:38:00.001-08:002017-03-21T16:41:18.534-07:00Getting Setup<h1>What We Use</h1>This post is intended as a guide to help new users of HaXe get setup with HaXeFlixel and understand how the HaXeFlixel demos are setup. Hopefully this will explain some of the curious things you might see in the demo files and help you prepare to make use of them!<br /><br />Technologies used in this post:<br /><ul><li>FlashDevelop</li><li>HaXe</li><li>HaXeFlixel</li></ul><h2>A Note About Terminology</h2>I've bounced around through enough programming languages in my life that I use similar terms in different languages interchangeably. You'll notice sometimes I call them methods, sometimes I call them functions. Hell, I'll even call them subroutines if I'm feeling froggy. I use the terms package and library interchangeably too. Please excuse me if I use terms wildly and freely. I promise I'll use a term that's appropriate for some language somewhere most of the time. It entirely depends on what's going through my head at any given moment and what randomly pops out.<br /><br /><h3>Install Needed Tools</h3>Setup FlashDevelop first since that's our IDE. Here are the instructions to download and install FlashDevelop, HaXe, and HaXeFlixel:<br /><br /><a href="http://www.flashdevelop.org/community/viewforum.php?f=11" target="_blank">Get FlashDevelop</a><br /><a href="http://haxe.org/download" target="_blank">Get HaXe</a><br /><a href="http://haxeflixel.com/download/" target="_blank">Get HaXeFlixel</a><br /><br />We install HaXeFlixel so we have a standard, well-developed game framework. OpenFL gets installed as a dependency of HaXeFlixel. OpenFL is an excellent cross-platform software aimed at using graphics hardware instead of loading all the work on the CPU (this works well with mobile platforms such as iOS, Android, and Blackberry). It's an excellent tool for developing high performance Flash games too because it mimics how Stage3D works. It's also a great tool for developing desktop applications aimed at Windows, Mac, and Linux because it's essentially an open port of OpenGL. This combination of technologies is like the Swiss army knife of software development!<br /><br /><h3>Installing Community Libraries</h3>There are a large number of well developed <a href="http://lib.haxe.org/" target="_blank">HaXe Community Libraries</a> available through haxelib. Some of the demo's on HaXeFlixel depend on community libs. Let's install some of those libs since you will need them to compile the HaXeFlixel demos. Run these commands from a command prompt:<br /><br /><span style="font-family: monospace;">haxelib install flixel</span><br /><span style="font-family: monospace;">haxelib install systools</span><br /><span style="font-family: monospace;">haxelib install task</span><br /><span style="font-family: monospace;">haxelib install nape</span><br /><span style="font-family: monospace;">haxelib install openfl</span><br /><span style="font-family: monospace;">haxelib install lime-tools</span><br /><span style="font-family: monospace;">haxelib install lime</span><br /><span style="font-family: monospace;">haxelib install flixel-tools</span><br /><span style="font-family: monospace;">haxelib run lime setup</span><br /><span style="font-family: monospace;">haxelib run flixel-tools setup</span><br /><div><code><br /></code></div>These are libs used in the HaXeFlixel demos. Install these so they're available in case you want to use one of the demos as a base for a project (I do this a lot).<br /><br /><h3>Installing Packages from Git</h3>At some point you'll want to install a package that's not a HaXe community lib. One of the demos on the HaXeFlixel site (<a href="http://haxeflixel.com/demos/RPGInterface/" target="_blank">RPG Interface</a>) uses a package called firetongue; this is not available as a community lib. Here's the syntax to install a git repo lib:<br /><br /><code>haxelib git package-name git-url</code><br /><br />I'm not saying you need to install this package but this is how you would install firetongue from git if you want it. You need to <a href="https://help.github.com/articles/set-up-git" target="_blank">install the git client</a> to do this (run git commands from the <b>Git Shell</b> on Windows, not the command prompt!):<br /><br /><code>haxelib git firetongue https://github.com/larsiusprime/firetongue</code><br /><br />As an alternative to installing a package you can download it and place it directly in your project's source folder. If you do this and your project config file calls for the package to be installed you'll need to comment that line out. In the root of your FlashDevelop HaXe project folder there's a file called <b>Project.xml</b>:<br /><br /><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;<br />&lt;project&gt;<br />&lt;app title="..."&gt;<br />...<br />&lt;haxelib name="flixel"/&gt;<br /><s>&lt;haxelib name="firetongue"/&gt;</s><br />&lt;!--haxelib name="firetongue"/--&gt;</code><br /><br />You only need to do this if you put the library directly in the source folder instead of installing it.<br /><br /><h3>Update to the Latest Version</h3>We want to use the latest version of HaXe, HaXeFlixel, and all our HaXe libraries. For anyone who's worked in a Linux/Unix environment this will look really familiar. Run this command from a command prompt (if you install libs from Git you'll need to use the Git Shell):<br /><br /><code>haxelib upgrade</code><br /><br />Now our libraries are up to date. Let's update haxelib (the HaXe package manager):<br /><br /><code>haxelib --global update haxelib<br />haxe upgrade.hxml</code><br /><br />We're all up to date! For a list of haxelib commands you can type <code>haxelib</code> from the command prompt. You can also view the <a href="http://haxe.org/doc/haxelib/using_haxelib" target="_blank">haxelib reference page</a>.<br /><br /><h1>HaXeFlixel Demo Breakdown</h1><br />Now that we're setup, let's look through the basic structure of a HaXeFlixel demo to get familiar with what's going on. In this section you'll see snippets of code in HaXe. Where appropriate I'll explain how they relate to other languages like AS3, Java, and C++.<br /><br />The structure for all the HaXeFlixel demos is similar so it doesn't matter which one you choose. Pick one you like and open it with FlashDevelop. I chose <a href="http://haxeflixel.com/demos/FileBrowse/" target="_blank">FileBrowse</a> because it's fairly simple and small. Start by opening <b>Main.hx</b>: <br /><br /><code>package;<br />...<br />import flash.Lib;<br />...<br />class Main extends Sprite<br />{<br />// Entry point<br />public static function main():Void<br />{<br />Lib.current.addChild(new Main());<br />}<br />...<br />}</code><br /><br />The <i>main()</i> method is the entry point where our program begins execution. The line <code>Lib.current.addChild(new Main());</code> is an extension of the flash package. If you didn't want to include the flash package you would rewrite this as:<br /><br /><code>flash.Lib.current...</code><br /><br />This line adds an instance of the <i>Main </i>class as a new object to the Flash stage. For those familiar with AS3 this will make sense. For those familiar with C++ and Java, the Flash stage is a display object handler with a lot of useful features for manipulating images and vector graphics. This is a required component for all HaXeFlixel applications. You should get familiar with how this works in AS3 because you'll use it a lot.<br /><br />You might find yourself asking, "if the entry point of my program starts in the<i> main()</i> function, does that mean making a new object from the <i>Main</i> class will call <i>main()</i> again?" This is a legitimate question and it confused me at first. Java behaves something like this so if you come from a Java background you likely just asked yourself that question.<br /><br />With HaXe, you write the <i>main()</i> method only for the class where program execution begins. You can write a main method for other classes but it'll never run unless you explicitly call it. When you make a new object from a class in HaXe it executes the <i>new()</i> function. Let's look at that:<br /><br /><code>class Main extends Sprite<br />{<br />...<br />public function new()<br />{<br />super();<br />if (stage != null)<br />{<br />init();<br />}<br />else<br />{<br />addEventListener(Event.ADDED_TO_STAGE, init);<br />}<br />}<br />...<br />}</code><br /><br />This is what executes when we add <i>Main</i> to the stage as a new object. In the if block it checks to make sure the stage exists first (this is based on Flash behavior). In Flash, it's not guaranteed the stage will exist when you add something to it. Extremely odd idea but that really is how it works. The else block adds an event listener to the stage that fires the function <i>init()</i> once the stage does exists.<br /><br />If you're not familiar with event handlers in Flash you can think of them similar to throwing an exception that will not stop execution or cause an error; the stage throws the exception and all child objects receive a copy. If a child of stage throws an object then it will bubble up through all parent objects including the stage. The event is handled silently by the runtime environment so it doesn't stop execution. It just throws an information object out for anyone listening so they know when something happens. There are loads and loads of different events in Flash. You can even extend the Flash native Event class to create your own custom events. Events in Flash are somewhat slow but can be useful at times. I stay away from events because they can cause serious memory leaks if not handled carefully.<br /><br />Let's look through the init function:<br /><br /><code>private function init(?E:Event):Void<br />{<br /> if (hasEventListener(Event.ADDED_TO_STAGE))<br />{<br />removeEventListener(Event.ADDED_TO_STAGE, init);<br />}<br /> initialize();<br /> var game:FlxGame = new GameClass();<br />addChild(game);<br />}</code><br /><br />This method is fairly straightforward so I'll focus on the critical pieces. You probably noticed the <i>?E:Event</i> as part of the function declaration and wondered what the heck that means. Adding a ? before a variable as part of a method declaration makes it optional; very useful for cases where you don't care if something gets passed or not. This saves creating a null object as a default value for a variable you don't care about and don't need. Good practice to use when you can.<br /><br />The <i>initialize()</i> method is fairly simple and easy to understand. It sets display characteristics so I won't bother explaining it.<br /><br />The variable <i>game</i> has a type of <i>FlxGame</i> and stores a newly created <i>GameClass</i>() object. It then gets added to the stage after it runs it's constructor. This is where we're going next. Open the file <b>GameClass.hx</b>:<br /><br /><code>...<br />class GameClass extends FlxGame<br />{<br />public function new()<br />{<br />var stageWidth:Int = Lib.current.stage.stageWidth;<br />var stageHeight:Int = Lib.current.stage.stageHeight;<br />var ratioX:Float = stageWidth / 640;<br />var ratioY:Float = stageHeight / 480;<br />var ratio:Float = Math.min(ratioX, ratioY);<br />var fps:Int = 60;<br />super(Math.ceil(stageWidth / ratio), Math.ceil(stageHeight / ratio), PlayState, ratio, fps, fps);<br />}<br />}</code><br /><br />This whole class consists of setting up the display, frame rate, and passing a class name to the Flixel engine via extending the <i>FlxGame</i> class and calling <i>super()</i>. For those familiar with AS3 this would be a nightmare to get working because you'd need to call <i>super()</i> as the very first line of the constructor. You can imagine trying to do all of that math crunching on one line inside the <i>super()</i> parenthesis... I don't want to imagine what that would look like. Thankfully you don't have that superficial restriction in HaXe; you can call <i>super()</i> anywhere in the constructor.<br /><br />Now that we've gotten this far we have a Flixel engine setup and it has the class name <i>PlayState</i> passed to it. Let's open <b>PlayState.hx</b> and see what we find:<br /><br /><code>...<br />class PlayState extends FlxState<br />{<br />... variable declarations<br />override public function create():Void<br />{<br />...<br />}<br />}</code><br /><br />Whoa, buddy! Where's the <i>new()</i> function? Flixel does create a new <i>PlayState</i> object but does not require an override constructor method. Instead, it relies heavily on <i>create()</i> and <i>destroy()</i> so it can run object pools! I'll explain object pools in another post but for now understand this allows an object to be reused. Mobile platforms and Flash take a lot of CPU power to make new objects so it's best practice to create many up front and use them over and over. Object pools work well for desktop applications because you don't make a new object every time you need it; you ask the object pool for one that already exists. Object creation is automatically handled, memory space already allocated, and you don't end up with as many weird memory bloat issues when you use object pools.<br /><br />This is where we stop for now because going further requires explaining how the Flixel camera system works and more about the Flixel framework in general. I have a series of posts planned to show you how to make a platformer infinite runner style game later so you'll just have to wait and see. If you want some further reading I would suggest these links:<br /><br /><a href="http://haxe.org/doc">http://haxe.org/doc</a><br /><a href="http://api.haxeflixel.com/">http://api.haxeflixel.com/</a><br /><a href="http://haxeflixel.com/documentation/handbook/">http://haxeflixel.com/documentation/handbook/</a>William Thompsonhttps://plus.google.com/110242405364671556350noreply@blogger.com0tag:blogger.com,1999:blog-8610983881839577380.post-45830284098778541252014-02-11T06:53:00.000-08:002014-02-16T05:20:38.893-08:00IntroductionsI've written software since my preteen years but I'm new to game development. I started working on an RPG in AS3 (Flash) as my first real game project but I've divert to other things along the way. Tutorials exist all over the Internet in different programming languages, styles, etc., and they're written for all skill levels. This is my set of tutorials and code snips.<br /><br />I've settled on HaXe as my go-to language because it's free, open-source, and easily available on all major platforms. It also compiles easily for a number of popular platforms (iOS, Android, BlackBerry, Windows, Mac, Linux, et al) using a huge number of IDEs, my preference being FlashDevelop. This is a place to keep a small piece of my sanity and a record of concepts I'm working through in HaXe.<br /><br />I don't pretend to know everything, nor do I pretend I'm the best at those things I discuss (or even good!). I work with logic more than code so my style will be different than yours. I am not a professional blogger, I work full-time and write games in my spare time. I do things half asleep because I have a wife and kids so I make mistakes too!<br /><br />I'm not the best graphic artist or musician so I rely heavily on those things provided by <a href="http://opengameart.org/">OpenGameArt.org</a>. They provide free, open-source, and nicely licensed content that's easy to use. I'm also friends with several of their members so you'll see their names here.<br /><br />For those who need a bullet style list to understand what technologies I'm using:<br /><br /><ul><li>FlashDevelop</li><li>HaXe</li><li>HaXeFlixel </li><li>Coffee!</li></ul><br />Let the game development blog begin!William Thompsonhttps://plus.google.com/110242405364671556350noreply@blogger.com0