(SOLVED) Load images just one time inside an "enterFrame" function

Hi folks, I’m trying to load few images from an enterFrame function: I’ve got a boolean variable that it is initialized @ false; then, I set it @true inside the enterFrame function. when that variable is true,  I load 5 images, but the app performances get worst each frame after frame (obviously this is due to the fact that these 5 images are loaded every millisecond).

The code I’m using is the following:

if(romaHasBeenChoosen==true)then roma[1] = display.newImageRect("immagini/sfondoROMA/bgColor.png", 712 , 150) roma[1].x = \_W/2 roma[1].y = \_H/2 - 250/2 roma[2] = display.newImageRect("immagini/sfondoROMA/bassa1.png",592 , 240) roma[2].x = \_W/2+500/2 roma[2].y = \_H/2-158/2 roma[3] = display.newImageRect("immagini/sfondoROMA/bassa2.png",592 , 240) roma[3].x = roma[2].x+1183/2 roma[3].y = \_H/2 -158/2 roma[4] = display.newImageRect("immagini/sfondoROMA/bassa3.png",592 , 240) roma[4].x = roma[3].x+1183/2 roma[4].y = \_H/2 -158/2 roma[5] = display.newImageRect("immagini/sfondoROMA/nuvolaRoma.png", 202, 98) roma[5].x = \_W/2 - 800/2 roma[5].y = \_H/2 -250/2 roma[5].xScale = 0.8 roma[5].yScale = 0.8 roma[5].alpha = 0.9 end local function cambioScenario() if(immaginiCambioScenario[1].x\<0)then if(landscapeToShowAfterChange==3)then Runtime:removeEventListener("enterFrame", moveRoma) Runtime:removeEventListener("enterFrame", moveUnderWater) Runtime:removeEventListener("enterFrame", moveNewYork) if(dubaiHasBeenChoosen==true)then for i=1, 16 do newYork[i]:removeSelf() newYork[i] = nil end newYorkHasBeenChoosen = false underWaterHasBeenChoosen = false romaHasBeenChoosen = true end end end end Runtime:addEventListener(“enterFrame”, cambioScenario)

Is there another way to load these images outside an enterFrame function? 

Thanks! 

Maybe a description of what your trying to accomplish will help.  enterFrame fires at least 30 times per second (or 60 depending on your settings).  I’m not sure why you would want to load images at that pace.  If you’re doing this to load a bunch of images to use later, enterFrame isn’t the answer.  Arrays (tables) and a for loop are your friend here.

Rob

Thanks Rob.

What I’m trying to accomplish is to load roma[1], roma[2], roma[3], roma[4], roma[5] when immaginiCambioScenario[1].x is <0.

In the code I’ve posted I load these images every frame. The fact is that I don’t want to load them every frame, but just one time (As I said before, when immaginiCambioScenario[1].x is <0): if I code the if condition here below outside of a function it won’t work.

if(immaginiCambioScenario[1].x\<0)then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(landscapeToShowAfterChange==3)then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Runtime:removeEventListener("enterFrame", moveRoma) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Runtime:removeEventListener("enterFrame", moveUnderWater) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Runtime:removeEventListener("enterFrame", moveNewYork) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(dubaiHasBeenChoosen==true)then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for i=1, 16 do &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;newYork[i]:removeSelf() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;newYork[i] = nil &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;newYorkHasBeenChoosen = false &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;underWaterHasBeenChoosen = false &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;romaHasBeenChoosen = true &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;end

How can I create and execute IF statements outside a function? 

Thanks again.

Load them once.  Hide them by setting .isVisible = false on each object.

Then in your enterFrame handler you can use a flag kinda like this:

local isShowing = false

local function cambioScenario()
    if (immaginiCambioScenario[1].x<0) and not isShowing then

          isShowing = true

          roma[1].isVisible = true

etc.

Rob

Thanks Rob, but using “isShowing” is what I used to do before posting my answers.

