Multiple instances of an enemy.
Posted: Tue Jan 15, 2013 4:35 am
I'm remaking this game as my first real love2d project, and am stuck on how to make multiple(unlimited) copies of the fruit.
http://www.newgrounds.com/portal/view/581553
Classes in Lua... well, the lack of classes in Lua rather, has me completely lost. Are there any tutorials as to how you might do this? I've already got this class for the oranges, using the class file I found in Mari0: https://love2d.org/wiki/Simple_Educative_Class_System (The top one.) I've been trying to use mari0 as a reference for most of what I'm doing, but I just can't find anything relating to this in there, even though there are clearly multiple copies of the enemies. (Granted they all always move in the same pattern, direction, and speed as the other copies, so that probably makes things easier, whereas mine requires them all to move in completely random directions.)
The move and direction functions were just for testing.
http://www.newgrounds.com/portal/view/581553
Classes in Lua... well, the lack of classes in Lua rather, has me completely lost. Are there any tutorials as to how you might do this? I've already got this class for the oranges, using the class file I found in Mari0: https://love2d.org/wiki/Simple_Educative_Class_System (The top one.) I've been trying to use mari0 as a reference for most of what I'm doing, but I just can't find anything relating to this in there, even though there are clearly multiple copies of the enemies. (Granted they all always move in the same pattern, direction, and speed as the other copies, so that probably makes things easier, whereas mine requires them all to move in completely random directions.)
The move and direction functions were just for testing.
Code: Select all
orange = class:new()
function orange:init()
self.sprite = love.graphics.newImage("graphics/orange.png")
self.speed = 100
self.timer = 0
self.dir = 0
self.x = 200
self.y = 200
end
function orange:update(dt)
self:direction(dt)
self:move(dt)
end
function orange:draw()
love.graphics.draw(self.sprite, self.x, self.y, 0, 1, 1, ( self.sprite:getWidth() / 2 ), ( self.sprite:getHeight() / 2 ))
end
function orange:direction(dt)
self.timer = self.timer + dt
if ( self.timer >= 2 ) then
self.timer = 0
self.dir = math.random(4) - 1
end
end
function orange:move(dt)
if ( self.dir == 0 ) then
self.y = ( self.y - ( self.speed * dt ) )
elseif ( self.dir == 1 ) then
self.x = ( self.x + ( self.speed * dt ) )
elseif ( self.dir == 2 ) then
self.y = ( self.y + ( self.speed * dt ) )
elseif ( self.dir == 3 ) then
self.x = ( self.x - ( self.speed * dt ) )
end
end