Skip to content

Commit df8f822

Browse files
Updated H2D sections - Objects, ObjectTrees, Scenes, Layers (#102)
1 parent 830a88f commit df8f822

File tree

5 files changed

+44
-40
lines changed

5 files changed

+44
-40
lines changed

H2D-Introduction.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,6 @@ The underlying `h2d.Object` (from which `h2d.Bitmap` inherits) provides the bitm
5454

5555
![h2d_introduction_helloworld2](https://user-images.githubusercontent.com/88530062/174428357-45f857ed-30bf-450d-99b6-72051f5b0b83.png)
5656

57-
The [next section](Objects) will discuss `h2d.Object` a bit closer.
57+
The [next section](https://heaps.io/documentation/objects.html) will discuss `h2d.Object` a bit closer.
5858

5959
And as said `h2d.Tile` which the bitmap required will be discussed in a later section: [[Graphical surfaces]].

Layers.md

+11-8
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ When using any `h2d.Scene` (`s2d` when inside `hxd.App`) you have all features o
66

77
Basically you have two features here:
88

9-
1. The first is you can place `h2d.Object`s at specific layers by `s2d.addChildAt( myobj, MY_LAYER )`. `0` will be the layer the most in the background, while each higher layer is rendered over/above the precedent layer. Usually you predefine your layer indices in some way for instance by `final MY_LAYER_NAME : Int = 23;` (see in the sample below) so you know what each integer (`Int` value) stands for.
9+
1. The first is you can place `h2d.Object`s at specific layers by `s2d.add( myobj, MY_LAYER )`. `0` will be the layer the most in the background, while each higher layer is rendered over/above the precedent layer. Usually you predefine your layer indices in some way for instance by `final MY_LAYER_NAME : Int = 23;` (see in the sample below) so you know what each integer (`Int` value) stands for.
1010

1111
2. The second feature is that the layer class also allows to control objects individually by `.over()` and `.under()`. Finally you can benefit from the `ysort()` method to order a layer. *In the sample below this will prevent housings to overlap in a weird way. A house farther away from our viewpoint should be rendered behind houses that are closer!*
1212

@@ -18,13 +18,16 @@ Note that the clouds are on top of all other objects. The green soil stains (mea
1818

1919
```haxe
2020
21-
class LayersDemo extends hxd.App {
21+
class Main extends hxd.App {
2222
var clouds : Array<h2d.Object> = [];
2323
var LAYER_FOR_SOIL : Int = 0;
2424
var LAYER_FOR_BUILDINGS : Int = 1;
2525
var LAYER_FOR_SKY : Int = 2;
26-
static function main() {new LayersDemo();}
27-
function new() {super();}
26+
27+
static function main() {
28+
new Main();
29+
}
30+
2831
override function init() {
2932
engine.backgroundColor = 0xFF33cc33; // game's background color is now green
3033
for( i in 0...40 )
@@ -56,7 +59,7 @@ class LayersDemo extends hxd.App {
5659
g.addVertex( -20, 0, 1, 0, 0, 1 );
5760
g.endFill();
5861
set_to_random_postion( g );
59-
s2d.addChildAt( g, LAYER_FOR_BUILDINGS );
62+
s2d.add( g, LAYER_FOR_BUILDINGS );
6063
return g; // allows to receive the reference to the created object...
6164
}
6265
function add_cloud() {
@@ -75,14 +78,14 @@ class LayersDemo extends hxd.App {
7578
add_row_of_bubbles( 24, 0 ); // lower level of bubbles
7679
set_to_random_postion( g );
7780
clouds.push( g );
78-
s2d.addChildAt( g, LAYER_FOR_SKY );
81+
s2d.add( g, LAYER_FOR_SKY );
7982
}
8083
function add_soil_stain() {
8184
var g = new h2d.Graphics();
8285
g.beginFill( 0x84e184 ); // lighter green
8386
g.drawEllipse( 0, 0, 32 + hxd.Math.random(64), 8 + hxd.Math.random(8) );
8487
set_to_random_postion( g );
85-
s2d.addChildAt( g, LAYER_FOR_SOIL );
88+
s2d.add( g, LAYER_FOR_SOIL );
8689
}
8790
function set_to_random_postion( obj:h2d.Object ) {
8891
obj.setPosition( hxd.Math.random( s2d.width ), hxd.Math.random( s2d.height ) );
@@ -98,4 +101,4 @@ class LayersDemo extends hxd.App {
98101
}
99102
```
100103

101-
Note that all we have to use is `s2d.addChildAt( myobj, MY_LAYER )` instead of just `s2d.addChild( myobj )` and finally we use `s2d.ysort( LAYER_FOR_BUILDINGS )` to arrange all houses.
104+
Note that all we have to use is `s2d.add( myobj, MY_LAYER )` instead of just `s2d.addChild( myobj )` and finally we use `s2d.ysort( LAYER_FOR_BUILDINGS )` to arrange all houses.

Object-trees.md

+12-3
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,14 @@ The child objects will inherit the transformations of the parent object they hav
1212
### Code
1313

1414
```haxe
15-
class ObjectTrees extends hxd.App { // ObjectTrees
15+
class Main extends hxd.App { // ObjectTrees
1616
var parent_obj : h2d.Object;
1717
var a_child_obj : h2d.Object;
18-
static function main() {new ObjectTrees();}
18+
19+
static function main() {
20+
new Main();
21+
}
22+
1923
override function init() {
2024
2125
// parent object placed on `s2d`
@@ -81,7 +85,11 @@ class Main extends hxd.App {
8185
var npc_deltaAlpha : Float = -0.01;
8286
var transitionsActive : Bool = false;
8387
//var npc_trans
84-
static function main() {new Main();}
88+
89+
static function main() {
90+
new Main();
91+
}
92+
8593
override function init() {
8694
8795
clock = new h2d.Graphics( s2d );
@@ -151,6 +159,7 @@ class Main extends hxd.App {
151159
t.text = 'changes on clock scale\n& NPC\'s alpha: ${ ( transitionsActive ? "active" : "inactive" ) }';
152160
};
153161
}
162+
154163
override function update(dt:Float) {
155164
super.update(dt);
156165
// clock movement

Objects.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Object
2-
Like shown previously in the introduction to give our Heaps application visual (2D) content we need classes that are build upon `h2d.Object`.
2+
Like shown previously in the introduction to give our Heaps application visual (2D) content we need classes that are built upon `h2d.Object`.
33

44
- An **Object** (represented by [h2d.Object](https://heaps.io/api/h2d/Object.html)) is the base class of all 2D objects, so any thing that you can *add* to the screen (so to a `h2d.Scene` to be more precise, which mainly is `s2d` when inside `hxd.App`).
55

@@ -11,7 +11,7 @@ Like shown previously in the introduction to give our Heaps application visual (
1111
(See more on [[Drawable]])
1212

1313
## Creating and adding Objects
14-
Objects can be added to a [scene](Scenes) directly (a `h2d.Scene`, for instance `s2d` when inside `hxd.App`) or be added to another `h2d.Object` creating an *object tree*. Object trees are regarded in the next section about [[Object trees]].
14+
Objects can be added to a [scene](https://heaps.io/documentation/scenes.html) directly (a `h2d.Scene`, for instance `s2d` when inside `hxd.App`) or be added to another `h2d.Object` creating an *object tree*. Object trees are regarded in the next section about [[Object trees]].
1515

1616
```haxe
1717
var a = new h2d.Object(); // object not added yet

Scenes.md

+18-26
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
# Scenes
2-
In the precedent sections we already used `s2d` which represents the active current scene rendered by the `hxd.App`.
2+
In the previous sections we already used `s2d` which represents the active current scene rendered by the `hxd.App`.
33

44
A **Scene** (represented by [h2d.Scene](https://heaps.io/api/h2d/Scene.html)) represents any scene the user can visit in the Heaps app. Scenes may be *an intro showing the developers logo on start-up*, the *main menu of the game*, maybe a *cutscene* and finally *the level or world* where the actual game takes place and the player walks around.
55

66
`h2d.Scene` (represented by [h2d.Scene](https://heaps.io/api/h2d/Scene.html)) is a special object (it is actually a `h2d.Object`, too!) which is at the root of the scene tree. In [hxd.App](https://heaps.io/api/hxd/App.html) it is accessible by using the `s2d` variable (as we have seen previously). You will need to add your objects to the scene before they can be displayed. The Scene also handles events such as clicks, touch, and keyboard keys.
77

88
```haxe
9-
class Myapp extends hxd.App
10-
{
11-
override private function init():Void
12-
{
9+
class Main extends hxd.App {
10+
override private function init():Void {
1311
super.init();
1412
s2d; // the current scene
1513
var myscene = new h2d.Scene(); // create a new scene
@@ -23,7 +21,9 @@ class Myapp extends hxd.App
2321
2422
}
2523
26-
public static function main(){ new Myapp(); }
24+
public static function main() {
25+
new Main();
26+
}
2727
}
2828
```
2929

@@ -36,14 +36,18 @@ You can deal with scenes mostly any way you like (like with most things in Heaps
3636
![demo_on_using_scenes](https://user-images.githubusercontent.com/88530062/174429682-6debdb8e-d35c-4f3e-87d0-d3d72fe7e2b4.png)
3737

3838
```haxe
39-
class ABC extends hxd.App {
40-
public static var app : ABC; // just will represent this app as an individual object at runtime (so as an instance of this class)
41-
static function main() { app = new ABC(); } // "save" the reference for access from outside later
39+
class Main extends hxd.App {
40+
public static var app : Main; // just will represent this app as an individual object at runtime (so as an instance of this class)
4241
public var myUpdateFunction : Dynamic; // this will allow us to define the update function from other classes (see Level01)
43-
public function new() {super();}
42+
43+
static function main() {
44+
app = new Main();
45+
} // "save" the reference for access from outside later
46+
4447
override function init() {
4548
setScene( new Intro() );
4649
}
50+
4751
override function update(dt:Float) {
4852
if(myUpdateFunction!=null)
4953
myUpdateFunction();
@@ -65,7 +69,7 @@ class Intro extends h2d.Scene {
6569
t.scale( 2 ); t.textColor = 0x606060;
6670
var t = new haxe.Timer( 3 * 1000 );
6771
t.run = () -> {
68-
ABC.app.setScene( new MainMenu() );
72+
Main.app.setScene( new MainMenu() );
6973
t.stop();
7074
};
7175
}
@@ -88,7 +92,7 @@ class MainMenu extends h2d.Scene {
8892
var button_quit = new h2d.Interactive( 300, 40, f ); button_quit.backgroundColor = 0xFFFF4040;
8993
//button_quit.y += 60;
9094
button_play.onClick = (e)->{
91-
ABC.app.setScene( new Level01() );
95+
Main.app.setScene( new Level01() );
9296
};
9397
button_quit.onClick = (e)->{ hxd.System.exit(); };
9498
var t = new h2d.Text( hxd.res.DefaultFont.get(), button_play ); t.text = "Play"; t.setPosition(8,8);
@@ -104,7 +108,7 @@ class Level01 extends h2d.Scene {
104108
var player = new h2d.Graphics( this ); player.setPosition( 200, 200 );
105109
drawSmileyFace( player );
106110
var t = new h2d.Text( hxd.res.DefaultFont.get(), player ); t.text = "Player"; t.setPosition(-50,50);
107-
ABC.app.myUpdateFunction = () -> { player.x += 0.1; };
111+
Main.app.myUpdateFunction = () -> { player.x += 0.1; };
108112
}
109113
function drawSmileyFace( g:h2d.Graphics ) {
110114
// face
@@ -126,16 +130,4 @@ class Level01 extends h2d.Scene {
126130
g.drawCircle( 20, -5, 3 );
127131
}
128132
}
129-
```
130-
131-
## Scenes and camera
132-
133-
Scenes and camera is explained here.
134-
135-
### Sizing
136-
137-
Scenes sizing is explained here.
138-
139-
### Zoom
140-
141-
Camera zoom is explained here.
133+
```

0 commit comments

Comments
 (0)