hi,
My first question is this. I have a “Planet” class that is a … planet. This class has a method - draw! I created a table with planets, but I can’t understand how to integrate through it using ipairs.
for k, planet in ipairs do
planet:draw()
end
Doesn’t work.
And one more on the same subjects. I have a space ship, that requires turrets. Can I make a class Turret, and then use it in my spaceship class?
@alexNaumov Here’s some code to give you an idea. I created a planet class, spaceship class, and a turret class for each ship. I don’t know what you want to do with planets, ships, or turrets, so I didn’t do a whole lot. I created 3 planets, 5 ships, and 3 turrets for each ship for this example. The number of planets, ships, and turrets plus whatever else you want will depend on your game.
displayMode(FULLSCREEN)
function setup()
planetTab={} -- table for planets
for p=1,3 do -- create 3 planets
table.insert(planetTab,planet(math.random(WIDTH),math.random(HEIGHT)))
end
shipTab={} -- table for ships
for s=1,5 do -- create 5 ships
table.insert(shipTab,spaceShip(math.random(100,WIDTH-100),
math.random(100,HEIGHT-100)))
end
end
function draw()
background(40, 40, 50)
for a,b in pairs(planetTab) do
b:draw() -- draw planets
end
for a,b in pairs(shipTab) do
b:draw() -- draw ships
end
end
planet=class()
function planet:init(x,y)
self.x=x
self.y=y
end
function planet:draw()
sprite("Space Art:Cloudy Nebula",self.x,self.y,70) -- draw planet
end
spaceShip=class()
function spaceShip:init(x,y)
self.x=x
self.y=y
self.turretTab={} -- turret table for this ship
for t=1,3 do -- create 3 turrets for this ship
table.insert(self.turretTab,turret(self.x,self.y+t*20-40))
end
end
function spaceShip:draw()
sprite("SpaceCute:Rocketship",self.x,self.y,60) -- draw ship
for a,b in pairs(self.turretTab) do
b:draw() -- draw turrets for this ship
end
end
turret=class()
function turret:init(x,y)
self.x=x
self.y=y
end
function turret:draw()
sprite("Tyrian Remastered:Sphere",self.x,self.y) -- draw turrets
end