Skip to content

Commit 6f06b43

Browse files
committed
start with simple bg scrolling and zoom
1 parent ec95a9b commit 6f06b43

File tree

4 files changed

+251
-0
lines changed

4 files changed

+251
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
assets
2+
bin

parallax.hxproj

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<project version="2">
3+
<!-- Output SWF options -->
4+
<output>
5+
<movie outputType="CustomBuild" />
6+
<movie input="" />
7+
<movie path="project.xml" />
8+
<movie fps="0" />
9+
<movie width="0" />
10+
<movie height="0" />
11+
<movie version="1" />
12+
<movie minorVersion="0" />
13+
<movie platform="Lime" />
14+
<movie background="#FFFFFF" />
15+
<movie preferredSDK=";3.1.3;" />
16+
</output>
17+
<!-- Other classes to be compiled into your SWF -->
18+
<classpaths>
19+
<class path="C:\HaxeToolkit_347\haxe\lib\openfl\8,6,1\src" />
20+
<class path="C:\HaxeToolkit_347\haxe\lib\lime\7,1,0\src" />
21+
<class path="C:\HaxeToolkit_347\haxe\lib\actuate\1,8,9\src" />
22+
<class path="src" />
23+
<class path="bin\html5\haxe" />
24+
</classpaths>
25+
<!-- Build options -->
26+
<build>
27+
<option directives="openfl=8.6.1&#xA;lime=7.1.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;HL_PATH=C:\Development\hl-1.10.0-win&#xA;tools=7.1.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;html5&#xA;web&#xA;html5" />
28+
<option flashStrict="False" />
29+
<option noInlineOnDebug="False" />
30+
<option mainClass="ApplicationMain" />
31+
<option enabledebug="True" />
32+
<option additional="--macro openfl._internal.macros.ExtraParams.include()&#xA;--macro lime._internal.macros.DefineMacro.run()&#xA;--remap flash:openfl" />
33+
</build>
34+
<!-- haxelib libraries -->
35+
<haxelib>
36+
<!-- example: <library name="..." /> -->
37+
</haxelib>
38+
<!-- Class files to compile (other referenced classes will automatically be included) -->
39+
<compileTargets>
40+
<compile path="src\Main.hx" />
41+
</compileTargets>
42+
<!-- Paths to exclude from the Project Explorer tree -->
43+
<hiddenPaths>
44+
<hidden path="obj" />
45+
</hiddenPaths>
46+
<!-- Executed before build -->
47+
<preBuildCommand>"$(CompilerPath)/haxelib" run lime build "$(OutputFile)" $(TargetBuild) -$(BuildConfig) -Dfdb</preBuildCommand>
48+
<!-- Executed after build -->
49+
<postBuildCommand alwaysRun="False" />
50+
<!-- Other project options -->
51+
<options>
52+
<option showHiddenPaths="False" />
53+
<option testMovie="Custom" />
54+
<option testMovieCommand="" />
55+
</options>
56+
<!-- Plugin storage -->
57+
<storage />
58+
</project>

project.xml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<project>
3+
<!-- NMML reference: https://gist.github.com/1763850 -->
4+
5+
<!-- metadata, make sure 'package' is at least 3 segments (ie. com.mycompany.myproject) -->
6+
<meta title="parallax" package="parallax" version="0.0.1" company="loudoweb" />
7+
8+
<!-- output -->
9+
<app main="Main" file="parallax" path="bin" />
10+
11+
<window background="#000000" fps="30" />
12+
<window width="0" height="0" unless="mobile" />
13+
<window orientation="landscape" vsync="false" antialiasing="0" if="cpp" />
14+
15+
<!-- classpath, haxe libs -->
16+
<source path="src" />
17+
<haxelib name="openfl" />
18+
<haxelib name="actuate" />
19+
20+
<!-- assets -->
21+
<assets path="assets/img" rename="img" />
22+
23+
<!-- optimize output
24+
<haxeflag name="-dce full" /> -->
25+
26+
</project>

