Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

[NEED HELP] Go To Scene error
Started by marco.merlini May 10 2019 01:19 PM

1 reply to this topic
composer scene table
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

marco.merlini

[GLOBAL: userInfoPane.html]
marco.merlini
  • Observer

  • 1 posts
  • Corona SDK

Hi everyone,

 

I'm new to Corona SDK and I'm having troubles switching from Menu to Game scene after putting most of the object code inside the Create Scene function.

 

Menu scene: 

 

 
local composer = require( "composer" )
 
local scene = composer.newScene()
 
-- -----------------------------------------------------------------------------------
-- Code outside of the scene event functions below will only be executed ONCE unless
-- the scene is removed entirely (not recycled) via "composer.removeScene()"
-- -----------------------------------------------------------------------------------
 
local function gotoGame()
    composer.gotoScene( "game", { time=800} )
end
 
-- -----------------------------------------------------------------------------------
-- Scene event functions
-- -----------------------------------------------------------------------------------
 
-- create()
function scene:create( event )
 
local sceneGroup = self.view
-- Code here runs when the scene is first created but has not yet appeared on screen
 
local background = display.newImageRect( sceneGroup, "background.png", 400, 750 )
    background.x = display.contentCenterX
    background.y = display.contentCenterY
 
local playButton = display.newText( sceneGroup, "Play", display.contentCenterX, 470, native.systemFont, 44 )
    playButton:setFillColor( 0.2, 0.2, 0 )
 
playButton:addEventListener( "tap", gotoGame )
 
end
 
 
-- show()
function scene:show( event )
 
local sceneGroup = self.view
local phase = event.phase
 
if ( phase == "will" ) then
-- Code here runs when the scene is still off screen (but is about to come on screen)
 
elseif ( phase == "did" ) then
-- Code here runs when the scene is entirely on screen
 
end
end
 
 
-- hide()
function scene:hide( event )
 
local sceneGroup = self.view
local phase = event.phase
 
if ( phase == "will" ) then
-- Code here runs when the scene is on screen (but is about to go off screen)
 
elseif ( phase == "did" ) then
-- Code here runs immediately after the scene goes entirely off screen
 
end
end
 
 
-- destroy()
function scene:destroy( event )
 
local sceneGroup = self.view
-- Code here runs prior to the removal of scene's view
 
end
 
 
-- -----------------------------------------------------------------------------------
-- Scene event function listeners
-- -----------------------------------------------------------------------------------
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )
-- -----------------------------------------------------------------------------------
 
return scene

 

 

Game scene:

 
local composer = require( "composer" )
 
local scene = composer.newScene()
 
-- -----------------------------------------------------------------------------------
-- Code outside of the scene event functions below will only be executed ONCE unless
-- the scene is removed entirely (not recycled) via "composer.removeScene()"
-- -----------------------------------------------------------------------------------
 
 
local physics = require( "physics" )
physics.start()
physics.setGravity(0,0)
 
--background
local background
 
--bricks / ball / goal
local rect
local rect2
local rect3
local rect4
local rect5
local rect6
local rect7
local rect8
local rect9
local rect10
local rect11
local rect12
local rect13
local rect14
local rect15
 
local ball
 
local goal
 
 
physics.addBody( ball, "dynamic", {bounce=0} )
ball.isFixedRotation = true
 
physics.addBody( goal, "static", {bounce=0} )
 
physics.addBody(rect, "static",{bounce=0})
physics.addBody(rect2, "static",{bounce=0})
physics.addBody(rect3, "static",{bounce=0})
physics.addBody(rect4, "static",{bounce=0})
physics.addBody(rect5, "static",{bounce=0})
physics.addBody(rect6, "static",{bounce=0})
physics.addBody(rect7, "static",{bounce=0})
physics.addBody(rect8, "static",{bounce=0})
physics.addBody(rect9, "static",{bounce=0})
physics.addBody(rect10, "static",{bounce=0})
physics.addBody(rect11, "static",{bounce=0})
physics.addBody(rect12, "static",{bounce=0})
physics.addBody(rect13, "static",{bounce=0})
physics.addBody(rect14, "static",{bounce=0})
physics.addBody(rect15, "static",{bounce=0})
 
 
local function gotonextlevel()
    composer.gotoScene( "leveltwo", { time=800} )
