MCPcopy
hub / github.com/Vishal-raj-1/Awesome-JavaScript-Projects / startAction

Function startAction

assets/js/fruitSlicer.js:79–130  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

77 //start sending fruits
78
79 function startAction() {
80
81 //generate a fruit
82 $("#fruit1").show();
83 chooseFruit(); //choose a random fruit
84 $("#fruit1").css({
85 'left': Math.round(550 * Math.random()),
86 'top': -50
87 }); //random position
88
89 //generate a random step
90 step = 1 + Math.round(5 * Math.random()); // change step
91
92 // Move fruit down by one step every 10ms
93 action = setInterval(function () {
94
95 //move fruit by one step
96 $("#fruit1").css('top', $("#fruit1").position().top + step);
97
98 //check if the fruit is too low
99 if ($("#fruit1").position().top > $("#fruitsContainer").height()) {
100 //check if we have trials left
101 if (trialsLeft > 1) {
102 //generate a fruit
103 $("#fruit1").show();
104 chooseFruit(); //choose a random fruit
105 $("#fruit1").css({
106 'left': Math.round(550 * Math.random()),
107 'top': -50
108 }); //random position
109
110 //generate a random step
111 step = 1 + Math.round(5 * Math.random()); // change step
112
113 //reduce trials by one
114 trialsLeft--;
115
116 //populate trialsLeft box
117 addHearts();
118
119 } else { // game over
120 playing = false; //we are not playing anymore
121 $("#startreset").html("Start Game"); // change button to Start Game
122 $("#gameOver").show();
123 $("#gameOver").html('<p>Game Over!</p><p>Your score is ' + score + '</p>');
124 $("#trialsLeft").hide();
125 $("#score").hide();
126 stopAction();
127 }
128 }
129 }, 10);
130 }
131
132 // generate a random fruit
133

Callers 1

fruitSlicer.jsFile · 0.85

Calls 5

chooseFruitFunction · 0.85
addHeartsFunction · 0.85
stopActionFunction · 0.85
$Function · 0.70
showMethod · 0.45

Tested by

no test coverage detected