Skip to content

Commit a88a5b8

Browse files
committed
set screen dimension when parsing to save lines of code; add experimental camera center; remove traces; fix resizing the sample
1 parent 897fe66 commit a88a5b8

File tree

6 files changed

+29
-18
lines changed

6 files changed

+29
-18
lines changed

README.md

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,11 @@ There is a sample with openfl that can help use this library in any engine.
77

88
- parallax scrolling constrained by a defined world dimension
99
- configurable by xml (or code)
10-
- zoom
10+
- zoom constrained by screen height
1111
- sample (openfl)
1212
- agnostic
1313

1414
# Demo
1515

1616
The sample can be tested here (better fullscreen): http://loudoweb.fr/app/parallax/
17-
18-
# known issue
19-
20-
The app is constrained by the world dimension. But it lacks screen dimension constrain. So now the app works better fullscreen. (A fix is coming).
17+
And here is the [xml](https://github.com/loudoweb/parallax/blob/master/assets/data/parallax.xml)

assets/data/parallax.xml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="utf-8" ?>
2-
<data id="test" camX="1000" camY="250" width="4000" height="1500">
2+
<data id="test" camX="1000" camY="250" width="4200" height="1500">
33
<layers>
44
<layer id="sky" depth="0">
55
<sprite img="_11_background" />
@@ -16,30 +16,37 @@
1616
<layer id="big-clouds" depth="0.28">
1717
<sprite img="_07_huge_clouds" />
1818
<sprite img="_07_huge_clouds" x="2048" />
19+
<sprite img="_07_huge_clouds" x="4096" />
1920
</layer>
2021
<layer id="hills" depth="0.45">
2122
<sprite img="_06_hill2" />
2223
<sprite img="_06_hill2" x="2048" />
24+
<sprite img="_06_hill2" x="4096" />
2325
</layer>
2426
<layer id="hills2" depth="0.5">
2527
<sprite img="_05_hill1" />
2628
<sprite img="_05_hill1" x="2048" />
29+
<sprite img="_05_hill1" x="4096" />
2730
</layer>
2831
<layer id="brushes" depth="0.7">
2932
<sprite img="_04_bushes" />
3033
<sprite img="_04_bushes" x="2048" />
34+
<sprite img="_04_bushes" x="4096" />
3135
</layer>
3236
<layer id="distant-trees" depth="0.75">
3337
<sprite img="_03_distant_trees" />
3438
<sprite img="_03_distant_trees" x="2048" />
39+
<sprite img="_03_distant_trees" x="4096" />
3540
</layer>
3641
<layer id="trees" depth="0.95">
3742
<sprite img="_02_trees and bushes" />
3843
<sprite img="_02_trees and bushes" x="2048" />
44+
<sprite img="_02_trees and bushes" x="4096" />
3945
</layer>
4046
<layer id="ground" depth="1">
4147
<sprite img="_01_ground" />
4248
<sprite img="_01_ground" x="2048" />
49+
<sprite img="_01_ground" x="4096" />
4350
</layer>
4451
</layers>
4552
</data>

parallax.hxproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
</classpaths>
2525
<!-- Build options -->
2626
<build>
27-
<option directives="openfl=9.1.0&#xA;lime=7.9.0&#xA;actuate=1.8.9&#xA;ANDROID_SETUP=true&#xA;ANDROID_NDK_ROOT=C:\Development\Android\NDK\android-ndk-r15c&#xA;howlerjs&#xA;lime-webgl&#xA;lime-dom&#xA;lime-howlerjs&#xA;lime-html5&#xA;openfl-disable-handle-error&#xA;HL_PATH=C:\Development\hl-1.10.0-win&#xA;gs&#xA;tools=7.9.0&#xA;ANDROID_SDK=C:\Development\Android\SDK\platforms\android-26&#xA;lime-canvas&#xA;no-compilation&#xA;openfl-html5&#xA;JAVA_HOME=C:\Program Files (x86)\java\jdk1.8.0&#xA;HXCPP_COMPILE_CACHE=E:\hxcpp\cache&#xA;fdb&#xA;html5&#xA;web&#xA;html5" />
27+
<option directives="openfl=9.1.0&#xA;lime=7.9.0&#xA;actuate=1.8.9&#xA;ANDROID_SETUP=true&#xA;ANDROID_NDK_ROOT=C:\Development\Android\NDK\android-ndk-r15c&#xA;howlerjs&#xA;lime-webgl&#xA;lime-dom&#xA;lime-howlerjs&#xA;lime-html5&#xA;openfl-disable-handle-error&#xA;HL_PATH=C:\Development\hl-1.10.0-win&#xA;tools=7.9.0&#xA;ANDROID_SDK=C:\Development\Android\SDK\platforms\android-26&#xA;lime-canvas&#xA;no-compilation&#xA;openfl-html5&#xA;JAVA_HOME=C:\Program Files (x86)\java\jdk1.8.0&#xA;HXCPP_COMPILE_CACHE=E:\hxcpp\cache&#xA;fdb&#xA;html5&#xA;web&#xA;html5" />
2828
<option flashStrict="False" />
2929
<option noInlineOnDebug="False" />
3030
<option mainClass="ApplicationMain" />

parallax/Parallax.hx

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,10 @@ class Parallax
7575
//trace("image height", height / camera.zoom, "stage", screenY);
7676
camera.deltaZoom = (1 - camera.minZoom) / 2;
7777
camera.maxZoom = 1 + camera.deltaZoom *2;
78-
trace(camera.minZoom, camera.zoom, camera.maxZoom, camera.deltaZoom);
79-
8078

79+
//to have zoom with 5 values
8180
var values = [for (i in 0...5) i * camera.deltaZoom + camera.minZoom];
82-
//find closest zoom to current
81+
//find closest zoom to current (when screen dimension changes)
8382
var closest = 42.;
8483
for (item in values)
8584
{
@@ -89,7 +88,6 @@ class Parallax
8988

9089
if (closest != camera.zoom)
9190
{
92-
trace("resize", camera.minZoom, "old", camera.zoom, "closest", closest, camera.maxZoom);
9391
camera.zoom = closest;
9492
//this.zoomOffsetX = zoomOffsetX;
9593
//this.zoomOffsetY = zoomOffsetY;
@@ -145,14 +143,24 @@ class Parallax
145143
updateLayers();
146144
}
147145

148-
public static function parse(xml:Xml):Parallax
146+
public static function parse(xml:Xml, screenWidth:Int = 1920, screenHeight:Int = 1080):Parallax
149147
{
150148
var _xml = new Access(xml.firstElement());
151149
var world = new Parallax(_xml.has.id ? _xml.att.id : "world", _xml.has.world ? _xml.att.world : "", _xml.getFloat("camX"), _xml.getFloat("camY"));
152150
if (_xml.has.width)
153151
world.width = Std.parseFloat(_xml.att.width);
154152
if (_xml.has.height)
155153
world.height = Std.parseFloat(_xml.att.height);
154+
155+
world.camera.width = screenWidth;
156+
world.camera.height = screenHeight;
157+
158+
var useCenterPos = _xml.getBool("useCenterPos", false);
159+
if (useCenterPos)
160+
{
161+
world.camera.x -= screenWidth/2;
162+
world.camera.y -= screenHeight/2;
163+
}
156164

157165
if (world.layers == null)
158166
world.layers = [];

parallax/ParallaxCamera.hx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ class ParallaxCamera
2626

2727
/**
2828
*
29-
* @param x starting position of the camera
30-
* @param y starting position of the camera
29+
* @param x starting position of the camera (top left corner of camera)
30+
* @param y starting position of the camera (top left corner of camera)
3131
* @param width
3232
* @param height
3333
* @param zoom

sample/OpenflSample.hx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class OpenflSample extends Sprite
3333

3434
//parallax
3535
var xml = Xml.parse(Assets.getText("parallax.xml"));
36-
parallax = Parallax.parse(xml);
36+
parallax = Parallax.parse(xml, stage.stageWidth, stage.stageHeight);
3737

3838
containers = [];
3939

@@ -57,7 +57,6 @@ class OpenflSample extends Sprite
5757
OpenflHelper.setWorldBounds(parallax, layer, spr);
5858
}
5959
}
60-
6160
parallax.setZoomBounds(stage.stageHeight);
6261

6362
stage.addEventListener(MouseEvent.MOUSE_WHEEL, onWheel);
@@ -68,8 +67,8 @@ class OpenflSample extends Sprite
6867

6968
function onResize(e:Event):Void
7069
{
71-
//parallax.camera.width = stage.stageWidth;
72-
//parallax.camera.height = stage.stageHeight;
70+
parallax.camera.width = stage.stageWidth;
71+
parallax.camera.height = stage.stageHeight;
7372
parallax.setZoomBounds(stage.stageHeight);
7473
}
7574

0 commit comments

Comments
 (0)