Reputation: 951
In my spelling game, when a word is complete it fades away in the grid. When a set amount of words are complete (in this case 3) the whole grid fades to reveal the image behind. The problem I have at the moment is when I complete the third word, nothing happens until I click the mouse again.
I need it so as soon as that word is complete the script runs without a click of the mouse.
Here is the piece of code that triggers the fadeOut...
$('.drag').on('click', function(e) {
e.preventDefault();
if(animation) return;
animation = true;
setTimeout(function(){animation = false;},500);
var target = $('.drop-box.spellword:not(.occupied):first');
var targetPos = target.position();
var currentPos = $(this).offset();
var b = $(this);
if (target.length) {
target.addClass("occupied");
$(".minibutton").prop("disabled", true);
b.clone().addClass(
b.data("letter") == target.data("letter") ? "wordglow3" : "wordglow").appendTo("table").css({
background: "transparent",
position: "absolute",
top: currentPos.top,
left: currentPos.left
}).animate({
top: targetPos.top,
left: targetPos.left
}, "slow", function() {
$(this).css({
top: 0,
left: 0
}).appendTo(target);
if (!$('.drop-box.spellword:not(.occupied)').length) {
var wordIsCorrect = 0;
$('.drop-box.spellword').each(function() {
if ($(this).attr("data-letter") == $(this).find("div").attr("data-letter")) {
wordIsCorrect++;
}
});
console.log(wordIsCorrect);
console.log($('.drop-box.spellword').length);
if ($('.drop-box.spellword').length == wordIsCorrect) {
$('.drop-box.spellword').addClass('wordglow2');
$(right).val('Well Done!');
$(right).show();
audioS.play();
$('.counter').html(completeWords + '/6').show();
$(wrong).hide();
$('.minibutton').prop('disabled', false);
} else {
$('.drop-box.spellword').addClass("wordglow4").css('color', 'transparent');
$(wrong).val('Try Again');
$('.minibutton').prop('disabled');
$(wrong).show();
audioF.play();
$('.counter').html(completeWords + '/6').show();
$(right).hide();
//$('.minibutton').prop('disabled', true);
$('.drop-box.spellword')
.animate({ 'opacity': 1 }, 2000, function()
{
$(this)
.removeClass('wordglow4')
.removeClass('occupied')
.html('')
});
}
}
});
}
var completeLetters = $('.wordglow2').length;
var completeWords = (completeLetters / 3);
$('.counter').html(completeWords + '/6');
if (completeWords == 3) {
$('table').fadeOut(2000);
}
var incompleteWords = $('.spellword').hasClass('.wordglow4').length;
if (incompleteWords == 3) {
$('.minibutton').prop('disabled', false);
}
});
THANKS!!
Upvotes: 1
Views: 740
Reputation: 14521
Your level completion test:
if (completeWords == 1) {
$('table').fadeOut(2000);
}
is hit before the clicked letter is added.. Your code is too messy for me to understand, but I suggest you move this test into the branch where you test for completed word, which works fine.
if ($('.drop-box.spellword').length == wordIsCorrect) {
$('.drop-box.spellword').addClass('wordglow2');
$(right).val('Well Done!');
// move the above test here
}
Upvotes: 1