Runtime Listener causing app to malfunction

I am using the Director Class to set up my game. I have a basic skeleton set up and a button that when pressed brings you to this gameplay screen, and a back button to bring you back to the main menu. This code below works just fine, with building images set up to scroll down the screen. Once I add the Runtime event listener, the app just will not work. When I press the back button the screen just goes black and nothing happens.

I have been pouring over the events and listeners Doc’s for awhile and cant seem to figure out whats going on, but I assume it is something to do with the Runtime being mixed with the backbutton Object listener. Anyone have any insight into this issue? Thanks a lot in advance!!

[code]

module(…, package.seeall)

display.setStatusBar(display.HiddenStatusBar)
– Main function - MUST return a display.newGroup()
function new()
local localGroup = display.newGroup()

local buildingLeft1 = display.newImage(“buildingLeft.png”)
buildingLeft1.x = 11
buildingLeft1.y = 480

local buildingLeft2 = display.newImage(“buildingLeft.png”)
buildingLeft2.x = 11
buildingLeft2.y = 0

local buildingRight1 = display.newImage(“buildingRight.png”)
buildingRight1.x = 309
buildingRight1.y = 480

local buildingRight2 = display.newImage(“buildingRight.png”)
buildingRight2.x = 309
buildingRight2.y = 0

local backButton = display.newImage(“backbutton.png”)
local function goToMainMenu( event )
if event.phase == “ended” then
director:changeScene(“MainMenu”, “fade”)
end
end
backButton:addEventListener(“touch”,goToMainMenu)
backButton.x = 160
backButton.y = 420

localGroup:insert(backButton)
localGroup:insert(buildingLeft1)
localGroup:insert(buildingLeft2)
localGroup:insert(buildingRight1)
localGroup:insert(buildingRight2)

local tPrevious = system.getTimer()

local speed = 1.2
local bgSpeed = .1

local function move(event)
local tDelta = event.time - tPrevious
tPrevious = event.time

local yOffset = ( 0.2 * tDelta )

buildingLeft1.y = buildingLeft1.y + yOffset*speed
buildingRight1.y = buildingRight1.y + yOffset*speed
buildingLeft2.y = buildingLeft2.y + yOffset*speed
buildingRight2.y = buildingRight2.y + yOffset*speed

if buildingLeft1.y > 720 then
buildingLeft1:translate( 0 , -480*2 )
end

if buildingRight1.y > 720 then
buildingRight1:translate( 0 , -480*2 )
end

if buildingLeft2.y > 720 then
buildingLeft2:translate( 0 , -480*2 )
end

if buildingRight2.y > 720 then
buildingRight2:translate( 0 , -480*2 )
end

end

Runtime:addEventListener(“enterFrame”, move)
– MUST return a display.newGroup()
return localGroup
end

[/code] [import]uid: 9968 topic_id: 3272 reply_id: 303272[/import]

Are you running the corona terminal it will show you what’s happening also add a print statement to each function that way you know if it’s even getting to each function [import]uid: 7911 topic_id: 3272 reply_id: 9841[/import]

Forgive my noob-ness, how exactly do you get the terminal to run with each app? Thanks a lot for the response because that will help from now on! [import]uid: 9968 topic_id: 3272 reply_id: 9857[/import]

Were all nooks at one point
I haven’t been here that long myself
Instead of running the simulator run the corona terminal it’s in the same folder as the simulator and it will bring up the welcome screen and you select your file to run on simulator like before [import]uid: 7911 topic_id: 3272 reply_id: 9858[/import]

Don’t forget to add the print statements the help alot
print( “function name here” ) [import]uid: 7911 topic_id: 3272 reply_id: 9859[/import]

The error im receiving is this:

Runtime error ...sers/User/Desktop/Project Name/GamePlay.lua:57: attempt to perform arithmetic on field 'y' (a nil value) stack traceback: [C]: ? ...sers/User/Desktop/Project Name/GamePlay.lua:57: in function <...sers name><br> ?: in function <?:214><br>

Also got this in the debugger:

