简体   繁体   中英

if statement compare places

We are working on another game and we have several answers to a question. Now the problem is that I need to compare the place of the whale to the place of a fish with the answer.

Does anyone know how to do that?

var Keys = {
    left: 37,
    right: 39,
    up:38,
    down:40
}

function Start(){    
    $("#start").hide();
    alert ("test start weg");
    Opdr1();
}

$(function(){
    $(document).keydown(function(e){
        switch(e.which){
            case Keys.right:
                $("#whale").css("left", "+=10px");
                $("#whale").attr("src", "pacman-right.png");
                break;
            case Keys.left:
                $("#whale").css("left", "-=10px");
                $("#whale").attr("src", "pacman-left.png");
                break;
            case Keys.up:
                $("#whale").css("top", "-=10px");
                break;
            case Keys.down:
                $("#whale").css("top", "+=10px");
                break;
        }
    });
});

function Opdr1(){
    $(".bubbel").css("visibility", "visible");
    $(".vis1-1").css("visibility", "visible");
    $(".vis2-1").css("visibility", "visible");
    $(".vis3-1").css("visibility", "visible");
    $(".vis4-1").css("visibility", "visible");

        //if the whale's css left and top is the same as the left and top of the vis-1 then { }

        if($("#whale").css("left") == ("left") {
            alert('vis-1 fout');
        }
        if($("#whale").css('left') == (".vis2-1").css('left')) {
            alert('vis-2 fout');
        }
        if($("#whale").css('left') == (".vis3-1").css('left')) {
            alert('vis-3 fout');
        }
        if($("#whale").css('left') == (".vis4-1").css('left')) {
            alert('vis-4 fout');
        }

}

You need to compare the offset of the two objects

For example,

if ( $("#whale").offset().top == $(".vis-1").offset().top )
{
}

similarly

if ( $("#whale").offset().left == $(".vis-1").offset().left )
{
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM