Level selector

I will check that out after I’m done with this project Jay. Thanks for the tip!

I got a question, I have now added the level selection buttons to the screen, I’ve added them in the " createScene function "

I need to add object.id to them aswell.

object = display.newImage("btn1lvl.png") object:translate( 250, 200) screenGroup:insert ( object )

How would I go forward when adding the object.id, I tried to add it under the " display.newImage "
But I get a error when trying it in the sim.

Thanks!

EDIT:

Writing the code under “object:translate” works. I got it.

Hi again guys, having some trouble with onObjectTouch

 

function scene:createScene( event ) local screenGroup = self.view image = display.newImage("BGlvl.jpg", centerX, centerY) screenGroup:insert ( image ) object = display.newImage("btn1lvl.png") object:translate( 250, 200) object.id = "level1" screenGroup:insert ( object ) object = display.newImage("btn2lvl.png") object:translate( 400, 200) object.id = "level2" screenGroup:insert ( object ) object = display.newImage("btn3lvl.png") object:translate( 550, 200) object.id = "level3" screenGroup:insert ( object )

I have that in the createScene function.

And I’m calling the objects from the enterScene function like this : 

 

function scene:enterScene( event ) local function onObjectTouch( event ) local level1 = event.target.id if event.phase == "began" then storyboard.gotoScene( "level1", "fade", 400) return true end end object:addEventListener( "touch", onObjectTouch ) print( "1: enterScene event" ) -- remove previous scene's view storyboard.purgeScene( "start" ) end

I’ve only made one touch listener, just for trying out.

Everything works OK, but when I’m in the level selection screen in the SIM.

If I try to press the image 1 to go to level1.lua, nothing happens.

If I press image 15 to go to level15.lua it works.

Do you guys have any idea what it could be ?

 

storyboard.gotoScene( "level1", "fade", 400 ) 

It doesn’t matter what I put in “level” if I put level1 or level 6, it’s still only allowing me to press the “level15button” to go to the next screen.

you’re using the same object variable ‘object’ so only the last instance will be valid.

You need to either name your variables object1, object2 etc or better still use an array.

I’m not sure how “array” works, might have to look for some tutorials. I tried changing names of the objects, but I get a error.

You can reuse the same object name (although in some cases it’s not a good idea) but you’d need to format your code differently.

Here’s one option:

local function onObjectTouch(event) local goto = event.target.id if event.phase == "began" then storyboard.gotoScene( goto, {effect="fade", time=400}) return true end end local function createDisplay() local image = display.newImage("BGlvl.jpg", centerX, centerY) scene.view:insert ( image ) local startXPos = 250 for x = 1, 3 do local object = display.newImage("btn" .. x .. "lvl.png") object:translate( startXPos, 200) object.id = "level" .. x object:addEventListener( "touch", onObjectTouch ) scene.view:insert ( object ) startXPos = startXPos + 150 end end function scene:createScene( event ) local screenGroup = self.view createDisplay() end function scene:enterScene( event ) print( "1: enterScene event" ) -- remove previous scene's view storyboard.purgeScene( "start" ) end

I haven’t actually run that code, but I think it’s correct. First thing I did was pull most of the code out of the “official” scene functions – that makes reuse in the future much easier. And if you end up wanting to “restart” a level it makes it a breeze if you don’t have stuff crammed into the createScene and enterScene functions.

There’s also a loop that puts three buttons on the screen, 150 pixels apart. And I think your syntax was off in storyboard.gotoScene(). I changed it to match the docs.

See if that helps.

 Jay

Thanks Jay, not happy with re-doing this, but I’ll guess I have to. I’ve made 15 buttons.                                                                                                

 

local object = display.newImage("btn" .. x .. "lvl.png") object:translate( startXPos, 200) object.id = "level" .. x object:addEventListener( "touch", onObjectTouch ) scene.view:insert ( object ) startXPos = startXPos + 150 end

How would I go about adding more buttons? do I just  add this several time, with different info ofc.

 

 

No, you change the 

for x = 1, 3 do  

to

for x = 1, 15 do  

in order to do 15 buttons. If you don’t know about loops you should go dig into them. Here’s a video I made on that topic:

https://www.youtube.com/watch?v=N3lmLaywy0U

It’s the 5th in a series of using Lua, so if you need a refresher, watch them all. :slight_smile:

The main problem with moving from 3 to 15 buttons is the placement. The code I wrote assumes they’re all at the same Y coordinate and you can do that with 15 buttons – as long as they’re small. Otherwise they’ll spill off the screen.

So you could create a table of locations for the buttons, which would look something like this:

 local btnPos = { {250,200}, {400,200}, {550,200}, {250,350}, {400,350}, {550,350} } for x = 1, #btnPos do local object = display.newImage("btn" .. x .. "lvl.png") object:translate( btnPos[x][1], btnPos[x][2]) object.id = "level" .. x object:addEventListener( "touch", onObjectTouch ) scene.view:insert ( object ) startXPos = startXPos + 150 end

