Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
173 views
in Technique[技术] by (71.8m points)

javascript - This arrow just Can't reach the monster

I'm making a little game for a school project and I'm stuck ... I Have a player with a x and y position. I want the arrow to start from player.x and reach monster.x. The problem is that the arrow stays stuck in player.x because I drew it on player.x. I want to take the player position only for one time , not during the whole animation , so if the player moves to another location , he can shoot another arrow from that position ...

The code

if (arrowShot) {
        drawSprite(fleche, Arrow.width * Arrow.frameX, Arrow.height * Arrow.frameY, Arrow.width, Arrow.height, player.x, player.y, Arrow.width, Arrow.height);
        if (monstre.health > 0) {
            ctx.fillText("-1", monstre.x, monstre.y + 50);
            monstre.health -= 1;
        }
        if (monstre.health == 0) {
            ctx.fillText("dead", monstre.x, monstre.y + 50);

        }

    }

Yes I add the arrow.speed to it in this code

if (clickX > monstre.x - 400 && clickX < monstre.x - 368 && clickY > monstre.y - 250 && clickY < monstre.y - 202) {
        arrowShot = 1;
        var inter = setInterval(function() {

            if (Arrow.y > monstre.y) {
                Arrow.y -= Arrow.speed;
                Arrow.moving = true;

            }
            if (Arrow.x < monstre.x) {

                Arrow.x += Arrow.speed;
                Arrow.moving = true;
            } else {
                Arrow.x = player.x - 30;
                Arrow.speed = 10;
                arrowShot = 0;
                Arrow.moving == false;
                clearInterval(inter);
            }



        }, 30);
    }
question from:https://stackoverflow.com/questions/65858492/this-arrow-just-cant-reach-the-monster

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...