1
Oops, I’m doing a project of a memory game that requires an image to be changed at the time of a click using Jquery.
The game is based on clicking on divs, and display the images inside them, two at a time. If they are equal, they stay on display, if they are different they are hidden.
By clicking on the first div, the image within it is displayed normally by clicking on the second div, the system checks if the images are the same, if yes, they stay the sample, otherwise the system hides the two images clicked a few seconds later.
But this probably doesn’t happen because the image only loads at the end of the click function div. ( code below).
$(".card").click(function () {
tent++;
var idCard = $(this).attr('id');
revelarImg(idCard);
desabilitarClick(idCard);
if (tent == 2) {
if (ganhou(primTent, idCard)) {
pontuacao++;
if (pontuacao == 9)
gameWin();
}
else {
setTimeout(esconderCards(primTent, idCard), 3000);
habilitarClicks(primTent, idCard);
}
tent = 0;
}
else {
primTent = idCard;
}
});
Note that the function that reveals the image is called in every click, but the change is only actually made at the end of the function. If the user made a mistake ( when it is his second attempt), the function is called esconderCards() which cancels the other which discloses them. ( functions codes below )
function revelarImg(id) {
$("#" + id +"> img").attr("src", arrayImgs[id]);
}
function esconderCards(primeira, segunda) {
$("#" + primeira +"> img").attr("src","imgs/front.png");
$("#" + segunda +"> img").attr("src","imgs/front.png");
Follows the html of divs with the pictures:
<div class="card" id="0">
<img src="imgs/front.png">
</div>
<div class="card" id="1">
<img src="imgs/front.png">
</div>
...
How can I display the second image without waiting for the function to end and if "auto cancel"?
Cara edits your question, puts html/css tb so that we can simulate your problem there, will make it easier for someone to answer you
– hugocsl
@hugocsl , done, did not insert css as they are just positions on the screen, do not insert anything from them
– Gabriel Arruda
I didn’t get to watch it, but I believe this video can help you https://www.youtube.com/watch?v=U0I6Z06N0kM
– Edward Ramos
@Caiqueromero when the "Change" would be fired? I would then delete my reveal function, and put the straight snippet in the click?
– Gabriel Arruda
It’s a little confusing here. What determines that one card is the same as another?
– Sam
@Sam , if the html of the two Divs are equal. The boolean function won is a comparison between the htmls.
– Gabriel Arruda
It would be better to edit the question and put the whole code so that it can reproduce the system.
– Sam
Take a look at the example I made, including the links I put in, I hope it helps you.
– Caique Romero