Refusing to popMatrix()

Hello
Quite new to Codea and trying to put together a simple game in a way which probably seems quite haphazard to experienced users!

At the moment I am trying to animate some objects on screen and then, when they come to a stop, overlay some buttons (which I have adapted from the Sound Buttons example project). However, my buttons are displaying very odd behaviour, including the fact that they aren’t appearing on screen where they are meant to. I’m guessing that this is a problem with the matrices I have been translating and rotating to achieve my animation (even though I’ve been trying to push and pop Matrices religiously). The code I’ve built up is probably too complex to post but one thing I have noticed is that one function I have (the “hit” function, which measures whether a touch coincides with a button) is refusing to let me popMatrix() at the end - an error comes up about expecting an “end” near it, even though I am very sure it has all the “ends” it needs.

I’m hoping this might sound sufficiently familiar that someone can diagnose where I’m going wrong. Any ideas? I have tried so many things that I’ve lost track.

buttons - if your animation is 3D, you need to convert back to 2D before drawing buttons. otherwise hard to say without seeing code.

popMatrix - sounds like a bug that has nothing to do with popMatrix (which doesn’t use end), but hard to say without seeing code. Can you just post the hit function?

How many tabs or total lines of code do you have. It might be OK to PM the code. If the code is written correctly, push and pop matrix always works.

Do you have your code formatted properly. That makes it easier to find problems.

See below the touched and hit functions, extracted from the code. Hit is the only function I have without push and pop Matrix, because even though it will allow the push expressions, it won’t allow the pop expressions at the end.

The error message I get is:-
‘end’ expected near ‘popMatrix’

Of course, it’s completely possible that this is unrelated to my button woes, and there are other bugs I need to locate, but my best guess is that it’s something to do with matrices, because I find that all the buttons I’m trying to draw at (0,0) end up somewhere closer to the middle of the screen, and even then they don’t recognise touches in the way they’re meant to.


function Button:hit(p)
    
    local l = self.pos.x
    local r = self.pos.x + self.size.x
    local t = self.pos.y + self.size.y
    local b = self.pos.y
    if p.x > l and p.x < r and
    p.y > b and p.y < t then
        return true
    end
    
    return false 
    
end

function Button:touched(touch)
    -- Codea does not automatically call this method
    pushStyle()
    pushMatrix()
    if touch.state == ENDED then print(touch.x .. ",  " .. touch.y) end
    if touch.state == ENDED and
    self:hit(vec2(touch.x,touch.y)) then
        print("action!")
        if self.action then
            self.action()
        end
    end
    popMatrix()
    popStyle()
    
end

You are getting an error because you are trying to put code after a return line in a function. return immediately exits the function with the value provided, no further code is executed, so lua hits you with a bug to make sure you don’t expect functionality you won’t get. You can fix this by moving popMatrix() above your return statement(s)

You also don’t need a pushMatrix anyway in the hit function, as you are not rotating anything in there.

I would also consider taking the pushStyle and pushMatrix commands out of the touched function and (if they are needed) put them in the Button:action function, just before you need them. That way, it is easier to figure out exactly what you are pushing and popping.

I can’t comment on the button positioning without seeing more code.

Thanks Jordan/Ignatz - always good to understand my mistakes. I’m pretty sure that’s unrelated to my button placement woes now, but I’m starting to make headway debugging that too.

Apparently you can put a popMatrix() after a return as long as you don’t return a value.


function setup()
end

function xx()
    pushMatrix()
    return
    popMatrix() -- no error on this
end

function yy()
    pushMatrix()
    return(5)
    popMatrix() -- error on this
end

Interesting, thank you! Although it’s far likelier that I’m using push/pop too much than insufficiently!

Can I ask, is the SoundButtons example that comes with Codea considered good coding? Because it’s a devil to follow. I still can’t work out at what point the basic SoundButton function is initialised, although I can see that the function to do so has been put in a table. I think I’m going to search for other simpler button inspirations!

@dave1707 I just checked with lua on my laptop, and the first version of your code is being interpreted as:

function xx()
    pushMatrix()
    return popMatrix()
end

so the popMatrix is being called in that first version.

(Here’s my test code:

function a()
    return 7
end

function b()
    return
    a()
end

print(b())

prints 7, showing that a() has been run.)

@epicurus101 As others have said, it’s hard to debug without the full code, but I would be wary of doing drawing commands inside a touched function. I would use the touched function to set up things so that on the next draw cycle they were drawn appropriately.

@LoopSpace I never thought of return, returning popMatrix() after executing it. I guess that makes sense why it didn’t give an error then.