It was working correctly, but on older devices the game performance were very bad since I got a lot of images in my game.

There isn’t another way to load them once?

Thanks again.

Are you using composer (scenes) or is this all done in the main.lua file?

It sounds and looks like you are going about this all wrong, what are you exactly trying to do?

Hi cublah, actually I’m using composer.

I just want to load roma[1], roma[2], roma[3], roma[4], roma[5] when “immaginiCambioScenario[1].x<0”.

I’ve already tried to load them at the beginning of the scene and then hide them using the “isVisible” property, but I would prefere to save as much texture memory as I can, that’s why I want them to load only when needed, but since “cambioScenario()” is executed each frame, it will load roma[1], roma[2], roma[3], roma[4], roma[5] 60 time a second. 

I just cannot find a way to load them once.

 

Ok, now it works, I have solved by myself. I’ve added an eventListener inside "  if(dubaiHasBeenChoosen==true)" which starts executing the function that “creates” roma[1], roma[2], roma[3], roma[4], roma[5]. Then, inside this function, I remove the event listener.

Thanks anyway.

Maybe a description of what your trying to accomplish will help.  enterFrame fires at least 30 times per second (or 60 depending on your settings).  I’m not sure why you would want to load images at that pace.  If you’re doing this to load a bunch of images to use later, enterFrame isn’t the answer.  Arrays (tables) and a for loop are your friend here.

Rob

Thanks Rob.

What I’m trying to accomplish is to load roma[1], roma[2], roma[3], roma[4], roma[5] when immaginiCambioScenario[1].x is <0.

In the code I’ve posted I load these images every frame. The fact is that I don’t want to load them every frame, but just one time (As I said before, when immaginiCambioScenario[1].x is <0): if I code the if condition here below outside of a function it won’t work.

if(immaginiCambioScenario[1].x\<0)then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(landscapeToShowAfterChange==3)then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Runtime:removeEventListener("enterFrame", moveRoma) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Runtime:removeEventListener("enterFrame", moveUnderWater) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Runtime:removeEventListener("enterFrame", moveNewYork) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(dubaiHasBeenChoosen==true)then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for i=1, 16 do &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;newYork[i]:removeSelf() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;newYork[i] = nil &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;newYorkHasBeenChoosen = false &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;underWaterHasBeenChoosen = false &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;romaHasBeenChoosen = true &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;end

How can I create and execute IF statements outside a function? 

Thanks again.

Load them once.  Hide them by setting .isVisible = false on each object.

Then in your enterFrame handler you can use a flag kinda like this:

local isShowing = false

local function cambioScenario()
    if (immaginiCambioScenario[1].x<0) and not isShowing then

          isShowing = true

          roma[1].isVisible = true

etc.

Rob

Thanks Rob, but using “isShowing” is what I used to do before posting my answers.

It was working correctly, but on older devices the game performance were very bad since I got a lot of images in my game.

There isn’t another way to load them once?

Thanks again.

Are you using composer (scenes) or is this all done in the main.lua file?

It sounds and looks like you are going about this all wrong, what are you exactly trying to do?

Hi cublah, actually I’m using composer.

I just want to load roma[1], roma[2], roma[3], roma[4], roma[5] when “immaginiCambioScenario[1].x<0”.

I’ve already tried to load them at the beginning of the scene and then hide them using the “isVisible” property, but I would prefere to save as much texture memory as I can, that’s why I want them to load only when needed, but since “cambioScenario()” is executed each frame, it will load roma[1], roma[2], roma[3], roma[4], roma[5] 60 time a second. 

I just cannot find a way to load them once.

 

Ok, now it works, I have solved by myself. I’ve added an eventListener inside "  if(dubaiHasBeenChoosen==true)" which starts executing the function that “creates” roma[1], roma[2], roma[3], roma[4], roma[5]. Then, inside this function, I remove the event listener.

Thanks anyway.