官术网_书友最值得收藏!

Hello World

Now that we have everything we need installed, we should make sure that Haxe, OpenFL, and HaxeFlixel are all up and running. To do this, we'll create a new project using the command line, run it, and then add an image to the screen to ensure that we can make changes.

Creating a project

To create a project, open up a command or terminal window and navigate to the folder in which you want your game's folder to be created.

After doing this, enter this command:

flixel tpl -n "HelloWorld"

This will create a new folder using the standard HaxeFlixel project template. It will also create project files for your code-editing tool of choice, allowing for quick integration out of the box.

Tip

If HaxeFlixel doesn't automatically open your project in your code-editing tool, open it in the tool manually. For FlashDevelop users, double-click on the .hxproj file in the project's folder. For Sublime Text users, drag your project's folder into an empty Sublime window.

A number of Haxe classes with the file extension .hx will be created. These are the base files you need to get a basic HaxeFlixel project up and running. We'll be going over the project's structure throughout the book, so it's not important to know what everything means right now.

Running the project

Now that we have a project set up, we can run it to see what we get out of the box.

If you're running FlashDevelop, select Debug from the drop-down list next to the play button and then select flash from the drop-down list next to that. After this, hit the play button to run your project.

For Sublime Text users, you can press Ctrl + Enter to run the project using the Haxe package that was installed earlier.

To create a build using the command line, navigate to your project's folder and enter this command:

lime test flash

The project should build and then open with your operating system's default application for running .swf files. When the .swf file loads, you'll briefly see the HaxeFlixel logo and then you'll be presented with a blank screen.

Copying assets

We're going to need the image that we want to display, so let's get that now. In the assets provided for this chapter, there is an image named HelloWorld.png. Copy it to the images folder under assets in your project's directory.

Making changes

Now that we have the image copied to the template project, let's add the traditional Hello World image to the screen.

Open MenuState.hx and navigate to the Create function. It will look like this:

override public function create():Void
{
  super.create();
}

After super.create();, add the following lines:

var helloWorldText = new FlxSprite();
helloWorldText.loadGraphic(AssetPaths.HelloWorld__png);
add(helloWorldText);

This will create a new variable of the FlxSprite class. It will then load in the image we just copied over, and then finally add the image to the screen. We'll go into more detail about how to use the FlxSprite class and work with loaded assets later, but that's a quick rundown of what's happening.

All that's left to do is to run the project to see your changes! In FlashDevelop, you just need to press the play button. For Sublime Text users, press Ctrl + Enter.

To create a build using the command line, enter the test command again:

lime test flash

That's it! You should see your SWF load and display the text Hello world!. If something along the way didn't work, take some time to review the chapter to see where things went wrong.

主站蜘蛛池模板: 富川| 泰顺县| 乌兰浩特市| 丽水市| 湘西| 临高县| 威远县| 乌兰浩特市| 鄯善县| 安乡县| 行唐县| 永济市| 临沂市| 德钦县| 柞水县| 南华县| 岗巴县| 英山县| 桐柏县| 长子县| 西乡县| 阿拉善左旗| 揭东县| 罗定市| 奈曼旗| 准格尔旗| 洛南县| 汤原县| 龙南县| 琼结县| 中宁县| 盱眙县| 竹北市| 东城区| 会理县| 禄丰县| 合阳县| 宿松县| 迁西县| 南靖县| 荃湾区|