<br>Runtime script error at file GamePlay.lua line 56<br>

Not quite sure what the issue is. Thanks for informing me on the proper way to use the terminal! It is a great help!
[import]uid: 9968 topic_id: 3272 reply_id: 9874[/import] </…sers>

Sorry I’m on iPhone and it doesn’t let me scroll your code so it’s all cut off on the ends could you repost without using the block command [import]uid: 7911 topic_id: 3272 reply_id: 9876[/import]

Also I haven’t looked at the director file yet is the code at the top it or is it your main.lua file [import]uid: 7911 topic_id: 3272 reply_id: 9877[/import]

Here is the unblocked code:

module(…, package.seeall)

display.setStatusBar(display.HiddenStatusBar)

– Main function - MUST return a display.newGroup()
function new()
local localGroup = display.newGroup()

local buildingLeft1 = display.newImage(“buildingLeft.png”)
buildingLeft1.x = 11
buildingLeft1.y = 480

local buildingLeft2 = display.newImage(“buildingLeft.png”)
buildingLeft2.x = 11
buildingLeft2.y = 0

local buildingRight1 = display.newImage(“buildingRight.png”)
buildingRight1.x = 309
buildingRight1.y = 480

local buildingRight2 = display.newImage(“buildingRight.png”)
buildingRight2.x = 309
buildingRight2.y = 0

local backButton = display.newImage(“backbutton.png”)
local function goToMainMenu( event )
if event.phase == “ended” then
director:changeScene(“MainMenu”, “fade”)
end
end
backButton:addEventListener(“touch”,goToMainMenu)
backButton.x = 160
backButton.y = 420

localGroup:insert(backButton)
localGroup:insert(buildingLeft1)
localGroup:insert(buildingLeft2)
localGroup:insert(buildingRight1)
localGroup:insert(buildingRight2)

local tPrevious = system.getTimer()

local speed = 1.2
local bgSpeed = .1

local function move(event)

local tDelta = event.time - tPrevious
tPrevious = event.time

local yOffset = ( 0.2 * tDelta )

buildingLeft1.y = buildingLeft1.y + yOffset*speed
buildingRight1.y = buildingRight1.y + yOffset*speed
buildingLeft2.y = buildingLeft2.y + yOffset*speed
buildingRight2.y = buildingRight2.y + yOffset*speed

if buildingLeft1.y > 720 then
buildingLeft1:translate( 0 , -480*2 )
end

if buildingRight1.y > 720 then
buildingRight1:translate( 0 , -480*2 )
end

if buildingLeft2.y > 720 then
buildingLeft2:translate( 0 , -480*2 )
end

if buildingRight2.y > 720 then
buildingRight2:translate( 0 , -480*2 )
end

end

Runtime:addEventListener(“enterFrame”, move)
– MUST return a display.newGroup()
return localGroup
end

The director code is in a separate director class that I downloaded from the code exchange section. The terminal says it is specifically line 56 and dealing with the y value. [import]uid: 9968 topic_id: 3272 reply_id: 9885[/import]

Ok now is this ur main.lua [import]uid: 7911 topic_id: 3272 reply_id: 9886[/import]

Try moving backButton.x and .y to above function
Lines 55 & 56 [import]uid: 7911 topic_id: 3272 reply_id: 9889[/import]

No luck with moving the values. What ive been showing you this whole time is the GamePlay.lua file.

My main.lua is set up like this:

display.setStatusBar( display.HiddenStatusBar )

– Import director class
local director = require(“director”)

– Create a main group
local mainGroup = display.newGroup()

– Main function
local function main()

– Add the group from director class
mainGroup:insert(director.directorView)

– Change scene without effects
director:changeScene(“mainMenu”)

return true
end

– Begin
main()

– It’s that easy! :slight_smile:

This is directly from the director class I downloaded. The mainmenu.lua just has a couple buttons that when pressed open a new file with this same director:changeScene() function, such as options.lua and gameplay.lua. It is a way to switch between screens using multiple files. So far I have not found a way that I understand how to do this, that works well!

