A1 Haxe 2.0 Basics | Source Code - kadedevteam/Kade-Engine-Offical GitHub Wiki

Haxe 2.0 Basics

HaxeFlixel Cheat Sheet: (Where most info was found!)

Index!

Declaring Varaibles

To declare a variable just write var name-of-the-variable. It can't use spaces so be careful. Variables are like containers they are used to store numbers, text and other important stuff.

In Haxe you need either say which type of variable it is with :Type

Basic Types are:

  • String - stores text, such as "Hello". String values are surrounded by quotes.
  • Int - stores integers (whole numbers), without decimals, such as 123 or -123.
  • Float - stores floating point numbers, with decimals, such as 19.99 or -19.99.
  • Bool - stores values with two states: true or false.

These are just Basic Types there are multiple different ones you can use from HaxeFlixel which is the game engine. You can see here some of the stuff you can use like sprites. Just copy the name of the class when declaring the type of variable.

Functions

The purpose of a function is to help you organize your code and to avoid writing the same code twice. You can you define a function once, and then call the function a number of times.

You can add a function put the code block inside and call it whenever you want.

function name-of-function():Void
{
	// code block
}

Calling the function to run the code works like this name-of-function();

Just make sure you add it outside of other functions {} or else you won't be able to call it everywhere.

Classes

If's and Classes are used as logical conditions to run code only if a condition is met.

Here are the conditions that exist.

  • Less than: a < b
  • Less than or equal to: a <= b
  • Greater than: a > b
  • Greater than or equal to: a >= b
  • Equal to a == b
  • Not Equal to: a != b
  • And: &&
  • Or: ||
if (condition1) {
  // block of code to be executed if condition1 is true
} else if (condition2) {
  // block of code to be executed if the condition1 is false and condition2 is true
} else {
  // block of code to be executed if the condition1 is false and condition2 is false
}

Class statements are used to select one block of code to be executed. It uses an expression and goes directly to the code block that matches the expression. For example if the expression is the song name, in case x the x is songname1 and in case y the y is songname2. If you play with songname1 only the code block inside the case songname1 will run, same if you play songname2. Default is when none of the cases match the switch expression.

FlxSprites

new FlxSprite(x, y) Creates a new sprite at a specified position.

Png Only:

.loadGraphic(Paths.image('folder/png-name')) Loads/stores an image to the sprite. Attention this is only for png's. If none are provided the sprite loads a 16x16 HaxeFlixel logo. The folder isn't necessary so if you don't want it just delete the folder/ part.

.makeGraphic(FlxG.width * 2, FlxG.height * 2, FlxColor.BLACK); Creates an image. FlxG.width * 2 is the width, FlxG.height * 2 is the height and FlxColor.BLACK is the color of the image.

.antialiasing Controls whether the object is smoothed when rotated, affects performance.

.scrollfactor.set(x, y) Controls how much this object is affected by camera scrolling. 0 = no movement (e.g. a background layer), 1 = same movement speed as the foreground.

.active Controls whether update() is automatically called.

.setGraphicSize(Std.int(bg.width * 0.8)) Set the graphic's dimensions by using scale, allowing you to keep the current aspect ratio. Change 0.8 to the number. It might make sense to call updateHitbox() afterwards!

.screenCenter(); Center the sprite in the center of the screen.

add(FlxSprite); Adds the sprite to the front of the "layers" so if you want to alter the layer order just add them by order.

remove(FlxSprite); Removes the sprite that was added before.

FlxTween

FlxTween is when a FlxSprite gets a animation using sheets and xmlFiles!

.frames = Paths.getSparrowAtlas('folder/name-of-sprite-sheet'); Stores an sprite sheet to the sprite. Attention this is only for animations. The folder isn't necessary so if you don't want it just delete the folder/ part. The name of the sprite sheet needs to match with the corresponding xml.

.animation.addByPrefix('idle', 'name of animation in xml', 24, false); Add/stores an animation by prefix. This means it adds an animation from the xml. 'idle' is the new name of the animation that is used to play it and other stuff. "name of animation in xml" is the name of the animation in the xml that is being added. 24 is the framerate and false is if it's being looped or not. You can as many animations as you want as long as you have them in the xml.

.animation.play('idle'); Plays the animation that was added. Just change 'idle' if you gave it another name. Also you might want to think about which place in the code will play this animation.

If the animation is a character it's not .animation.play('idle'); but instead it's .playAnim('idle');

Like in the screenshot above if you want to force an animation to play do .animation.play('idle', true); or if it's a character do .playAnim('idle', true);

For example this will tween the boyfriend sprite to x = 500, y = 500, it will rotate it 40 degrees, the opacity/transparency (alpha) to 50% (0.5) and the width of the sprite to 500. 1 is the seconds the tween takes from start to finish in this case it takes 1 second.

Then it will use the ease option that was chosen (use the demo above to pick one) and when the tween completes it will run that code block inside the {} in this case it will change the boyfriend width to 250 when the tween finishes.

FlxTimer

FlxTimer is used to start a timer. It can be used to run code after the timer finishes so you can have more control.

.start(5, function(tmr:FlxTimer){}); Starts the timer. 5 is the seconds the timer takes to complete (in this case is 5 seconds). function(tmr:FlxTimer){}); is the what is used when the timer completes, just put the code inside the {} like in the example above.

.start(5, function(tmr:FlxTimer){}); Starts the timer. 5 is the seconds the timer takes to complete (in this case is 5 seconds). function(tmr:FlxTimer){}); is the what is used when the timer completes, just put the code inside the {} like in the example above.

FlxTrail

FlxTrail is used to make trails. You can see them in the demo for FlxTweens above.

dad is the target which the trail will be created so if you want a trail on boyfriend or another FlxSprite change that. null is the image to use for the trailsprites, optional, uses the sprite's graphic if null. 4 is the amount of trailsprites to create. 24 is how often to update the trail (0 updates every frame). 0.3 is the alpha value for the very first trailsprite. 0.069 is how much lower the alpha of the next trailsprite is.

IF I MISSED ANYTHING PLEASE FIX IT USING PULL REQUEST!