So I’m now able to go to the levels.lua,
local object = display.newImage( “level1btn.jpeg”) is wrong, when I changed to jpg it worked.
local object = display.newImage( “level1btn.jpg”) is correct
But the level selection “buttons” is not visible.
So I’m now able to go to the levels.lua,
local object = display.newImage( “level1btn.jpeg”) is wrong, when I changed to jpg it worked.
local object = display.newImage( “level1btn.jpg”) is correct
But the level selection “buttons” is not visible.
Try setting a position too;
object :translate( x, y )
local object = display.newImage( "Shape1.jpg") object.id = "level1" object:translate( 150, 50)
I tried that ? Is that correct ?
It’s still not showing.
Is there another image in front of that one? Corona layers images from back to front, so creating an alien display object and then creating a background display object means the alien won’t be seen.
Also, are you see in any error messages?
Jay
Actually, I noticed from your screenshot that you are creating a new scene via storyboard but have not setup any storyboard events such as enterScene or createScene.
If your coming to this scene from another via storyboard.goto then this will confuse it because it will load your module, display your image, then create the scene.view which will not have your object inside it.
Take a look at the Corona storyboard example and use it as a template.
You can then move for you code to load the image in createScene and add it to the scene group.
--------------------- -- levels.lua --------------------- local storyboard = require ( "storyboard") local scene = storyboard.newScene() --------------------- -- Beginning of code --------------------- local centerX = display.contentCenterX local centerY = display.contentCenterY --local image -- called when the scenes view does not exist: function scene:createScene( event ) local screenGroup = self.view image = display.newImage("BGlvl.jpg", centerX, centerY) image.touch = onSceneTouch print( "n1: createScene event") end local object = display.newImage( "Icon.png" ) object:translate(44, 50 ) object.id = "level1" local function onObjectTouch( event ) local level1 = event.target.id if event.phase == "began" then storyboard.gotoScene( "level1", "fade", 400 ) end return true end object:addEventListener( "touch", onObjectTouch ) -- Called immediately after scene has moved onscreen function scene:enterScene( event ) print( "1: enterScene event" ) -- remove previous scene's view storyboard.purgeScene( "start" ) -- called immediately after scene has moved onscreen function scene:exitScene( event ) print( "1: exitScene event" ) -- remove touch listener for image image:removeEventListener( "touch", image ) end end -- called prior to the removal of scenes view (display group) function scene:destroyScene ( event ) print( "(( destroying start view))") end --------------------------------------------------------------------------------- -- END OF YOUR IMPLEMENTATION --------------------------------------------------------------------------------- -- "createScene" event is dispatched if scene's view does not exist scene:addEventListener( "createScene", scene ) -- "enterScene" event is dispatched whenever scene transition has finished scene:addEventListener( "enterScene", scene ) -- "exitScene" event is dispatched before next scene's transition begins scene:addEventListener( "exitScene", scene ) -- "destroyScene" event is dispatched before view is unloaded, which can be -- automatically unloaded in low memory situations, or explicitly via a call to -- storyboard.purgeScene() or storyboard.removeScene(). scene:addEventListener( "destroyScene", scene ) --------------------------------------------------------------------------------- return scene This is my levels.lua
That code that creates your Icon image and adds the touch event, etc? Throw all that inside a function and then call it from scene:enterScene.
Also, you’ll need to insert any display objects you create into the scene display group, like this:
scene.view:insert(object)
It also looks like you have scene:exitScene() inside of scene:enterScene() – move that out of there to the root of your document.
Start with those and see what happens.
Jay
Finally guys.
After going back and forth, I’ve got the Image to display on the screen, I haven’t added the touch listener to the image.
But I’ll try to do that .
New Lua file
-- levels.lua --------------------- local storyboard = require ( "storyboard") local scene = storyboard.newScene() --------------------- -- Beginning of code --------------------- local centerX = display.contentCenterX local centerY = display.contentCenterY local image local object -- called when the scenes view does not exist: function scene:createScene( event ) local screenGroup = self.view image = display.newImage("BGlvl.jpg", centerX, centerY) screenGroup:insert ( image ) object = display.newImage("Icon.png", centerX, CenterY) screenGroup:insert ( object ) print( "n1: createScene event") end -- Called immediately after scene has moved onscreen function scene:enterScene( event ) print( "1: enterScene event" ) -- remove previous scene's view storyboard.purgeScene( "start" ) -- called immediately after scene has moved onscreen function scene:exitScene( event ) print( "1: exitScene event" ) -- remove touch listener for image image:removeEventListener( "touch", image ) end end -- called prior to the removal of scenes view (display group) function scene:destroyScene ( event ) print( "(( destroying start view))") end --------------------------------------------------------------------------------- -- END OF YOUR IMPLEMENTATION --------------------------------------------------------------------------------- -- "createScene" event is dispatched if scene's view does not exist scene:addEventListener( "createScene", scene ) -- "enterScene" event is dispatched whenever scene transition has finished scene:addEventListener( "enterScene", scene ) -- "exitScene" event is dispatched before next scene's transition begins scene:addEventListener( "exitScene", scene ) -- "destroyScene" event is dispatched before view is unloaded, which can be -- automatically unloaded in low memory situations, or explicitly via a call to -- storyboard.purgeScene() or storyboard.removeScene(). scene:addEventListener( "destroyScene", scene ) --------------------------------------------------------------------------------- return scene
Well Done!
As Jay pointed out, your exitScene is in enterScene.
Change to this. ( basically move one end up)
function scene:enterScene( event ) print( "1: enterScene event" ) -- remove previous scene's view storyboard.purgeScene( "start" ) end -- moved here -- called immediately after scene has moved onscreen function scene:exitScene( event ) print( "1: exitScene event" ) end
burhan
Done Burhan.
Thanks again guys.
I’m going to try to add touch listeners to the level selection images, I have all the information in here, so I’ll try to see if I get it working.
Doing it yourself is always good, because you’re learning techniques that can be used over and over again. But if you want another option, I sell a library called OGT Level Manager. You can find out about it here: http://masteringcoronasdk.com/ogt-level-manager/
Jay
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.
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
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