[SOLVED]Factory randomly works (or not) on game run

Hi,

I try to make appears some enemies for my game. I call this code from init function. But when i run my game, sometime they appear, sometime they don’t.
worms_count is still equal to 163, and MAX_MAP_WIDTH or HEIGHT are equals to 64.
Any idea ?

EDIT: The worms are just… randomly invisible. But when i collide with them, they are destroyed, like i need to.

-- make worms spawn
function create_worms(worms_count)
	-- create worms
	local pos = vmath.vector3(0, 0, 0)
	local url = "/map#level1"
	local cpt = 0
	local dx = 0
	local dy = 0
	local angle = 0

	for y = 1, MAX_MAP_HEIGHT do
		for x = 1, MAX_MAP_WIDTH do
			if cpt % worms_count == 0 then
	
				if tilemap.get_tile(url, "layer1", x, y) < 2 then
					dx = 0
					dy = 0
	
					if angle == 0 and y < MAX_MAP_HEIGHT and tilemap.get_tile(url, "layer1", x, y + 1) < 2 then
						dy = 1
					elseif angle == 180 and y > 1 and tilemap.get_tile(url, "layer1", x, y - 1) < 2 then
						dy = -1
					elseif angle == 270 and x < MAX_MAP_WIDTH and tilemap.get_tile(url, "layer1", x + 1, y) < 2 then
						dx = 1
					elseif angle == 90 and x > 1 and tilemap.get_tile(url, "layer1", x - 1, y) < 2 then
						dx = -1
					end
				end
	
				if dx ~= 0 or dy ~= 0 then
					pos.x = (((x - 1) * TILESIZE) + (TILESIZE / 2) + (dx * TILESIZE / 2)) * GAME_SCALE
					pos.y = (((y - 1) * TILESIZE) + (TILESIZE / 2) + (dy * TILESIZE / 2)) * GAME_SCALE
	
					factory.create("/wormsfactory#factory", pos, nil, {angle = angle})
				end
			end

			cpt = cpt + 1
			angle = (angle + 90) % 360
		end
	end
end

Might be a Z-order issue - the sprites are being drawn behind the tilemap sometimes if they have the same Z value.

Try setting pos.z to 1 before calling factory.create

1 Like

Thanks a lot benjames171 ! :slight_smile: