-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathScrollingBitmap.hx
More file actions
238 lines (184 loc) · 4.21 KB
/
ScrollingBitmap.hx
File metadata and controls
238 lines (184 loc) · 4.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package com.stencyl.models.scene;
import openfl.display.Sprite;
import openfl.display.Bitmap;
import com.stencyl.Engine;
class ScrollingBitmap extends Sprite
{
public var image1:Bitmap; //Center
public var image2:Bitmap; //W
public var image3:Bitmap; //E
public var image4:Bitmap; //NW
public var image5:Bitmap; //N
public var image6:Bitmap; //NE
public var image7:Bitmap; //SW
public var image8:Bitmap; //S
public var image9:Bitmap; //SE
public var speed:Float;
public var curStep:Float;
public var running:Bool;
public var parallax:Bool;
public var scrolling:Bool;
public var cacheWidth:Float;
public var cacheHeight:Float;
public var xP:Float;
public var yP:Float;
public var xPos:Float;
public var yPos:Float;
public var xVelocity:Float;
public var yVelocity:Float;
public var parallaxX:Float;
public var parallaxY:Float;
public var backgroundID:Int;
public var repeats:Bool;
public function new(img:Dynamic, dx:Float, dy:Float, px:Float=0, py:Float=0, ID:Int=0, repeats:Bool = true)
{
super();
curStep = 0;
running = true;
this.repeats = repeats;
image1 = new Bitmap(img);
addChild(image1);
cacheWidth = image1.width;
cacheHeight = image1.height;
if (repeats)
{
image2 = new Bitmap(img);
image2.x = image1.x-cacheWidth;
addChild(image2);
image3 = new Bitmap(img);
image3.x = image1.x+cacheWidth;
addChild(image3);
//
image4 = new Bitmap(img);
image4.x = image1.x-cacheWidth;
image4.y = image1.y-cacheHeight;
addChild(image4);
image5 = new Bitmap(img);
image5.y = image1.y-cacheHeight;
addChild(image5);
image6 = new Bitmap(img);
image6.x = image1.x+cacheWidth;
image6.y = image1.y-cacheHeight;
addChild(image6);
//
image7 = new Bitmap(img);
image7.x = image1.x-cacheWidth;
image7.y = image1.y+cacheHeight;
addChild(image7);
image8 = new Bitmap(img);
image8.y = image1.y+cacheHeight;
addChild(image8);
image9 = new Bitmap(img);
image9.x = image1.x+cacheWidth;
image9.y = image1.y+cacheHeight;
addChild(image9);
}
xP = 0;
yP = 0;
xPos = 0;
yPos = 0;
xVelocity = dx;
yVelocity = dy;
parallaxX = px;
parallaxY = py;
scrolling = (dx != 0 || dy != 0);
parallax = (px != 0 || py != 0);
backgroundID = ID;
}
public function update(x:Float, y:Float, elapsedTime:Float)
{
var needsReset:Bool = false;
if(parallax)
{
xPos = -Std.int(x * parallaxX);
yPos = -Std.int(y * parallaxY);
needsReset = true;
}
else if(running)
{
xPos = 0;
yPos = 0;
}
else
{
xPos = xP;
yPos = yP;
}
if(scrolling && running)
{
var width = cacheWidth;
var height = cacheHeight;
xP += xVelocity / 10.0 * Engine.SCALE;
yP += yVelocity / 10.0 * Engine.SCALE;
if (this.repeats)
{
if(xP < -width || xP > width)
{
xP = xP % width;
}
if(yP < -height || yP > height)
{
yP = yP % height;
}
}
xPos += Math.floor(xP);
yPos += Math.floor(yP);
curStep += 1;
if(curStep >= 1)
{
needsReset = true;
curStep -= Math.floor(curStep);
}
}
if(needsReset)
{
//TODO: optimize?
resetPositions();
}
}
public function resetPositions()
{
cacheWidth = image1.width;
cacheHeight = image1.height;
if (this.repeats)
{
if (xPos < -cacheWidth)
{
xPos = xPos % cacheWidth;
}
if (yPos < -cacheHeight)
{
yPos = yPos % cacheHeight;
}
}
image1.x = xPos;
image1.y = yPos;
if (this.repeats)
{
image2.x = xPos - cacheWidth;
image2.y = yPos;
image3.x = xPos + cacheWidth;
image3.y = yPos;
image4.x = xPos - cacheWidth;
image4.y = yPos - cacheHeight;
image5.x = xPos;
image5.y = yPos - cacheHeight;
image6.x = xPos + cacheWidth;
image6.y = yPos - cacheHeight;
image7.x = xPos - cacheWidth;
image7.y = yPos + cacheHeight;
image8.x = xPos;
image8.y = yPos + cacheHeight;
image9.x = xPos + cacheWidth;
image9.y = yPos + cacheHeight;
}
}
public function start()
{
running = true;
}
public function stop()
{
running = false;
}
}