I am making a game for children. It works perfectly when it is played for the first time after being opened. But if the player chooses to play again I have a problem. I have many draggable and droppable elements, and I managed to clone the draggable elements by copying the function inside the event when the PLAY AGAIN button is clicked.
But I have many, like 30 or 40 or maybe more droppable functions which I can only clone if I copy the whole code inside that event which is not practical at all.
I've tried with clone(true,true) but it didn't clone the draggable and droppable functions so I did it with copying as I said before.
Is there any way to clone droppable functions without copying them?
Oh, I am using the draggable and droppable plugins from jquery.ui.
Here is the code for the cloning:
var one, two, three, four, five, six, seven, eight;
one = $("#one").clone(true,true);
Right now I am trying only for one stage so that's why I have only cloning for one.
This is the draggable function:
$(function() {
$( "#dr1,#dr2,#dr3,#dr4,#dr5, #dr6, #dr7, #dr8, #dr9, #dr10, #dr11, #dr12, #dr13, #dr14, #dr15, #dr16, #dr17, #dr18" )
.draggable({containment:"#wrapper", scroll:false});
});
This is the droppable function for the first stage:
$( "#drop1" ).droppable({
accept: "#dr1",
drop: function( event, ui ) {
$(this).addClass("morph");
scream.play();
txt=$("#dr1").text();
$(".drop1").html(txt);
$("#dr1").hide();
poeni += 1;
points += 5;
$("#poeni").html(points);
if(poeni==2){
$( "#wrapper" ).addClass( "back" );
$("#firstNext").show();
end = $("#time").text();
niza = end.split(":");
}
}
});
And this is the PLAY AGAIN event:
$(document).on("click","#Yes", function(){
$("#one").replaceWith(one);
$(function() {
$( "#dr1,#dr2,#dr3,#dr4,#dr5, #dr6, #dr7, #dr8, #dr9, #dr10, #dr11, #dr12, #dr13, #dr14, #dr15, #dr16, #dr17, #dr18" )
.draggable({containment:"#wrapper", scroll:false});
});
game.play();
points = 0;
$("#stazi").show();
$("#final, #one,#two,#three,#four,#five,#six,#seven,#eight").hide();
$("#one").show();
$("#poeni").html(points);
setTimeout( function(){
game.pause();
$("#stazi").fadeOut(1000);
$("#finalPoints").html(points);
$("#final").delay(1000).fadeIn(2000);
win.play();
}
, 60000 );
});
I assumed that once the cloned levels are called, that the droppable function will have the same effect because the ids aren't changed for the elements.