Once in gameplay and only in gameplay, if I press the back button, I get the black screen and these errors. This is also the only file so far that im using more than one listener and one is Runtime and the other is an object listener. If I remove the runtime listener, the button works perfectly, but I need that to scroll the background images, unless there is a better way to pull off an infinite scroll.

The scroll code is taken from the samples provided in the sdk. Sorry about my late responses, im at school so im going back and forth.

[import]uid: 9968 topic_id: 3272 reply_id: 9891[/import]

ok i copied your file into a main.lua file removed line 1 and supplied my own images and it all works fine maybe you need to call director from this file idk i havent played with director so not sure how its setup to work but theres nothing wrong with your code [import]uid: 7911 topic_id: 3272 reply_id: 9892[/import]

Thanks for checking it out, I guess ill have to start playing with the arrangement of files and where the code is located. At least I know the code is solid! Thanks a lot! [import]uid: 9968 topic_id: 3272 reply_id: 9894[/import]

try changing goTOMainMenu function to this

local goToMainMenu = function( event )

then if that doesnt work leave as above remove the if end statement leave the changeScene line andchange your listener from “touch” to “tap”
[import]uid: 7911 topic_id: 3272 reply_id: 9895[/import]

The problem here is that once your NEW function is done, the object buildingLeft1 is NIL because it was defined as local inside that fucntion and so gets garbage collected, then later on, furing the “enterframe” event, you call MOVE, and there buildingLeft1 is unknown and so has no property called y.

I would try this:

[lua]module(…, package.seeall)

display.setStatusBar(display.HiddenStatusBar)
local buildingLeft1

– Main function - MUST return a display.newGroup()
function new()
local localGroup = display.newGroup()

buildingLeft1 = display.newImage(“buildingLeft.png”)

…[/lua] [import]uid: 5712 topic_id: 3272 reply_id: 9942[/import]

No such luck unfortunately. I guess I need to go back to the Docs some more and get a better grasp on how this all works because I just cant figure it out!

If anyone is interested in potentially taking a look at the entire project folder of mine, my email is kbegeman888 (at) gmail dot com.

Thanks for everyones help! [import]uid: 9968 topic_id: 3272 reply_id: 10033[/import]

When backbutton is pressed, you can try to add a code line to cancel the timer.

e.g.
local backbutton = function (event)
timer.cancel (tPrevious) --to remove your timer
director:changeScene(“MainMenu”, “fade”)
end

Have a look, perhaps it can help you:
http://developer.anscamobile.com/forum/2010/10/22/systemgettimer

[import]uid: 8970 topic_id: 3272 reply_id: 10070[/import]

“Once in gameplay and only in gameplay, if I press the back button, I get the black screen and these errors. This is also the only file so far that im using more than one listener and one is Runtime and the other is an object listener. If I remove the runtime listener, the button works perfectly, but I need that to scroll the background images, unless there is a better way to pull off an infinite scroll.”

I had a similar problem; I’m doing a scrolling background too and I couldn’t work it out - but I could be way off on what what you’re actually asking, so we’ll see.

If you have a standard menu with a background that just sits there and then a game screen with a scrolling background, I’d set your back button to remove your runtime event listener.

Runtime:removeEventListener("enterFrame", move)  

Can you try adding that to your back button and let me know your results, please?

I’m still VERY new to Corona but as we seem to be working on projects that are similar in how they work, and I was able to solve my problem, I’d like to try and help at least. :slight_smile: [import]uid: 10144 topic_id: 3272 reply_id: 10094[/import]

@peach
Yeah imthought of that last nigh also but didn’t work
@Kyle
Im not at home so can’t try it but give this a try

Change your goToMainMenu listener to go to a new function called
stopAnimation
And remove event from goToMainMenu

Then add this
local function stopAnimation(event)
Runtime:removeEventListener(“enterframe”,move)
timer:performWithDelay(250,goToMainMenu)
end [import]uid: 7911 topic_id: 3272 reply_id: 10097[/import]