Log In  


I've been trying for a day to figure this out and I can't. There's barely any information online about this. Although it feels like everyone knows it but me.

My idea is on initialize to run a nested for loop and add any enemies to a list. And in the update move them.
I did this and it worked! But it incremented in grids. There's no actual code of this since I deleted it, but I could easily reprogram it, if someone asks what the block was.

1


What do you mean by "incremented in grids?" What's the issue you're trying to resolve?


I mean when I go to move the enemy it increments the position in a grid. Meaning it's not smooth at all.


Without an example its hard to visualise. Can you supply a cart to look at (or just sample code) ?


I imagine you're moving the enemies as tiles on the map using mset() but this will be restricted (as map tiles are) to the 8x8 grid. Instead, you'll want to create the enemies as objects separate from the map (like your player, probably) using the map tile coords (times 8) as their starting positions.


I made it more readable for you guys but here is a good replication of what it looked like.

-- enemies

enemies={}

function init_en()
	-- check screen for any tile with a flag of 7
	for y=0,128 do
		for x=0,128 do
			if fget(mget(x,y),6) then
				add(enemies,{x=x,y=y,dir=0})
			end
		end
	end
end

function update_en()
	-- update each enemy's position on the tile map
	for enemy in all(enemies) do
   -- clear the current enemy tile
   mset(enemy.x, enemy.y, 0)

  	-- update the position based on direction
  	if enemy.dir == 0 then
   	   enemy.x -= .01
  	elseif enemy.dir == 1 then
   	   enemy.x += .01
  	end

  	-- change direction at boundaries
  	if enemy.x < 0 then
  	   enemy.x = 127
  	elseif enemy.x > 127 then
   	   enemy.x = 0
  	end
  	-- set the enemy tile at the new position
  	mset(enemy.x, enemy.y, 1)
 end
end


[Please log in to post a comment]