end
 
 
local function globalTouchHandler(event)
 
    local swipeLength = math.abs(event.x - event.xStart) 
local swipeHeight = math.abs(event.y - event.yStart) 
    print(event.phase, swipeLength)
print(event.phase, swipeHeight)
 
    local t = event.target
    local phase = event.phase
 
    if (phase == "began") then
 
    elseif (phase == "moved") then
 
    elseif (phase == "ended" or phase == "cancelled") then
 
local vx,vy = ball: getLinearVelocity()
if vx+vy~=0 then
--do nothing
 
        elseif (event.xStart > event.x and swipeLength > 40
and math.abs(event.x - event.xStart) > math.abs(event.y - event.yStart)) then 
            -- left
ball:applyForce(-12, 0, ball.x, ball.y)
        elseif (event.xStart < event.x and swipeLength > 40
and math.abs(event.x - event.xStart) > math.abs(event.y - event.yStart)) then  
            -- right
ball:applyForce(12, 0, ball.x, ball.y)
        elseif (event.y < event.yStart and swipeHeight > 40
and math.abs(event.y - event.yStart) > math.abs(event.x - event.xStart)) then
-- up
ball:applyForce(0, -15, ball.x, ball.y)
elseif (event.y > event.yStart and swipeHeight > 40
and math.abs(event.y - event.yStart) > math.abs(event.x - event.xStart)) then
-- down    
ball:applyForce(0, 15, ball.x, ball.y) 
        end
    end
ball:applyTorque(0)
end
 
 
local function onCollision( event )
 
    if ( event.phase == "began" ) then
 
        local obj1 = event.object1
        local obj2 = event.object2
 
        if ( ( obj1.myName == "rect" and obj2.myName == "ball" ) or
( obj1.myName == "rect2" and obj2.myName == "ball" ) or
( obj1.myName == "rect3" and obj2.myName == "ball" ) or
( obj1.myName == "rect4" and obj2.myName == "ball" ) or
( obj1.myName == "rect5" and obj2.myName == "ball" ) or
( obj1.myName == "rect6" and obj2.myName == "ball" ) or
( obj1.myName == "rect7" and obj2.myName == "ball" ) or
( obj1.myName == "rect8" and obj2.myName == "ball" ) or
( obj1.myName == "rect9" and obj2.myName == "ball" ) or
( obj1.myName == "rect10" and obj2.myName == "ball" ) or
( obj1.myName == "rect11" and obj2.myName == "ball" ) or
( obj1.myName == "rect12" and obj2.myName == "ball" ) or
( obj1.myName == "rect13" and obj2.myName == "ball" ) or
( obj1.myName == "rect14" and obj2.myName == "ball" ) or
( obj1.myName == "rect15" and obj2.myName == "ball" ) )
        then
            -- Set force to zero
 
ball:setLinearVelocity(0,0)
ball:applyForce(0,0)
--ball:applyTorque(0,0)
--ball.rotation = 0
--ball.angularVelocity = 0
 
        elseif ( obj1.myName == "ball" and obj2.myName == "goal" ) 
        then
            --go to next level
gotonextlevel()
 
        end
    end
end
 
 
local function checkball()
if (ball.x > display.contentWidth or
ball.x < 0 or
ball.y > display.contentHeight or
ball.y < 0) 
then
ball.x = 135
ball.y = 400
ball:setLinearVelocity(0,0)
ball:applyForce(0,0)
ball:applyTorque(0,0)
ball.rotation = 0
ball.angularVelocity = 0
end 
end
 
Runtime:addEventListener("touch",globalTouchHandler)
Runtime:addEventListener("enterFrame", checkball)
 
-- -----------------------------------------------------------------------------------
-- Scene event functions
-- -----------------------------------------------------------------------------------
 
-- create()
function scene:create( event )
 
local sceneGroup = self.view
 
background = display.setDefault( "background", 0.894, 0.945, 0.996 )
sceneGroup: insert(background)
 
