-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBouncing1.hx
52 lines (46 loc) · 1.16 KB
/
Bouncing1.hx
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
import openfl.events.Event;
import openfl.display.StageAlign;
import openfl.display.StageScaleMode;
import openfl.display.Sprite;
class Bouncing1 extends Sprite {
private var ball:Ball;
private var vx:Float;
private var vy:Float;
public function new() {
super();
init();
}
private function init():Void {
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
ball = new Ball();
ball.x = stage.stageWidth / 2;
ball.y = stage.stageHeight / 2;
vx = Math.random() * 10 - 5;
vy = Math.random() * 10 - 5;
addChild(ball);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
private function onEnterFrame(event:Event):Void {
ball.x += vx;
ball.y += vy;
var left:Float = 0;
var right:Float = stage.stageWidth;
var top:Float = 0;
var bottom:Float = stage.stageHeight;
if (ball.x + ball.radius > right) {
ball.x = right - ball.radius;
vx *= -1;
} else if (ball.x - ball.radius < left) {
ball.x = left + ball.radius;
vx *= -1;
}
if (ball.y + ball.radius > bottom) {
ball.y = bottom - ball.radius;
vy *= -1;
} else if (ball.y - ball.radius < top) {
ball.y = top + ball.radius;
vy *= -1;
}
}
}