Skip to content

Commit e8252d0

Browse files
Merge pull request #320 from developerzohaib786/add-sound-effects
applying sound when flappy crashed on the roof top or on the ground
2 parents f318b0f + 4fe1197 commit e8252d0

1 file changed

Lines changed: 55 additions & 46 deletions

File tree

javascript/flapping.js

Lines changed: 55 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -4,54 +4,58 @@ function Flappy(layerName) {
44
this.position = gameObject(this.layer);
55
this._isCollided = false;
66
var me = this;
7-
this.action = new Action(function(){me.fly()}, function(){me.started()});
7+
this.action = new Action(function () { me.fly() }, function () { me.started() });
88
document.onmousedown = function () { me.flap() };
99
}
1010

1111
Flappy.prototype = {
12-
velocity : 0,
13-
gravity : 1.8,
14-
flapVelocity : -20,
12+
velocity: 0,
13+
gravity: 1.8,
14+
flapVelocity: -20,
1515

16-
startFlapping : function() {
16+
startFlapping: function () {
1717
this.action.start();
1818
this.position.show();
1919
},
20-
stopFlapping : function() {
20+
stopFlapping: function () {
2121
this.action.stop();
2222
},
2323

24-
fly : function() {
24+
fly: function () {
2525
this.velocity += this.gravity;
2626
this.position.y = this.position.y + this.velocity;
2727
if (this.position.screen.bottom > playfield().height) {
2828
this.position.screen.bottom = playfield().height;
29-
this.isCollided = true;
29+
if (!this.isCollided) {
30+
this.isCollided = true;
31+
}
3032
}
3133
if (this.position.screen.top < 0) {
3234
this.position.screen.top = 0;
33-
this.isCollided = true;
35+
if (!this.isCollided) {
36+
this.isCollided = true;
37+
}
3438
}
3539

3640
this.checkCollision();
3741
},
38-
started : function() {
42+
started: function () {
3943
this.position.y = 0;
4044
this.velocity = 0;
4145
},
42-
flap : function() {
46+
flap: function () {
4347
this.velocity = this.flapVelocity;
44-
var flapSound = new Audio("../flappy-svg/Sounds/Flappy.mp3");
45-
flapSound.play();
48+
var flapSound = new Audio("Sounds/Flappy.mp3");
49+
flapSound.play();
4650
},
47-
show : function() {
51+
show: function () {
4852
this.position.show();
4953
},
50-
hide : function() {
54+
hide: function () {
5155
this.position.hide();
5256
},
5357

54-
checkCollision : function() {
58+
checkCollision: function () {
5559
var flappy_rect = this.layer.getBoundingClientRect();
5660
for (i = 0; i < 3; i++) {
5761
if (i >= obstacles.length)
@@ -60,34 +64,37 @@ Flappy.prototype = {
6064

6165
var c = isOverlap(flappy_rect, o_rect);
6266

63-
if(!c){
64-
var d = isBehind(o_rect,flappy_rect);
65-
if(d){
67+
if (!c) {
68+
var d = isBehind(o_rect, flappy_rect);
69+
if (d) {
6670
updateScore();
6771
}
6872
} else {
69-
var gameOverSound = new Audio("../flappy-svg/Sounds/GameOver.mp3");
70-
gameOverSound.play();
71-
alert('Game Over :( Final Score: ' + Number(document.getElementById("tspan17169").innerHTML));
72-
}
73+
var gameOverSound = new Audio("Sounds/GameOver.mp3");
74+
gameOverSound.play();
75+
alert('Game Over :( Final Score: ' + Number(document.getElementById("tspan17169").innerHTML));
76+
}
7377

7478
if (c || o_rect.right < flappy_rect.left)
7579
obstacles.push(obstacles.shift());
7680

77-
this.isCollided = c;
81+
if (c && !this.isCollided) {
82+
this.isCollided = c;
83+
}
7884
}
7985
},
8086

81-
set isCollided(value){
82-
if (this._isCollided != value)
87+
set isCollided(value) {
88+
if (this._isCollided != value) {
8389
this._isCollided = value;
8490

85-
//On collision code here.
86-
if (this._isCollided)
87-
onCollision();
91+
//On collision code here.
92+
if (this._isCollided)
93+
onCollision();
94+
}
8895
},
8996

90-
get isCollided(){
97+
get isCollided() {
9198
return this._isCollided;
9299
}
93100
}
@@ -115,10 +122,10 @@ function characterChange(layer) {
115122
flappy.show();
116123
}
117124

118-
function stopCharacterChange(){
119-
window.characterChange=function(){
120-
return false;
121-
}
125+
function stopCharacterChange() {
126+
window.characterChange = function () {
127+
return false;
128+
}
122129
}
123130

124131
function startFlapping(layer) {
@@ -134,12 +141,12 @@ function stopFlappingBackgound(name) {
134141
}
135142
}
136143

137-
function getCenteredrect(rect){
144+
function getCenteredrect(rect) {
138145
centered_rect = {
139-
x:rect.left+rect.width/2,
140-
y:rect.bottom+rect.height/2,
141-
width:rect.width,
142-
height:rect.height,
146+
x: rect.left + rect.width / 2,
147+
y: rect.bottom + rect.height / 2,
148+
width: rect.width,
149+
height: rect.height,
143150
};
144151
return centered_rect;
145152
}
@@ -149,23 +156,25 @@ function isOverlap(e1, e2) {
149156
rect1 = getCenteredrect(e1);
150157
rect2 = getCenteredrect(e2);
151158
if (rect1.x < rect2.x + rect2.width &&
152-
rect1.x + rect1.width > rect2.x &&
153-
rect1.y < rect2.y + rect2.height &&
154-
rect1.height + rect1.y > rect2.y)
159+
rect1.x + rect1.width > rect2.x &&
160+
rect1.y < rect2.y + rect2.height &&
161+
rect1.height + rect1.y > rect2.y)
155162
return true;
156163
else return false;
157164
}
158165

159-
function onCollision(){
166+
function onCollision() {
167+
var gameOverSound = new Audio("Sounds/GameOver.mp3");
168+
gameOverSound.play();
160169
show_layer('gameover');
161170
stopAllBackgrounds();
162171
}
163172

164-
function restartGame(){
173+
function restartGame() {
165174
location.reload();
166175
}
167176

168177

169-
function isBehind(r1,r2){
170-
return (r1.right<=r2.left);
178+
function isBehind(r1, r2) {
179+
return (r1.right <= r2.left);
171180
}

0 commit comments

Comments
 (0)