3.2.7 readImage seems not to work

Following code draws no sprite at 500,500 but does draw her at 600,600.

-- imagecopy

function setup()
    img = readImage(asset.builtin.Planet_Cute.Character_Princess_Girl)
end

-- This function gets called once every frame
function draw()
    -- This sets a dark background color 
    background(40, 40, 50)
    spriteMode(CENTER)
    sprite(img, 500,500)
    sprite(asset.builtin.Planet_Cute.Character_Princess_Girl,600,600)
    stroke(255)
    strokeWidth(3)
    noFill()
    rectMode(CENTER)
    rect(600,600,100,100)
end

OK this is weird. I changed the above program to print(img.width,img.height) and it started to work. Removing the print, it still works. Closing the program, still works.

exiting codea, and it still works. I confess total confusion. Change the sprite referred to, still works. Absolute confusion now.

Worked OK for me the first time. Haven’t noticed any problems with readImage in the past.

OK. This code prints false, false. I believe this means that the result of readImage is all zero. And, of course, so is the copy.

Something is borked with `readImage, but it isn’t consistent. My prior example at first did not draw the sprite, then later it did.

-- readCopyImage
function setup()
    img = readImage(asset.builtin.Space_Art.Asteroid_Large)
    print(checkBits(img))
    jmg = img:copy()
    print(checkBits(img))
end

function draw()
    background(40, 40, 50)
end

function checkBits(anImage)
    for x = 1,anImage.width do
        for y = 1,anImage.height do
            local r,g,b,a = anImage:get(x,y)
            if r+g+b+a ~= 0 then return true end
        end
    end
    return false
end

Yes, it’s weird, Dave. It failed, then worked. And there’s no doubt that the shields in Invaders are blank. They are read with readImage and copied with image.copy to get unique copies for each, for individual damage.

An extended version of the same program. Display shown at https://ronjeffries.com/articles/020-dung/01ff/d-17/spritebug.png

-- readCopyImage
function setup()
    img = readImage(asset.builtin.Space_Art.Asteroid_Large)
    print(checkBits(img))
    jmg = img:copy()
    print(checkBits(img))
end

function draw()
    background(40, 40, 50)
    stroke(255)
    strokeWidth(2)
    noFill()
    rectMode(CENTER)
    spriteMode(CENTER)
    ellipseMode(CENTER)
    sprite(img,500,500)
    sprite(jmg,600,600)
    sprite(asset.builtin.Space_Art.Asteroid_Large,700,700)
    ellipse(500,500, 200,200)
    rect(600,600, 200,200)
    ellipse(700,700,200,200)
end

function checkBits(anImage)
    for x = 1,anImage.width do
        for y = 1,anImage.height do
            local r,g,b,a = anImage:get(x,y)
            if r+g+b+a ~= 0 then return true end
        end
    end
    return false
end

I’ve seen it work, and I’ve seen it not work. Sometimes with the same darn program. I am confusée.

readImage seems to work, but :copy messes things up. Run the below code as is. It displays the large asteroid. Tap the screen and the cloudy nebula shows. Uncomment the kmg line and run again. Tap the screen and the nebula shows but the asteroid disappears. Uncomment the lmg line and run. When you tap the screen, everything disappears. The print statements show that the kmg and lmg have values when they’re uncommented.

@Simeon looks like there’s a problem.

PS. This appears to work OK with version 3.2.6 (242).

function setup()
    img = readImage(asset.builtin.Space_Art.Asteroid_Large)
    print(img)
end

function draw()
    background(40, 40, 50)
    sprite(img,WIDTH/2,HEIGHT/2+200)
    if a then
        sprite(jmg,WIDTH/2,HEIGHT/2-200)
    end
end

function touched(t)
    if t.state==BEGAN then
        jmg = readImage(asset.builtin.Space_Art.Cloudy_Nebula)
        --kmg = img:copy()
        --lmg = jmg:copy()
        print("jmg\
",jmg)
        print("kmg\
",kmg)
        print("lmg\
",lmg)
        a=true
    end
end

@dave1707 @RonJeffies - I can confirm your observations. Tried to see if viewer.mode and blanking out background() had any effect. Also triedsprites from my documents and Dropbox with no change. But measured spriteSize() and that was consistent, copy seemed to work in those terms but no display of the sprites.
Code below, but - I did notice a flash on the screen just after running the project. Think the sprite routine may be at fault or the download of the sprite data.


-- readCopyImage

viewer.mode = OVERLAY
function setup()
    sW,sH,cW,cH = WIDTH, HEIGHT, WIDTH/2, HEIGHT/2
    img = readImage(asset.documents.Dropbox.falling.Bt)
    imgW,imgH = spriteSize(img)
    print(imgW.." : "..imgH)
    jmg = img:copy(1,1,imgW,imgH)
    jmgW,jmgH = spriteSize(jmg)
    print(jmgW.." : "..jmgH)
end

function draw()
    background(40, 40, 50)
    spriteMode(CENTER)
    sprite(img,cW,cH+200)
    sprite(jmg,cW,cH-200)
end

@Simeon - 328 (244) wow, that’s what I call rapid service. Now seems to be working well but needs further testing. Thank you.

if it’s just copy, why does the checkbits in my example say there are no bits in img?

-- readCopyImage
function setup()
    img = readImage(asset.builtin.Space_Art.Asteroid_Large)
    print(checkBits(img))
    jmg = img:copy()
    print(checkBits(jmg))
end

function draw()
    background(40, 40, 50)
    stroke(255)
    strokeWidth(2)
    noFill()
    rectMode(CENTER)
    spriteMode(CENTER)
    ellipseMode(CENTER)
    sprite(img,500,500)
    sprite(jmg,600,600)
    sprite(asset.builtin.Space_Art.Asteroid_Large,700,700)
    ellipse(500,500, 200,200)
    rect(600,600, 200,200)
    ellipse(700,700,200,200)
end

function checkBits(anImage)
    for x = 1,anImage.width do
        for y = 1,anImage.height do
            local r,g,b,a = anImage:get(x,y)
            if r+g+b+a ~= 0 then return true end
        end
    end
    return false
end

@RonJeffries - is the result with the code you just posted after updating to 328(244) ?

No, that’s 3.2.7 is 3.2.8 on the app store?

@RonJeffries - yes and I’ve checked out some of your code and mine on it and it works - but we need to check further and make sure poistioning etc is OK.

i’m only seeing 3.2.7 in app store. are you running a beta?

@RonJeffries 3.2.8 is a beta version only available to beta testers.

thot so. i wonder what the bug really is. it’d be nice to know.

@RonJeffries Here’s what was on the comment for the new release.

Fixes image:copy / get / set APIs

does my final example above run on .8? it doesn’t seem to be a copy problem. i guess it could be a get problem …

@RonJeffries Three asteroids were displayed and 2 true printed.