src/Main.hx

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
package;
2+
3+
import motion.Actuate;
4+
import openfl.display.Bitmap;
5+
import openfl.display.Sprite;
6+
import openfl.Lib;
7+
import openfl.display.StageDisplayState;
8+
import openfl.events.Event;
9+
import openfl.events.MouseEvent;
10+
import openfl.geom.Point;
11+
12+
/**
13+
* ...
14+
* @author Ludovic Bas - www.lugludum.com
15+
*/
16+
class Main extends Sprite
17+
{
18+
19+
public var image:Bitmap;
20+
21+
22+
var deltaZoom:Float;
23+
var maxZoom:Float;
24+
var minZoom:Float;
25+
var zoom:Float;
26+
var bgPos:Point;
27+
28+
public function new()
29+
{
30+
super();
31+
32+
zoom = 1;
33+
34+
//stage.displayState = StageDisplayState.FULL_SCREEN;
35+
36+
37+
// Assets:
38+
var data = openfl.Assets.getBitmapData("img/background-full0000.png");
39+
image = new Bitmap(data, null, true);
40+
addChild(image);
41+
42+
setZoomBounds();
43+
44+
bgPos = new Point();
45+
46+
stage.addEventListener(MouseEvent.CLICK, onClick);
47+
stage.addEventListener(MouseEvent.MOUSE_WHEEL, onWheel);
48+
stage.addEventListener(Event.ENTER_FRAME, onUpdate);
49+
stage.addEventListener(MouseEvent.RIGHT_CLICK, onReset);
50+
stage.addEventListener(Event.RESIZE, onResize);
51+
}
52+
53+
function setZoomBounds():Void
54+
{
55+
//zoom bounds
56+
//to have fluid zoom
57+
minZoom = stage.stageHeight / image.height;
58+
deltaZoom = (1 - minZoom) / 2;
59+
maxZoom = 1;
60+
do {
61+
maxZoom += deltaZoom;
62+
}while (maxZoom < 1.2);
63+
64+
trace(minZoom, zoom, maxZoom);
65+
}
66+
67+
function onResize(e:Event):Void
68+
{
69+
trace("resize");
70+
setZoomBounds();
71+
//find closest zoom to current
72+
var closest = minZoom - deltaZoom;
73+
var temp = closest;
74+
var diff = 42.;
75+
do {
76+
temp += deltaZoom;
77+
if (zoom - temp < diff)
78+
{
79+
diff = zoom - temp;
80+
closest = temp;
81+
}
82+
}while (closest < maxZoom && diff != 0);
83+
84+
if (closest != zoom)
85+
{
86+
zoom = closest;
87+
//apply zoom TODO
88+
}
89+
}
90+
91+
function onReset(e:MouseEvent):Void
92+
{
93+
zoom = 1;
94+
Actuate.tween(image, 0.25, {scaleX: zoom, scaleY: zoom});
95+
}
96+
97+
function onUpdate(e:Event):Void
98+
{
99+
if (this.mouseX < 30)
100+
{
101+
image.x += 10;
102+
}else if (this.mouseX > stage.stageWidth - 30)
103+
{
104+
image.x -= 10;
105+
}else if (this.mouseY < 30)
106+
{
107+
image.y += 10;
108+
}else if (this.mouseY > stage.stageHeight - 30)
109+
{
110+
image.y -= 10;
111+
}else{
112+
return;
113+
}
114+
bgPos.setTo(image.x, image.y);
115+
checkBounds(bgPos, stage.stageWidth, stage.stageHeight, image.width, image.height);
116+
image.x = bgPos.x;
117+
image.y = bgPos.y;
118+
119+
}
120+
function onClick(e:MouseEvent):Void
121+
{
122+
stage.displayState = StageDisplayState.FULL_SCREEN;
123+
124+
}
125+
function onWheel(e:MouseEvent):Void
126+
{
127+
var preZoom = zoom;
128+
zoom += e.delta/100 * deltaZoom;
129+
if (zoom > maxZoom)
130+
zoom = maxZoom;
131+
if (zoom < minZoom)
132+
zoom = minZoom;
133+
134+
if (preZoom == zoom)
135+
return;
136+
137+
var imagePivot = image.globalToLocal(new Point(e.stageX, e.stageY));
138+
139+
var offsetX = image.x + imagePivot.x * preZoom - imagePivot.x * zoom;
140+
var offsetY = image.y + imagePivot.y * preZoom - imagePivot.y * zoom;
141+
142+
bgPos.setTo(offsetX, offsetY);
143+
image.scaleX = image.scaleY = zoom;
144+
checkBounds(bgPos, stage.stageWidth, stage.stageHeight, image.width, image.height);
145+
image.x = bgPos.x;
146+
image.y = bgPos.y;
147+
148+
}
149+
150+
function checkBounds(pt:Point, screenX:Int, screenY:Int, imageWidth:Float, imageHeight:Float):Void{
151+
if (pt.x > 0){
152+
pt.x = 0;
153+
}else if (pt.x < screenX - imageWidth){
154+
pt.x = screenX - imageWidth;
155+
}
156+
157+
if (pt.y > 0){
158+
pt.y = 0;
159+
}else if (pt.y < screenY - imageHeight){
160+
pt.y = screenY - imageHeight;
161+
162+
}
163+
}
164+
165+
}

0 commit comments

Comments
 (0)