Dealing with images created in a project

Hi there,

I’m experimenting with images created inside a project.

In short, I’m trying the create a background image from scratch, then create another image which contains little sprites, then overlay the latter on top of the former in order to create one last image (which therefore contains both the background image and the sprites).

I do it like this:

bg = image(400,400)
littleSprites = image(400,400)
finalImage = image(400,400)

setContext(bg)
sprite(aLarge-ishImage,0,0)
setContext()

setContext(littleSprites)
for i = 1,5 do
    sprite(Littlesprite..i,math.random(0,400),math.random(0,400))
end
setContext()

setContext(finalImage)
sprite(bg,0,0)
sprite(littleSprites,0,0)
setImage()

Now, I can’t seem to get what I need, but instead, only the littleSprites image on a white background. Could this be because when creating an image, Codea creates a white image, and not a transparent one?

Thanks!

@Rodolphe


function setup()    
    bg = image(400,400)
    littleSprites = image(400,400)
    finalImage = image(400,400)
    
    setContext(bg)
    sprite("Cargo Bot:Startup Screen",0,0)
    setContext()
    
    setContext(littleSprites)
    for i=1,5 do
        sprite("Cargo Bot:Condition Blue",math.random(0,400),math.random(0,400))
    end
    setContext()
    
    setContext(finalImage)
    sprite(bg,200,200)
    sprite(littleSprites,200,200)
    setContext()
end

function draw()
    background(0)
    sprite(finalImage,200,200)
end

Codea creates transparent images, not white images. However, you’re drawing the sprites at 0, 0. I think you mean to draw them at 200, 200 because the default spriteMode is CENTER. Also, did you mean to say for i in 1, 5 do? (proper syntax is for i = 1, 5 do)

Oh thank you so much @dave1707 and @SkyTheCoder, this had been bugging me for an hour :smiley:

@Rodolphe If you don’t need the other images, you can do everything with just the one image.


function setup()    
    finalImage = image(400,400)
    setContext(finalImage)
    sprite("Cargo Bot:Startup Screen",0,0)
    for i=1,5 do
        sprite("Cargo Bot:Condition Blue",math.random(0,400),math.random(0,400))
    end
    setContext()
end

function draw()
    background(0)
    sprite(finalImage,200,200)
end

Oh yes, but the two images are actually done at different times during gameplay, then put together.

@Rodolphe You can still just use one image. Create image1 with the background you want, then later on create the foreground images onto image1.

Oh of course, that would save memory for sure… Thanks !