在我正在处理的头像生成器的代码中,我有两个按钮事件处理程序方法,它们可以使角色的鼻子变小或变大。它们几乎包含相同的代码,我想修复(即干)
再往下你会看到我是如何将事件处理程序方法中的逻辑删除到一个新函数中的,但我不知道如何改变这个函数内部的实际逻辑,所以它要么使鼻子变小,要么变大。所以我基本上想要的是能够将 - 更改为 + 或其他方式,具体取决于是否从事件处理程序方法调用它以使鼻子变小或变大。
有什么建议么?
现在的代码:
smallerNoseBtn.on('click', function() {
var nose = AvGen.noses.nose1;
if (nose.size >= 0.3) {
nose.x += 3, nose.y += 3;
nose.size = nose.size -= 0.1;
nose.size = Math.round(nose.size * 10) / 10;
nose.noseObj.transform('S' + nose.size + ', ' + nose.size + ', 0, 0, T' + nose.x + ', ' + nose.y);
}
});
biggerNoseBtn.on('click', function() {
var nose = AvGen.noses.nose1;
if (nose.size <= 1.8) {
nose.x -= 3, nose.y -= 3;
nose.size = nose.size += 0.1;
nose.size = Math.round(nose.size * 10) / 10;
nose.noseObj.transform('S' + nose.size + ', ' + nose.size + ', 0, 0, T' + nose.x + ', ' + nose.y);
}
摆脱干燥:
smallerNoseBtn.on('click', function() {
var nose = AvGen.noses.nose1;
if (nose.size >= 0.3) {
changeNoseSize(nose, 'smaller');
}
});
biggerNoseBtn.on('click', function() {
var nose = AvGen.noses.nose1;
if (nose.size <= 1.8) {
changeNoseSize(nose, 'bigger');
}
});
// The question is what to write in the functions to make it possible to use the variable
// changeSize (which will contain of either 'bigger' or 'smaller')?
function changeNoseSize(nose, changeSize) {
nose.x -= 3, nose.y -= 3;
nose.size = nose.size += 0.1;
nose.size = Math.round(nose.size * 10) / 10;
nose.noseObj.transform('S' + nose.size + ', ' + nose.size + ', 0, 0, T' + nose.x + ', ' + nose.y);
}