Newbie help on lua

Hi I’m very new to corona SDK however i am familiar with programming  but I’ve encountered a error that i have no idea how to solve.

I’m trying to make my town image move across the screen through the function moveTown but when i call the method “Runtime:addEventListener(“enterFrame”,moveTown)” the code doesn’t run. Without this line the code works

Please someone help me and explain to me what i’m doing wrong.

…BTW sorry if i posted this in the wrong place


– level1.lua


local storyboard = require( “storyboard” )

local scene = storyboard.newScene()

– include Corona’s “physics” library

local physics = require “physics”

physics.start(); physics.pause()



– BEGINNING OF YOUR IMPLEMENTATION


– Called when the scene’s view does not exist:

function scene:createScene( event )

    local group = self.view

    ship = display.newImage( “spaceship.png”)

    ship.x = 60

    ship.y = 140

    physics.addBody( ship, “dynamic”, {density = .1,bounce = 0.1, friction=2, radius= 2 } )

    

    town = display.newImage( “planet.png”)

    town.x = 500

    town.y = 140

    town.speed = math.random(2,6)

    physics.addBody( town, “static”, {density = .1,bounce = 0.1, friction=2, radius= 2 } )

    town.enterFrame = moveTown

    

    ground = display.newImage( “grass.png”)

    ground:setReferencePoint( display.BottomLeftReferencePoint )

    ground.x = 400

    ground.y = 370

    physics.addBody( ground, “static”, { friction=0.3 } )

    

    bg = display.newImage( “background.png”)

    

    floating = display.newImage( “ground1.png”)

    floating.x = 0

    floating.y = 300

    physics.addBody( floating, “static”, {friction=0.5} )

    – all display objects must be inserted into group

    group:insert( bg )

    group:insert( ground)

    group:insert( ship )

    group:insert( floating )

    group:insert( town )

end

    --Runtime:addEventListener(“enterFrame”, town )

– Called immediately after scene has moved onscreen:

function scene:enterScene( event )

    local group = self.view

    

    physics.start()

    

end

function moveTown(self,event)

    if self.x < -50 then

        self.x = 500

        else

            self.x = self.x - self.speed

            end

end

function flightRight(self,event)

    self:applyForce(0,-0.05,self.x,self.y)

end

function touchScreen(event)

    if event.phase == “began” then

    ship.enterFrame = flightRight

    Runtime:addEventListener(“enterFrame”, ship)

    end

    if event.phase == “ended” then

    Runtime:removeEventListener(“enterFrame”, ship)

    print(“ended”)

    end

end

Runtime:addEventListener(“touch”, touchScreen)

Runtime:addEventListener(“enterFrame”,moveTown)

function scene:exitScene( event )

    local group = self.view

    

    physics.stop()

    

end

function scene:destroyScene( event )

    local group = self.view

    

    package.loaded[physics] = nil

    physics = nil

end

scene:addEventListener( “createScene”, scene )

scene:addEventListener( “enterScene”, scene )

scene:addEventListener( “exitScene”, scene )

scene:addEventListener( “destroyScene”, scene )


return scene

Looks like a lua scope issue. 

Try adding the runtime listener AFTER you declare the function moveTown and not before. 

That didn’t work either, I do believe it is a lua scope issue though as it claims that i’m using a nil value

What is the error message that you are getting?

I’m not certain, but my first guess is that I don’t think you need the “self” parameter when you define the moveTown function. So it should just be moveTown(event). You are using “moveTown” with an “enterFrame” listener, which only applies to the Runtime object. So looking at the code, you are trying to move the Runtime object and I’m not sure if that is possible. Heck, I don’t even really know what the Runtime object is, other than you can define some listeners to it. Looks like I have some reading to do.

Anyway, you probably just want to move “town” object. Since it looks like you using it as a global, just reference it as “town” inside of the moveTown function.

Thank you very much for your help. I took out the self statement and it began to work. Without you explaining the concept of it i wouldn’t have noticed that. Thanks

The whole concept of using “self” I was initially confused by. When you define the function, you have to make sure you define “self” as one of the parameters. So essentially it is just another parameter. Nothing special.
 
However, if you start attaching methods to objects, you can start using the colon “:”. And I find this makes it much easier to read, use and it automatically passes a “self” parameter.

Just for an example, I took town and added a move method to it using the colon operator.
 
[lua]

local town = display.newImage( “planet.png”)
town.x,town.y = 500,140
town.speed = math.random(2,6)
physics.addBody( town, “static”, {density = .1,bounce = 0.1, friction=2, radius= 2 } )
    
function town:move()
   if self.x < -50 then
       self.x = 500
   else
       self.x = self.x - self.speed
   end
end
 
local function checkFrame(event)
   town:move()
end
 
Runtime:addEventListener(“enterFrame”,checkFrame)
[/lua]
 
And when you see examples with other listeners you can typically see “self” being used because the method is attached to the object that will have the listener. If you wanted to drag the town around, for example:
 [lua]
function town:touch(event)
–drag town around
    self.x,self.y = event.x,event.y
end
town:addEventListener(“touch”,town)

[/lua]

Looks like a lua scope issue. 

Try adding the runtime listener AFTER you declare the function moveTown and not before. 

That didn’t work either, I do believe it is a lua scope issue though as it claims that i’m using a nil value

What is the error message that you are getting?

I’m not certain, but my first guess is that I don’t think you need the “self” parameter when you define the moveTown function. So it should just be moveTown(event). You are using “moveTown” with an “enterFrame” listener, which only applies to the Runtime object. So looking at the code, you are trying to move the Runtime object and I’m not sure if that is possible. Heck, I don’t even really know what the Runtime object is, other than you can define some listeners to it. Looks like I have some reading to do.

Anyway, you probably just want to move “town” object. Since it looks like you using it as a global, just reference it as “town” inside of the moveTown function.

Thank you very much for your help. I took out the self statement and it began to work. Without you explaining the concept of it i wouldn’t have noticed that. Thanks

The whole concept of using “self” I was initially confused by. When you define the function, you have to make sure you define “self” as one of the parameters. So essentially it is just another parameter. Nothing special.
 
However, if you start attaching methods to objects, you can start using the colon “:”. And I find this makes it much easier to read, use and it automatically passes a “self” parameter.

Just for an example, I took town and added a move method to it using the colon operator.
 
[lua]

local town = display.newImage( “planet.png”)
town.x,town.y = 500,140
town.speed = math.random(2,6)
physics.addBody( town, “static”, {density = .1,bounce = 0.1, friction=2, radius= 2 } )
    
function town:move()
   if self.x < -50 then
       self.x = 500
   else
       self.x = self.x - self.speed
   end
end
 
local function checkFrame(event)
   town:move()
end
 
Runtime:addEventListener(“enterFrame”,checkFrame)
[/lua]
 
And when you see examples with other listeners you can typically see “self” being used because the method is attached to the object that will have the listener. If you wanted to drag the town around, for example:
 [lua]
function town:touch(event)
–drag town around
    self.x,self.y = event.x,event.y
end
town:addEventListener(“touch”,town)

[/lua]