1

我对编码(Python、C 和 XHTML)有所了解,并且正在尝试了解 Game Maker 的基础知识。我创建了一个房间,敌人在移动,撞到墙壁等等,但现在,我想在房间里随机生成敌人,只要他们在地上。目前,它仅在我随机生成它们时才有效。

这是我在 Create 事件中放入的代码,obj_enemy但显然有些东西不起作用,因为它根本不会产生任何敌人。

另外,不知道这是否重要,但如果我还没有把自己放在obj_enemy房间里,它们也不会产生......

// INIT //
dir = -1;      // direction
movespeed = 3; // movement speed
hsp = 0;       // horizontal speed
vsp = 0;       // vertical speed
grav = 0.5;    // gravity

// CREATE //
// Find a random X position in the room
var randx = random(room_width);
// Find a random Y position in the room
var randy = random(room_height);

// If the random position is empty
if position_empty (randx, randy)
{
    // If there is a block
    // 16 pixels under
    // the random Y position
    // (the sprite of obj_enemy is 32x32 pixels)
    if place_meeting (randx, randy+16, obj_block01)
    {
        // If there is less than 4 ennemies
        if instance_number (obj_ennemy) <= 4
        {
            // Create an ennemy
            instance_create(randx, randy, obj_ennemy);
        }
    }
}

我的房间

4

1 回答 1

1

这是 obj_enemy 的创建事件。如果房间里没有 obj_enemy,那么这段代码将永远不会运行!

您需要从房间中的至少一个敌人开始,或者创建一个控制器对象来负责创建您放入房间的敌人(我推荐这种方法)。

此外,即使代码确实运行了,那么在正确位置在墙壁上方生成敌人的机会也很小,因此您必须多次运行程序才能看到它发生。为了避免这种情况,只需将生成代码放入 while true 循环中,并在生成 4 个敌人后从中中断:

while (instance_number (obj_ennemy) <= 4)
{
// Find a random X position in the room
var randx = random(room_width);
// Find a random Y position in the room
var randy = random(room_height);

// If the random position is empty
if position_empty (randx, randy)
{
    // If there is a block
    // 16 pixels under
    // the random Y position
    // (the sprite of obj_enemy is 32x32 pixels)
    if place_meeting (randx, randy+16, obj_block01)
    {

       // Create an ennemy
       instance_create(randx, randy, obj_ennemy);
     }

}
}
于 2016-02-07T15:41:43.450 回答