-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ball.js
68 lines (57 loc) · 1.68 KB
/
Ball.js
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
function Ball(element) {
var ballHTMLElement = element;
this.changedXDirectionLastTick = false;
this.changedYDirectionLastTick = false;
this.horizontalMotion = "";
this.verticalMotion = "";
this.speed = 1;
this.getRandomDirectionString = function() {
direction = getRandomDirection();
return {
x: direction.x + "=" + this.speed + "px",
y: direction.y + "=" + this.speed + "px"
};
};
this.getElement = function() {
return ballHTMLElement;
}
function getRandomDirection() {
return {
x: getPlusOrMinus(),
y: getPlusOrMinus()
};
};
function getPlusOrMinus() {
var posNeg = Math.floor(Math.random()*2);
if (posNeg === 0) return "-";
else return "+";
};
this.reverseDirection = function(directionString) {
var directionStringArray = directionString.split("");
if (directionStringArray[0] === '-') directionStringArray[0] = '+';
else directionStringArray[0] = "-";
return directionStringArray.join("");
};
this.moveBall = function(xString, yString) {
ballHTMLElement.css('marginTop', yString);
ballHTMLElement.css('marginLeft', xString);
};
// using external object from globals.js
this.xOutOfBounds = function() {
var position = this.getCurrentPosition();
return position.x - screenPadding < 0 || position.x + screenPadding > windowWidth;
};
this.yOutOfBounds = function() {
var position = this.getCurrentPosition();
return position.y - screenPadding < 0 || position.y + screenPadding > windowHeight;
};
// end
this.getCurrentPosition = function() {
var currX = ballHTMLElement.offset().left + ballHTMLElement.width()/2;
var currY = ballHTMLElement.offset().top + ballHTMLElement.height()/2;
return {
x: currX,
y: currY
}
};
}