That btnPos is a table of button positions, the X and Y for each button. That loop shows six buttons because I didn’t want to wrap on the screen, but you could keep adding more sets of X/Y positions for as many buttons as you want.

The start of the loop now uses #btnPos to show how many times to loop – it says how many elements (sets of X/Y coordinates) are in that table.

 Jay

Thanks I’ll look into that :slight_smile:

But I need to write object.id to the buttons.

how do I attach the id to each button ? 

Thanks again Jay!

This line in the code above does it:

object.id = “level” … x

So you’ll end up with each button having an id that’s level1, level2, level3, etc.

That x variable that get’s concatenated is the one in the for loop that goes from 1 to however many buttons you have.

 Jay

I got some issues with physics.
 

local function createDisplay() local image = display.newImage( "gameBg1.jpg", centerX, centerY ) scene.view:insert ( image ) local startXpos = 30 for x =1, 40 do local block = display.newImage("blocktest1.png") block:translate( startXpos, 600) scene.view:insert ( block ) startXpos = startXpos + 40 physics.addBody(block, "static", {friction=0.5, bounce=0.3 } ) --local startXpos = 100 local ball = display.newImage("coconut.png") ball:translate(400, 20 ) scene.view:insert (ball) physics.addBody(ball, {density = 0.6, friction = 0.6, bounce= 0.6, radius = 19}) end end

When the ball hit’s the ground, there’s suddenly (almost like a explosion) 20 balls.

 

I’m thinking it might be the code that put’s the ground. 
I wrote that code so I didn’t have to put every single block(ground) in code.

I’m not sure if that will work. Anyone had any experience with that ?

Your do statement should end earlier since you are not creating 40 balls but 40 blocks only.

local function createDisplay() local image = display.newImage( "gameBg1.jpg", centerX, centerY ) scene.view:insert ( image ) local startXpos = 30 for x =1, 40 do local block = display.newImage("blocktest1.png") block:translate( startXpos, 600) scene.view:insert ( block ) startXpos = startXpos + 40 physics.addBody(block, "static", {friction=0.5, bounce=0.3 } ) end \<--------- move here --local startXpos = 100 local ball = display.newImage("coconut.png") ball:translate(400, 20 ) scene.view:insert (ball) physics.addBody(ball, {density = 0.6, friction = 0.6, bounce= 0.6, radius = 19}) end

Good Luck!

burhan

I see.
 Thanks.

What would be the best option to get a ball to move, to follow the ground.

I’m currently looking at : setLinearVelocity

Do you have any tip?

local object = display.newImage( "level1.png" ) object.id = "level1" local function onObjectTouch( event ) if event.phase == "began" then storyboard.gotoScene( "level1", "fade", 400 ) end return true end object:addEventListener( "touch", onObjectTouch )

Would this be the correct way to go ? 

Hi @sindrekristiansen,

You are almost there but you may add event.target.id.

try this

local object = display.newImage( "level1.png" ) object.id = "level1" local object = display.newImage( "level2.png" ) object.id = "level2" local function onObjectTouch( event ) local level = event.target.id if event.phase == "began" then storyboard.gotoScene( level, "fade", 400 ) end return true end object:addEventListener( "touch", onObjectTouch )

Good Luck!

burhan

Thanks for the reply burthan.

I want multiple level’s on the " Level selector screen " 

Ex:

5 levels, if you press on picture 1 you go to level 1, picture 2 you go to level 2 etc etc

Do I need to add this multiple times ? 

 

local function onObjectTouch( event ) local level = event.target.id if event.phase == "began" then storyboard.gotoScene( level1, "fade", 400 ) local function onObjectTouch( event ) local level = event.target.id if event.phase == "began" then storyboard.gotoScene( level2, "fade", 400 ) 

And how does Corona / Lua know that when you press the first picture, you go to level 1 etc

There’s nothing in that function " onObjectTouch " that tells it when you press pic 1,
you go to Level.Lua

I’m sorry if I’m writing this complicated, I just want to understand this.

Thanks again!

hi,

You just need to create your display objects. 5 pictures in your case.

You add the object.id to them. level1 to level5

in your onObjectTouch  function (created just once) you have

local level = event.target.id .

level will change depending on which image(display object) you clicked.

Then gotoscene will go to the level which you clicked.

burhan

Hi @sindrekristiansen,

If you’re using level director (which i think your are) can you not just add 5 button objects to your scene?

In code you simply attach the onPress event handler to each button which in turn will have the code to launch each level?

2rdcdig.png

I’m getting this error, what could it be ? 
I’m just trying out one button for now.

@retrofitProductions

I’m not using level director right now, I just want to learn basic lua before I jump into LD again.
It’s a verry nice software, but I need to learn the basics first I think.
 

Yes learning the basics is a good idea.

I think from the screenshot it appears that newImage is failing therefore your object var is nil.

Are you sure your image is named correctly and is not in a sub folder?

Yes, the image is named correctly and is not in a sub folder.    Thanks for replying