/
QuestionSet.js
executable file
·167 lines (130 loc) · 5.87 KB
/
QuestionSet.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
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
/*
Copyright 2011, Carnegie Learning
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Engine Imports
var cocos = require('cocos2d');
var geo = require('geometry');
var events = require('events');
// Static Imports
var XML = require('XML').XML;
// Project Imports
var Question = require('Question').Question;
var NumberLine = require('NumberLine').NumberLine;
var MOT = require('ModifyOverTime').ModifyOverTime;
// Holds the in game representation of a QuestionSubset
var QuestionSet = cocos.nodes.Node.extend({
numberline : null, // Holds the numberline for this set of questions
questions : null, // List of questions in this QuestionSet
current : -1, // Index of the current question being presented (within this set)
lineColor : '#FFFFFF', // Current numberline color
init: function (node) {
QuestionSet.superclass.init.call(this);
// Statically binding functions used only as callbacks
this.onQuestionTimeout = this.onQuestionTimeout.bind(this)
this.set('position', new geo.Point(150, 200));
this.numberline = NumberLine.create(XML.getChildByName(node, 'NUMBER_LINE'));
// Get XML representation
var ql = XML.getChildrenByName(node, 'QUESTION')
this.questions = [];
// Build the list of questions in the subset
for(var i=0; i<ql.length; i++) {
this.questions.push(Question.create(ql[i]));
events.addListener(this.questions[i], 'questionTimeout', this.onQuestionTimeout);
}
this.addChild({child: this.numberline});
this.numberline.set('position', new geo.Point(QuestionSet.NumberLineX, QuestionSet.NumberLineY));
this.correctLocator = cocos.nodes.Sprite.create({file: '/resources/General_Wireframe/Green_Star.png'});
},
// Advance to the next question
nextQuestion: function() {
if(this.current < this.questions.length - 1) {
// Advance to next question
events.trigger(this, 'beforeNextQuestion');
// Remove the current question only if this is not a first question
if(this.current > -1) {
this.removeChild({child: this.questions[this.current]});
}
this.current += 1;
setTimeout(this.nextQuestionCallback.bind(this), QuestionSet.QDelay);
}
else {
var that = this;
setTimeout(function() {events.trigger(that, 'endOfSet')}, QuestionSet.QDelay);
}
},
nextQuestionForced: function() {
events.trigger(this, 'beforeNextQuestion');
if(this.current > -1) {
this.removeChild({child: this.questions[this.current]});
}
this.current += 1;
this.nextQuestionCallback()
},
// Finish advancing to the next question
nextQuestionCallback: function() {
this.questions[this.current].set('position', new geo.Point(235, -130));
this.addChild({child: this.questions[this.current]});
this.questions[this.current].scheduleUpdate();
events.trigger(this, 'nextQuestion');
if(this.questions[this.current].timeLimit != null) {
events.trigger(this, 'questionTimerStart', this.questions[this.current].timeLimit);
}
},
// Answers the current question
// Returns: -1 if the answer was not valid, [0-2] if te answer was valid
giveAnswer: function(ans) {
ans -= (this.get('position').x + this.numberline.get('position').x);
ans /= this.numberline.length;
console.log('Answer given: ' + ans);
//Prevent off line answers
if(ans > 1.05) {
return -1;
}
ans = Math.min(ans, 1);
if(ans < -0.05) {
return -1;
}
ans = Math.max(ans, 0);
var retVal = this.questions[this.current].answerQuestion(ans);
// Correct feedback
//TODO: Remove hardcoding
if(retVal < 2) {
events.trigger(this, 'rightAnswer');
}
// Incorrect feedback
else {
events.trigger(this, 'wrongAnswer');
}
events.trigger(this, 'scoreChange', this.questions[this.current].pointsEarned);
var x = this.questions[this.current].correctValue * this.numberline.length + QuestionSet.NumberLineX;
this.correctLocator.set('position', new geo.Point(x, QuestionSet.NumberLineY));
this.correctLocator.set('opacity', 255);
this.addChild({child: this.correctLocator});
setTimeout(this.fadeCorrectLocator.bind(this), 2000);
this.nextQuestion();
return retVal;
},
fadeCorrectLocator: function() {
MOT.create(255, -255, 1.5).bind(this.correctLocator, 'opacity');
},
// Handles questions timing out
onQuestionTimeout: function () {
events.trigger(this, 'questionTimeout');
events.trigger(this, 'scoreChange', this.questions[this.current].pointsEarned);
this.set('lineColor', '#220000');
this.nextQuestion();
},
});
QuestionSet.NumberLineX = 225 - 150;
QuestionSet.NumberLineY = 510 - 200;
QuestionSet.QDelay = 3500; // Delay (in ms) between answering a question and advancing to the next one
exports.QuestionSet = QuestionSet;