M = nil?

Can someone tell me whats wrong with this code? I tried what i could do.

Play = class()

local switchTimer = 0
    
-- ripple code made by ignatz.

function Play:setup()
    m=mesh()
    img=readImage("Dropbox:companylogo")
    scale=2
    m:addRect(0,0,img.width*scale,img.height*scale)
    m.texture=img
    m.shader=shader(ripple.v,ripple.f)
    m.shader.freq=1
    m.shader.centre=vec2(0.5,0.5)
    m.pos=vec2(WIDTH/2,HEIGHT/2) --position of image on screen
    m.w,m.h=img.width*scale,img.height*scale
end

function Play:draw()
    switchTimer=switchTimer + 1
    background(40, 40, 50)
    sprite("Dropbox:space",WIDTH/2,HEIGHT/2,1024,768)
    pushMatrix()
    translate(m.pos.x,m.pos.y)
    m.shader.time=ElapsedTime
    m:draw()
    popMatrix()
    font("Futura-CondensedExtraBold")
    fill(60, 63, 62, 255)
    fontSize(50)
    text("CRONUS",WIDTH/2,HEIGHT/2)
    
    -- switch setup
    if switchTimer > 120 then
        Scene.Change("mainmenu")
    end
end

function Play:touched(t)
    if t.state==ENDED and math.abs(t.x-m.pos.x)<=m.w/2 and math.abs(t.y-m.pos.y)<=m.h/2 then
       m.shader.centre=vec2((t.x-m.pos.x)/m.w+.5,(t.y-m.pos.y)/m.h+.5)
        print("touched",m.shader.centre)
    end
end

ripple={
v=[[
uniform mat4 modelViewProjection;
attribute vec4 position;
attribute vec4 color;
attribute vec2 texCoord;
varying lowp vec4 vColor;
varying highp vec2 vTexCoord;

void main()
{
    gl_Position = modelViewProjection * position;
    vColor.rgb = color.rgb * color.a;
    vColor.a = color.a;
    vTexCoord = texCoord;
}
]],
f=[[
uniform lowp sampler2D texture;
varying lowp vec4 vColor;
varying highp vec2 vTexCoord;
uniform highp float time;
uniform highp float freq;
uniform lowp vec2 centre;

void main()
{
    highp vec2 tc = vTexCoord.xy;
    highp vec2 p =  tc-centre; //-1.0 + 2.0 * tc;
    highp float len = length(p);
    highp vec2 uv = tc + (p/len)*freq*cos(len*24.0-time*4.0)*0.03;
    highp vec4 col = texture2D(texture,uv);
    gl_FragColor = vec4(col.rgb * vColor.rgb, col.a);
}
]]
}

Try changing

function Play:setup()

to

function Play:init()

@Codeabin It works for me until switchTimer reaches 120 and it calls Scene.Change() which there isn’t any code for.

Ignatz i already did that, still doesn’t work

Off-topic: @Codeabin use an @ before someone’s username such as: @ExampleUsername to notify them when you say something. (Note: doesn’t notify if you added it via an edit)

@Ignatz, i tried putting that and it still did not work?

@Codeabin As I said above, it works for me. What exactly are you getting that it doesn’t work.

@dave1707 To him it says m is nil.

I think it’s because he’s not using self, and m is being overwritten by something else.

@SkyTheCoder I’m using the code he posted above with @Ignatz change. I added a setup() and draw() function to use the class and it works until switchTimer reaches 120. So that’s why I asked what he was getting that didn’t work. He said he made the change too.

@Codeabin @SkyTheCoder It also works by removing Play from everything in the code above and not using it as a class.

@Codeabin - post the rest of your code that uses this class, and maybe we can see the problem

Heres scene manager.


--     Original code from Brainfox, off the Codea forums

Scene = {}
local scenes = {}
local sceneNames = {}
local currentScene = nil

setmetatable(Scene,{__call = function(_,name,cls)
   if (not currentScene) then
       currentScene = 1
   end
   table.insert(scenes,cls)
   sceneNames[name] = #scenes
   Scene_Select = nil
end})

--Change scene
Scene.Change = function(name)
  currentScene = sceneNames[name]
   scenes[currentScene]:init()
   if (Scene_Select) then
       Scene_Select = currentScene
   end
   collectgarbage()
end

Scene.Draw = function()
   pushStyle()
   pushMatrix()
   scenes[currentScene]:draw()
   popMatrix()
   popStyle()
end

Scene.Touched = function(t)
   if (scenes[currentScene].touched) then
       scenes[currentScene]:touched(t)
   end
end

Scene.Keyboard = function()
   if (scenes[currentScene].keyboard) then
       scenes[currentScene]:keyboard(key)
   end
end

Scene.OrientationChanged = function()
   if (scenes[currentScene].orientationChanged) then
       scenes[currentScene]:orientationChanged()
   end
end

@Ignatz there you go

@Codeabin - the new code you posted doesn’t use the Play class at all.

Why don’t you post ALL your code in a gist, to make it easier to see what is happening?

@ignatz thats the code that runs Scene.Change(“mainmenu”)