Making a game of pairs and suddenly its disappeared!
Hey, I've been making a pairs game in javascript with jquery and I've been fiddling about with it but now suddenly the cards have disappeared.
My code is below. I'm pretty stumped, can someone let me know what's going on please?
Thanks
Code:
// JavaScript Document
// Pairs game
$(document).ready(function() {
// Sets up variables, sets up image location, size of each card and arrays for each card's picture and position
var imageFiles = 'images/';
var cards = ['card1.jpg', 'card2.jpg', 'card3.jpg', 'card4.jpg', 'card5.jpg', 'card6.jpg'];
var cardSize = 100;
var rows = 3;
var cols = 4;
var posns = [];
var rowsByCols = rows*cols;
// Function that checks for num in posns array
function checkArray(array, num) {
for(i = 0; i < array.length; i++) {
if(array[i] == num) return true;
}
return false;
}
// For loop that creates random unique positions for the cards and then stores them within the posns array
for(i = 0; i < rowsByCols; i++) {
while(true) {
var ranNum = Math.floor(Math.random() * (rowsByCols));
if(!checkArray(posns, ranNum)) {
posns[i] = ranNum;
break;
}
}
// For loop that makes sure then card is the right size then adds a picture to the card at the position specified in the posns array
}
for(i = 0; i < posns.length; i++) {
if(posns[i] >= cards.length) {
posns[i] = posns[i] - cards.length;
}
$('#cards').append('<div class="picture"><img src="' + imageFiles + cards[posns[i]] + '" alt="" /></div>');
}
$('.picture').hover(function() { //apply the mouse over
$(this).css('background-position', '0 -100px');
}, function() { //mouse out
$(this).css('background-position', '0 0');
});
// Setup variable for how many cards have been selected. Function for fading in selected card.
var selected = 1;
$('.picture').click(function() {
if($(this).children().css('display') != 'none') return false;
var step = parseInt($('#information strong').text()) + 1; //add a step
$('#information strong').text(step); //update at informations
$('#steps').text(step); //update at the sidebar
if(selected != 2) {
$(this).children().fadeIn('fast');
selected++;
}
// If 2 cards are selected, stores urls of the picture of each card selected.
else if(selected == 2) {
$(this).children().fadeIn('fast', function() {
var card1; var card2;
$('.picture img').not('.found').each(function() {
if($(this).css('display') != 'none') {
if(!card1) card1 = $(this).attr('src');
else card2 = $(this).attr('src');
}
});
// Compare urls of the two selected cards and if they match adds a class so that the pair is 'out'
if(card1 == card2) {
$(".picture img:visible").each(function() {
$(this).addClass('found');
});
}
// If pair doesn't match then they fade out
else {
$('.picture img').not('.found').fadeOut('slow');
}
// If no hidden pairs are left the game ends
if($('.picture img:hidden').css('display') != 'none') {
$('#cards').fadeOut('slow');
}
selected = 1;
});
}
});
});
Ahem ... if you used this statement to put some content into an element with id="cards" you should be aware that you'd need some element with id="cards" in your HTML
Something like
Code:
<div id="cards"></div>
If your code worked before there must have been such an element before.... I am confused
Ahh perfect thankyou, I must have renamed it to game2 for some reason. I'm having a little trouble making the cards show their background image though. They're just plain white until they're clicked on and fade in, any way I can give them a background image before they're turned?
Maybe you can use the background image on top (position:absolute) of the original image and if clicked, you first remove the background image and then fade in the original one beneath.
I've tried making a seperate div and object for the bkg and placing it behind the card but that doesn't really work. I can't get them to dissapear when i click on a card.
// JavaScript Document
// Pairs game
$(document).ready(function() {
// Sets up variables, sets up image location, size of each card and arrays for each card's picture and position
var imageFiles = 'images/';
var cards = ['card1.jpg', 'card2.jpg', 'card3.jpg', 'card4.jpg', 'card5.jpg', 'card6.jpg'];
var cardbkg = ['bkg.jpg'];
var cardSize = 100;
var rows = 3;
var cols = 4;
var posns = [];
var rowsByCols = rows*cols;
// Function that checks for num in posns array
function checkArray(array, num) {
for(i = 0; i < array.length; i++) {
if(array[i] == num) return true;
}
return false;
}
// For loop that creates random unique positions for the cards and then stores them within the posns array
for(i = 0; i < rowsByCols; i++) {
while(true) {
var ranNum = Math.floor(Math.random() * (rowsByCols));
if(!checkArray(posns, ranNum)) {
posns[i] = ranNum;
break;
}
}
// For loop that makes sure each card is the right size then adds a picture to the card at the position specified in the posns array
}
for(i = 0; i < posns.length; i++) {
if(posns[i] >= cardbkg.length) {
posns[i] = posns[i] - cardbkg.length;
}
$('#cardbkg').append('<div class="bkg"><img src="' + imageFiles + cardbkg + '" alt="" /></div>');
}
for(i = 0; i < posns.length; i++) {
if(posns[i] >= cards.length) {
posns[i] = posns[i] - cards.length;
}
$('#cards').append('<div class="picture"><img src="' + imageFiles + cards[posns[i]] + '" alt="" /></div>');
}
$('.picture').hover(function() { //apply the mouse over
$(this).css('background-position', '0 -100px');
}, function() { //mouse out
$(this).css('background-position', '0 0');
});
// Setup variable for how many cards have been selected. Function for fading in selected card.
var selected = 1;
$('.picture').click(function() {
if($(this).children().css('display') != 'none') return false;
if(selected != 2) {
$(this).children().fadeIn('fast');
selected++;
}
// If 2 cards are selected, stores urls of the picture of each card selected.
else if(selected == 2) {
$(this).children().fadeIn('fast', function() {
var card1; var card2;
$('.picture img').not('.found').each(function() {
if($(this).css('display') != 'none') {
if(!card1) card1 = $(this).attr('src');
else card2 = $(this).attr('src');
}
});
// Compare urls of the two selected cards and if they match adds a class so that the pair is 'out'
if(card1 == card2) {
$(".picture img:visible").each(function() {
$(this).addClass('found');
});
}
// If pair doesn't match then they fade out
else {
$('.picture img').not('.found').fadeOut('slow');
}
// If no hidden pairs are left the game ends
if($('.picture img:hidden').css('display') != 'none') {
$('#cards').fadeOut('slow');
}
selected = 1;
});
}
});
});