I’m up to rewriting a major bit of my game, which was entirely built with sprites.
I found it suffers several performance issues when there’s only few item on the screen, so now I’m going back to do it again with meshes. However I don’t know how to reiterate a certain object on screen as the following code should be supposed to do.
Can you tell me why does each spawned item leave an infinite trail of itself?
thanks in advance and sorry for bothering
function setup()
frame = 0
foes = {}
horde_counter = 0
horde_limit = 5
end
function draw()
frame = (frame+1)%128
if frame%64 == 0 and horde_counter < horde_limit then
table.insert(foes,Foe1m(500,HEIGHT-10))
horde_counter = horde_counter + 1
end
for i,v in ipairs(foes) do
v:draw()
end
end
Foe1m = class()
function Foe1m:init(x,y)
self.position = vec2(x, y)
self.texture = readImage("Planet Cute:Rock")
self.spriteSize = vec2(40, 30)
self.mesh = mesh()
self.mesh.texture = self.texture
self.index = self.mesh:addRect(self.position.x, self.position.y, self.spriteSize.x, self.spriteSize.y)
end
function Foe1m:draw()
pushMatrix()
pushStyle()
self.position.x = self.position.x - 0.3
self.position.y = self.position.y - 1
translate(self.position.x,self.position.y)
self.mesh:setRect(self.index,0, 0, self.spriteSize.x, self.spriteSize.y)
self.mesh:draw()
popStyle()
popMatrix()
end
Hi @West, thanks for your help.
You mean something like this ?
function setup()
frame = 0
foes = {}
horde_counter = 0
horde_limit = 5
position = vec2(WIDTH/2, HEIGHT-50)
texture = readImage("Planet Cute:Rock")
spriteSize = vec2(40, 30)
mesh = mesh()
mesh.texture = texture
index = mesh:addRect(position.x, position.y, spriteSize.x, spriteSize.y)
end
function draw()
frame = (frame+1)%128
if frame%64 == 0 and horde_counter < horde_limit then
horde_counter = horde_counter + 1
index = mesh:addRect(position.x, position.y, spriteSize.x, spriteSize.y)
end
position.x = position.x - 0.3
position.y = position.y - 1
translate(position.x,position.y)
mesh:setRect(index,0, 0, spriteSize.x, spriteSize.y)
mesh:draw()
popStyle()
popMatrix()
end
I couldn’t get this working either way, and mesh:clear() makes my item to spawn in the very same place without moving.
I can wait for you to grab your’ Ipad thou
ok I got it working now and adding rects to the same mesh ( instead of adding a mesh instance for each spawn ), but how do I keep independent x,y values for each rect I created?
this is my current code :
Foe1mEmitter = class()
local horde_counter = 5
local horde_limit = 5
function Foe1mEmitter:init()
Foe1m = Foe1m(WIDTH/2,HEIGHT-50)
self.frame = 0
end
function Foe1mEmitter:draw()
self.frame = (self.frame+1)%100
if self.frame%100 == 0 and horde_counter <= horde_limit then
Foe1m:SpawnFoe()
horde_counter = horde_counter + 1
end
Foe1m:draw()
end
function Foe1mEmitter:StartSpawingHorde()
horde_counter = 0
end
Foe1m = class()
function Foe1m:init()
self.position = vec2(WIDTH/2, HEIGHT-50)
self.texture = readImage("Dropbox:raptor-cntr")
self.spriteSize = vec2(30, 30)
self.mesh = mesh()
self.mesh.texture = self.texture
self.index = 0
--self.index = self.mesh:addRect(self.position.x, self.position.y, self.spriteSize.x, self.spriteSize.y)
end
function Foe1m:draw()
if self.index > 0 then
pushMatrix()
pushStyle()
self.position.x = self.position.x - 0.3
self.position.y = self.position.y - 1
translate(self.position.x,self.position.y)
self.mesh:setRect(self.index,0, 0, self.spriteSize.x, self.spriteSize.y)
self.mesh:draw()
if self.position.y < 0 then
self.mesh:clear()
end
popStyle()
popMatrix()
end
end
function Foe1m:SpawnFoe()
self.index = self.mesh:addRect(self.position.x, self.position.y, self.spriteSize.x, self.spriteSize.y)
end