Classes: using ipairs / objects that require other objects

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?

Thank you

for k, planet in ipairs(planetTable) do
 planet:draw()
end

Yes, you can use a Turret class in your spaceship class

You can also use inherited (sub) classes, if you need to

i think a spaceship ‘has’ turrets, and not ‘is’ a turret, so sub class may not be appropiate here.

I know that, I was just mentioning it

(although a spaceship that was one big turret would be pretty awesome :smiley: )

i knew you knew, but was not sure he knew :smiley:

@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

I think he needs more turrets. You can never have enough turrets

http://fc09.deviantart.net/fs70/i/2011/059/a/1/ship_by_jakemcneil-d3alqrf.jpg

@Ignatz There’s some room on the sides for more turrets.

lol

People, thank you greatly! I will look into everything closely today!

Is there any way to add network capabilities in Codea? Send messages to some a specific IP address.

only via http.request, no sockets.

See documentation or google this forum, lots of discussions about it