0

I'm new to Konvasjs and new to canvas and I've been try to make a game similar to the demo "animals on the beach" I wanna keep the part where if it in the right outline the score increases but instead of the animals in the demo snapping to their specific outline I want the animal to be able to snap to any outline so the player can get a wrong answer, or get less score.

Sorry if what I'm asking is actually a lot of stuff.

  • You have to be more specific. What did you try? What was not working? – lavrton Apr 20 '16 at 14:22
  • @lavrton Like how can I make the "giraffe" image snap to the "Lion" position when it's near the lion's outline. I've tried changing the if(isNearOutline(animal, outline)) part of the on(dragend) so that the "outline" is only the lion. This works if I change all the images to the lion's(which is fine for my game) but I need a function that can "loop" thru all the outlines to snap images to them. – Sian Finlay Apr 20 '16 at 15:12

1 Answers1

0

I found an alternative solution this may not automatically allow drag objects to snap to every snap zone but it does allow you to control it with less code.

I created a function to get the object(code) and the zone(outline):

    function snapTo(code, outlineSnap) {
        var outline = outlines[outlineSnap + '_black'];
        if(isNearOutline(code, outline)) {
            code.position({
                x : outline.x,
                y : outline.y
            });

        console.log("privKey:"+ privKey);
        console.log("Key:"+ key);
        console.log(code.id());
        codeLayer.draw();
        var testKey = privKey+ "_black";


        if(code.id() == outlineSnap) {
            setTimeout(function() {
                code.draggable(false);
            }, 50);
        }
    }
}

This function has to be in created the for(var key in codes) {} and outside the drag events. I then called the function inside the dragend event.

code.on('dragend', function() {
    snapTo(code, "pstart");
    snapTo(code, "pend");
}

There's a bit of an issue with adding the score, I have found a solution that just yet other than keeping the if statement to change draggable to false for the right object outline combination, as if this it taken out the user could 'farm' the drag feature for points...