Optimizing

Hello there, guys . I have an optimization problem in my project. I make a 2d table (maybe that can solve with 1d table), which contains position of platforms on the screen. There is a screenshot how it looks in game

Sprites in bottom are an animation, which slows down then platforms appear (platforms are static. I had an idea make that field in photoshop, but i decide made it in codea)

Here is my code :

Platforms class

Lvls = class()

function Lvls:init(x)
    -- you can accept and set parameters here
    self.platform = {}
    for x = 1,size do 

    --size = 7

        self.platform[x] = {}
        for y = 1,size do
            self.platform[x][y] = vec2(150+x*90,50+y*90)
            if math.fmod(x,2) == 0 then
                self.platform[x][y].y = self.platform[x][y].y - 45
            end
            end
    end
end
function Lvls:construction()
    
    for k,v in pairs(self.platform) do
        for a,b in pairs(self.platform[k]) do
        sprite("Documents:RhPlatform",b.x,b.y,512/2,512/2)
        
    end
    end

end

function Lvls:draw()
    -- Codea does not automatically call this method
    background(0,0,0)
    self:construction()
    hero:draw()
    monsters:draw()
    monsters1:draw()
    monstersfr:draw()
end

function Lvls:touched(touch)
    -- Codea does not automatically call this method
end

Hero’s Animation class (blue man in the bottom).
Similar to other three sprites

Hero = class()

function Hero:init(x)
    -- you can accept and set parameters here
    self.frames = {"Documents:RhHeroframe1",
    "Documents:RhHeroframe2",
    "Documents:RhHeroframe3"}
    self.herox = WIDTH/2
    self.heroy = 95
    self.count = 1
    self.stop = false
    self.currentFrame = 1
    self.herow = 512
end

function Hero:position()
    
end

function Hero:arrows()
    if CurrentTouch.x > WIDTH/2 and touching == true then
        self.herox = self.herox + 5
        self.herow = 512
        elseif CurrentTouch.x < WIDTH/2 and touching == true then
        self.herox = self.herox - 5
        self.herow = -512
    end
    
    if self.herox < 100 then
        self.herox = 101
        elseif self.herox > WIDTH-100 then
        self.herox = WIDTH-100
    end


    self.position()
end


function Hero:draw()
    -- Codea does not automatically call this method
--    background(0, 0, 0, 255)
    if touching == true then
    self.count = self.count + 1
    if self.count > 7 then
        self.count = 1
        self.currentFrame = self.currentFrame + 1 
    if self.currentFrame > 3 then
          self.currentFrame = 1
      end
      end
        sprite(self.frames[self.currentFrame],self.herox,self.heroy,self.herow/4,512/4)
    
    else
    sprite("Documents:RhHeroframe2",self.herox,self.heroy,self.herow/4,512/4)
    end 
    self:arrows()
end

function Hero:touched(touch)
    -- Codea does not automatically call this method
end

I have no idea how increase fps rate of my project, can you help me, please

@lupino8211 - if the platforms stay in the same place on the screen, then why not draw them to an image in memory using setContext, then you can just draw this one image each time after that, which will make things faster.

@Ignatz , hm, i didnt know that, thanks!

if you have trouble with that, just ask

I avoided using setContext() for a while because I thought it would be fiddly to get the drawing in the right place. I wish I hadn’t waited, it was remarkably easy once I applied myself to it. This seems like a perfect use for it @lupino8211

If a group of objects use the same texture, add them to a single mesh using addRect and then animate them with setRect. It’s much faster than the sprite command in my experience.

SetContext and sprite are fine for a static background

@Ignatz i tried to understan this func for a couple of days , but i did something wrong and now i have only black screen (in platforms class)

Lvls = class()

function Lvls:init(x)
    -- you can accept and set parameters here
    platform = {}
    self.image = image(500,500)
    for x = 1,size do
        platform[x] = {}
        for y = 1,size do
            platform[x][y] = vec2(150+x*90,50+y*90)
            if math.fmod(x,2) == 0 then
                platform[x][y].y = platform[x][y].y - 45
            end

            end
    end
    
end

function Lvls:construction()
    setContext(self.image)
    sprite(self.image,0,0)
    for k,v in pairs(platform) do
        for a,b in pairs(platform[k]) do
        sprite("Documents:RhPlatform",b.x,b.y,512/2,512/2)
        
    end
    end
    setContext()
  
end

function Lvls:draw()
    -- Codea does not automatically call this method
    background(0,0,0)
    
    self:construction()
    hero:draw()
    monsters:draw()
    monsters1:draw()
    monstersfr:draw()
   -- bonuses:draw()
end

function Lvls:touched(touch)
    -- Codea does not automatically call this method
end

@lupino8211 - Try this, and see notes underneath

Lvls = class()

function Lvls:init(x)
    -- you can accept and set parameters here
    platform = {}
    self:createBackground() --(1)
    for x = 1,size do
        platform[x] = {}
        for y = 1,size do
            platform[x][y] = vec2(150+x*90,50+y*90)
            if math.fmod(x,2) == 0 then
                platform[x][y].y = platform[x][y].y - 45
            end
        end
    end
end

function Lvls:createBackground()
    self.image = image(500,500) --(2)
    setContext(self.image)
    for k,v in pairs(platform) do
        for a,b in pairs(platform[k]) do
            sprite("Documents:RhPlatform",b.x,b.y,512/2,512/2)
        end
    end
    setContext()
end

function Lvls:draw()
    background(0,0,0)
    sprite(self.image,WIDTH/2,HEIGHT/2) --(3)
    hero:draw()
    monsters:draw()
    monsters1:draw()
    monstersfr:draw()
   -- bonuses:draw()
end

Notes - see the lines numbered --(1), --(2) above…

(1) when setting up the class, create the image (just once)

(2) create a blank image, use setContext to tell Codea to draw on it, then draw all your platforms. Now you have one image with all your platforms on it.

(3) in draw, sprite your image on the screen. I assumed you wanted it in the middle, if not, just put it where you want it.

@Ignatz Huh, i copied and pasted , but background still black. Maybe problem in another place?

@Ignatz and thank you alot for these notes, now i understand how it work, but background still black

@Ignatz , help me please. Your code dont work. I dont know why