Errors near brackets

I’m trying to make a game, but in a function it gives me an error with this message: “main.lua.36 unexpected symbol near ‘{’”

Here’s my code:

local function onLevelSwitch( event ) if levelselect==3 then { levelselectvar==1 display.newImage("level select/1.png", 400 , 400) } if levelselect==1 then { levelselectvar==2 display.newImage("level select/2.png") } if levelselect==2 then { levelselectvar==3 display.newImage("level select/3.png") } return true end

It’s not because i didn’t have an addEventListener function, I just didnt copy that part in.

It looks to me like you’re thinking like a C programmer (or Java, JavaScript, ActionScript, etc.)

local function onLevelSwitch( event ) if levelselect==3 then levelselectvar = 1 display.newImage("level select/1.png", 400 , 400) end if levelselect==1 then levelselectvar = 2 display.newImage("level select/2.png") end if levelselect==2 then levelselectvar = 3 display.newImage("level select/3.png") end return true end

Lua uses “end” to end code blocks. Also you were using == for assignment instead of a single =

Rob

I’m actually using a new method for that now. The new method has an error, though, it says

"?:0: attempt to concatenate global ‘sceneName’ (a nil value)

stack traceback:

        ?: in function ‘gotoScene’

        main.lua:35: in function <main.lua:33>

        ?: in function <?:221>

My new code:

local function onLevelSwitch( event ) print ("level switched") composer.gotoScene( "lvlswitch\_2" ) return true end ctrl\_select:addEventListener( "tap", onLevelSwitch )

But thanks for helping me with the first error. :slight_smile:

I think I put “if levelselect == 3 then” in part of it because I’m so used to Game Maker 8.1 coding that I threw in that code.

The error you are getting indicates that there is an error in your levelswitch_2.lua scene. There should be more to the error message, but it will show in a console/terminal window that the simulator opens, not in the dialog box that pops up.  We need to see the whole error message.

Rob

Full error on simulator:

NRgnuUV.png

On console:

6P15P8m.png

I should’ve edited it so that I blur my username or directory on the console log.

There should be more in the console log. Don’t worry about obscuring your directory. Many people feel that they are giving away some secrets but you’re not.

Rob

Well, I usually see people blur their username when their taking pictures of a folder or something, but how do I fix the error? That’s all that was in the console.

EDIT: You can see the window border on the screenshot.

Can you scroll the window up?

The other part of the console log is just a print to log function I put in the code.

But if you want the full thing, here:

ay38yrF.png

The “----------------------- ---- ATARI 2600 ADVENTURE FOR MOBILE…” is the print function I’m talking about.

Can you post the top and bottom of levelswitch_2.lua?

The code for levelswitch_2.lua?

You are executing this line of code:

composer.gotoScene( "lvlswitch\_2" )

You are trying to load a file called lvlswitch_2.lua and execute it as a Composer scene file. Given there are no errors being reported from lvlswitch_2.lua in your error log, the assumption there is either a) the file doesn’t exist or b ) it’s not a valid Composer scene file. Please post the code from that file so we can take a look.

Rob

------------------------------------------ -- -- -- ATARI 2600 ADVENTURE FOR MOBILE -- -- -- ------------------------------------------ --level select local levelselect = display.newImage("level select/menu.png",200,320); -- control objects local ctrl\_dpad\_left = display.newImage("dpad\_left.bmp", -300, 560); local ctrl\_dpad\_up = display.newImage("dpad\_up.bmp", -160, 420); local ctrl\_dpad\_down = display.newImage("dpad\_down.bmp", -160, 560); local ctrl\_dpad\_right = display.newImage("dpad\_right.bmp", -20, 560); local ctrl\_fire = display.newImage("fire.bmp", 620, 580); local ctrl\_select = display.newImage("select.bmp", 620, 440); local ctrl\_start = display.newImage("start.bmp",649,50) local composer = require "composer" local lvlselect2 = display.newImage("level select/2.png",200,300); lvlselect2:scale(2,2) ctrl\_dpad\_up:scale(2,2) ctrl\_dpad\_down:scale(2,2) ctrl\_dpad\_left:scale(2,2) ctrl\_dpad\_right:scale(2,2) ctrl\_fire:scale(4.75,4.75) ctrl\_select:scale(4,4) levelselect:scale(1.801,1.67) ctrl\_start:scale(3,3) levelselect:scale(1.801,1.67) local function onLevelSwitch( event ) print ("level switched") composer.gotoScene( "lvlswitch\_2" ) return true end ctrl\_select:addEventListener( "tap", onLevelSwitch )

Is there another way to easily goto a scene or something? #lua2c0mplik4t3d

If you are going to call composer.gotoScene() to load a scene, that Lua module has to be formatted properly as a Composer Scene file.  That is at the top you must require composer and create the new scene:

local composer = require("compser") local scene = composer.newScene()

The bottom part of the file needs to implement the four Composer event functions and return the scene to the caller:

function scene:create( event ) local sceneGroup = self.view -- your scene create code here end function scene:show( event ) local sceneGroup = self.view params = event.params if event.phase == "did" then -- start any runtime event handlers, physics, timers, transitions, audio, etc. here. end end function scene:hide( event ) local sceneGroup = self.view if event.phase == "will" then -- -- Stop any runtime event handlers, physics, timers, transitions, audio, etc. here. end end function scene:destroy( event ) local sceneGroup = self.view end --------------------------------------------------------------------------------- -- END OF YOUR IMPLEMENTATION --------------------------------------------------------------------------------- scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) return scene

You need to put the right code in the right place. I highly recommend you stop working on your app and spend some time learning how to use Composer. We have a bunch of tutorials and guides on using it.  The better you understand this the easier your life will be when it comes to actually building the rest of your app.

https://coronalabs.com/resources/tutorials/user-interface-scenes-and-widgets/

https://docs.coronalabs.com/guide/system/composer/index.html

Rob

It looks to me like you’re thinking like a C programmer (or Java, JavaScript, ActionScript, etc.)

local function onLevelSwitch( event ) if levelselect==3 then levelselectvar = 1 display.newImage("level select/1.png", 400 , 400) end if levelselect==1 then levelselectvar = 2 display.newImage("level select/2.png") end if levelselect==2 then levelselectvar = 3 display.newImage("level select/3.png") end return true end

Lua uses “end” to end code blocks. Also you were using == for assignment instead of a single =

Rob

I’m actually using a new method for that now. The new method has an error, though, it says

"?:0: attempt to concatenate global ‘sceneName’ (a nil value)

stack traceback:

        ?: in function ‘gotoScene’

        main.lua:35: in function <main.lua:33>

        ?: in function <?:221>

My new code:

local function onLevelSwitch( event ) print ("level switched") composer.gotoScene( "lvlswitch\_2" ) return true end ctrl\_select:addEventListener( "tap", onLevelSwitch )