rect = display.newRoundedRect(300,400,30,30,4)
rect:setFillColor( 0.45 )
sceneGroup: insert(rect)
 
rect2 = display.newRoundedRect(300,50,30,30,4)
rect2:setFillColor( 0.45 )
sceneGroup: insert(rect2)
 
rect3 = display.newRoundedRect(270,80,30,30,4)
rect3:setFillColor( 0.45 )
sceneGroup: insert(rect3)
 
rect4 = display.newRoundedRect(45,80,30,30,4)
rect4:setFillColor( 0.45 )
sceneGroup: insert(rect4)
 
rect5 = display.newRoundedRect(75,310,30,30,4)
rect5:setFillColor( 0.45 )
sceneGroup: insert(rect5)
 
rect6 = display.newRoundedRect(75,20,30,30,4)
rect6:setFillColor( 0.45 )
sceneGroup: insert(rect6)
 
rect7 = display.newRoundedRect(135,20,30,30,4)
rect7:setFillColor( 0.45 )
sceneGroup: insert(rect7)
 
rect8 = display.newRoundedRect(135,460,30,30,4)
rect8:setFillColor( 0.45 )
sceneGroup: insert(rect8)
 
rect9 = display.newRoundedRect(45,430,30,30,4)
rect9:setFillColor( 0.45 )
sceneGroup: insert(rect9)
 
rect10 = display.newRoundedRect(15,400,30,30,4)
rect10:setFillColor( 0.45 )
sceneGroup: insert(rect10)
 
rect11 = display.newRoundedRect(240,220,30,30,4)
rect11:setFillColor( 0.45 )
sceneGroup: insert(rect11)
 
rect12 = display.newRoundedRect(270,340,30,30,4)
rect12:setFillColor( 0.45 )
sceneGroup: insert(rect12)
 
rect13 = display.newRoundedRect(135,190,30,30,4)
rect13:setFillColor( 0.45 )
sceneGroup: insert(rect13)
 
rect14 = display.newRoundedRect(300,110,30,30,4)
rect14:setFillColor( 0.45 )
sceneGroup: insert(rect14)
 
rect15 = display.newRoundedRect(15,370,30,30,4)
rect15:setFillColor( 0.45 )
sceneGroup: insert(rect15)
 
ball = display.newCircle(135,400,14)
ball:setFillColor( 0.3,0.4,1 )
sceneGroup: insert(ball)
 
goal = display.newCircle(75,250,10.5)
goal:setFillColor( 0.9,0.8,0 )
sceneGroup: insert(goal)
 
 
end
 
 
-- show()
function scene:show( event )
 
local sceneGroup = self.view
local phase = event.phase
 
if ( phase == "will" ) then
-- Code here runs when the scene is still off screen (but is about to come on screen)
 
elseif ( phase == "did" ) then
-- Code here runs when the scene is entirely on screen
 
end
end
 
 
-- hide()
function scene:hide( event )
 
local sceneGroup = self.view
local phase = event.phase
 
if ( phase == "will" ) then
-- Code here runs when the scene is on screen (but is about to go off screen)
 
elseif ( phase == "did" ) then
-- Code here runs immediately after the scene goes entirely off screen
 
end
end
 
 
-- destroy()
function scene:destroy( event )
 
local sceneGroup = self.view
-- Code here runs prior to the removal of scene's view
 
end
 
-- -----------------------------------------------------------------------------------
-- Scene event function listeners
-- -----------------------------------------------------------------------------------
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )
-- -----------------------------------------------------------------------------------
 
return scene
 

 

 

Previously the game was running as expected when I was creating all the object outside of the create scene function. Now instead I'm getting the following error:

 

ERROR: table expected. If this is a function call, you might have used '.' instead of ':'
Stack traceback:
In function 'error'
in function 'gotoScene'

menu.lua:12: in function '?'

?: in function <?:190>

 

 

Any help is much appreciated



[TOPIC: post.html]
#2

SGS

[GLOBAL: userInfoPane.html]
SGS
  • Corona Geek

  • 2,080 posts
  • Corona SDK

simplify your project until you get no errors - then you will find the error 




[topic_controls]
[/topic_controls]

Also tagged with one or more of these keywords: composer, scene, table