【发布时间】:2022-12-17 10:16:25
【问题描述】:
当我射击小行星时,它们会消失,这正是我想要的,但是我希望产生更多的小行星,而不是它们永远消失。我不确定要在此代码中添加什么以在游戏中产生更多的小行星。
任何帮助表示赞赏
我认为将小行星的数量设为常数可以确保屏幕上始终显示 5 颗小行星,但这似乎不起作用
// the asteroids
const NUM_ASTERIODS = 3;
for (let i = 0; i < NUM_ASTERIODS; i++) {
var spawnPoint = asteroidSpawnPoint();
var a = add([
sprite("asteroid"),
pos(spawnPoint),
rotate(rand(1,90)),
origin("center"),
area(),
scale(0.2),
solid(),
"asteroid",
"mobile",
"wraps",
{
speed: rand(5, 10),
initializing: true
}
]);
while (a.isColliding("mobile")) {
spawnPoint = asteroidSpawnPoint();
a.pos = spawnPoint;
a.pushOutAll();
}
a.initializing = false;
a.pushOutAll();
}
function asteroidSpawnPoint() {
// spawn randomly at the edge of the scene
return choose([rand(vec2(0), vec2(width(), 0)),
rand(vec2(0), vec2(0, height())),
rand(vec2(0, height()), vec2(width(), height())),
rand(vec2(width(), 0), vec2(width(), height()))]);
}
【问题讨论】:
标签: game-development spawning kaboom