Checking aspect

@sim @john - I know there will be a simple explanation for this but I can’t see why this code fires an error up. It’s basically written to capture orientation change. It fires an error up as it is posted but if you rem out the aspect call in function update(dt) then the code works fine. I’m sure I’ve used this in the past without problem.


viewer.mode = FULLSCREEN
function setup()
    --
    init()
end

function update(dt)
    --
    aspect = checkAspect()
end

function draw()
    -- 
    update(DeltaTime)
    background(0)
    if aspect > 1 then
        -- landscape
        pushStyle()
            fill(255, 14, 0)
            font("AmericanTypewriter-Bold")
            fontSize(32)
            textMode(CENTER)
            text("Please switch to Portrait Mode", cW, cH)
        popStyle()
    else
        -- portrait
        
    end
end

function touched(touch)
    --
    t = touch
    if t.state == BEGAN then
        
    end
end

function init()
    --
    aspect = WIDTH/HEIGHT
    sW, sH, cW, cH = WIDTH, HEIGHT, WIDTH//2, HEIGHT//2
end

function checkAspect()
    --
    local check = WIDTH/HEIGHT
    local asp
    if check ~= aspect then 
        asp = WIDTH//HEIGHT 
        sW, sH, cW, cH = WIDTH ,HEIGHT, WIDTH//2, HEIGHT//2
    end
    return asp
end

Edit : The only explanation I can give is that Update() is run before init() - is that the case?

Edit2: I think I know what it is, in the checkAspect()
function the aspect derived is an integer so it’s neither greater or less than one. Will check up when I get chance on my iPad.

Edit3: Modified to obtain a real for asp and it still throws out an error.

This is because checkAspect() can return nil if asp is never assigned — which happens the first time the function is run

If you modify it to

function checkAspect()
    --
    local check = WIDTH/HEIGHT
    local asp = 1.0 -- give asp an initial value
    if check ~= aspect then 
        asp = WIDTH//HEIGHT 
        sW, sH, cW, cH = WIDTH ,HEIGHT, WIDTH//2, HEIGHT//2
    end
    return asp
end

Then it should work because checkAspect() will always return a number

@sim - thanks for the reply, amended code and it worked but had a flickering of the text when in the wrong orientation message screen. Found out that it was due to incorrect asset path resource definitions.

Tha project was stored on iCloud but the assets sources were different and held on both my iPads. I used local assets in each case - an argument for storing assets on iCloud.