逐步添加图像(数组) [英] Progressively add image (arrays)

查看:73
本文介绍了逐步添加图像(数组)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在创建一款太空飞船在移动的游戏,它必须避开火球图像才能获胜。我的问题是我只有一个火球一遍又一遍地循环。相反,我想拥有许多火球,随着时间的流逝,火球成倍增加。我认为我应该合并一个数组并使用push()方法,但是我尝试了一下,但没有成功。如果有人可以帮助我,将不胜感激。谢谢

So I am creating a game where a spaceship moves and it must avoid a fireball image in order to win. My issue is that I have only one fireball looping over and over. Instead, I would like to have many fireballs, which are multiplied as time passes. I think I should need to incorporate an array and use push() method but I tried and it didn't worked. If anyone could help me, it would be very appreciated. Thanks

//Fireball script
function fireballScript(offset) {
    return Math.floor(Math.random() * (window.innerWidth - offset))
}
let fireballMovement = {
    x: fireballScript(fireball.offsetWidth),
    y: 0
}
const fireLoop = function () {
    fireballMovement.y += 1
    fireball.style.top = fireballMovement.y + 'px'
    if (fireballMovement.y > window.innerHeight) {
        fireballMovement.x = fireballScript(fireball.offsetWidth)
        fireball.style.left = fireballMovement.x + 'px'
        fireballMovement.y = 0
        fireball.setAttribute('hit', false)
    }
}
fireball.style.left = fireballMovement.x + 'px'
let fireballSpeed = setInterval(fireLoop, 1000 / 250)

let fireball = document.querySelector("#fireball")


<img src = "Photo/fireball.png" id = "fireball" >

//Stop game on collision
function checkCollision() {
    if (detectOverlap(spaceship, fireball) && fireball.getAttribute('hit') == 'false') {
        hits++
        fireball.setAttribute('hit', true)
        alert("lost")
    }
    setTimeout(checkCollision, 1)
}
var detectOverlap = (function () {
    function getPositions(spaceship) {
        var pos = spaceship.getBoundingClientRect()
        return [[pos.left, pos.right], [pos.top, pos.bottom]]
    }

    function comparePositions(p1, p2) {
        let r1 = p1[0] < p2[0] ? p1 : p2
        let r2 = p1[0] < p2[0] ? p2 : p1
        return r1[1] > r2[0] || r1[0] === r2[0]
    }
    return function (a, b) {
        var pos1 = getPositions(a),
            pos2 = getPositions(b)
        return comparePositions(pos1[0], pos2[0]) && comparePositions(pos1[1], pos2[1])
    }
})()

let spaceship = document.querySelector("#icon")
<img src = "Photo/Spaceship1.png" id="icon">

Ps:我问了十遍这个问题,但是我永远也找不到答案。码。如果有人可以解决此问题,将非常有帮助...再次感谢

推荐答案

您需要一个数组火球数量

You need to have an array of fireballs

var fireballs = [];

为火球创建一个构造函数,而不是直接将它们放入html

Make a constructor for fireballs instead of putting them directly in the html

function fireball(x, y) {
    movementX = x;
    movementY = y;
}

然后使用动态位置值将新的推入数组。要将它们添加到文档中,您需要将它们附加到父元素。如果< body> 是该父母,则可以执行以下操作:

Then push new ones into the array with dynamic position values. To add them to the document, you need to append them to a parent element. If the <body> is that parent, you'd do this:

let f = new fireball(10, 20)
fireballs.push(f)
document.body.appendChild(f)

在您的更新中,反复浏览火球并更新其移动。

In your update, iterate through the fireballs and update their movement.

fireballs.forEach((fireball) => {
    // update position for each fireball
});

这篇关于逐步添加图像(数组)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