Storyboard: How do I remove a display object after the scene is created?

Hi all,

I am having trouble getting an object to appear on another part of the screen in response to a touch event using the Storyboard API.

This works perfectly in Director by simply setting the alpha of these objects to 0, using a flag and embedding trasition.to(object{time = 500, alpha = 1}) within the touch event to fire if the flag is up.

I have tried all kinds of things in Storyboard, but can’t seem to figure out the exact placement to get this wired up properly since it wants all the functions in one place, event listeners in another, etc.

Am very new to programming and Corona, and my thinking in switching from Director is that I should start off using the Ansca-supported APIs, right?

Any insight into this would be most appreciated!

Thanks much,

Chris [import]uid: 97836 topic_id: 25317 reply_id: 325317[/import]

Without seeing any code, I’m going to guess this is a scope problem.

Lets say you’ve done something like:

function scene:createScene()  
 local group = self.view()  
 local button = display.newImageRect("filenmae.png", 64, 64)  
 group:insert(button)  
...  
end  
function scene:enterScene()  
 button.x = 100  
 button.y = 100  
end  

Because button was declared “local” inside of createScene you don’t have access to it in enterScene.

I tend to do this:

local button  
  
function scene:createScene()  
 local group = self.view()  
 button = display.newImageRect("filenmae.png", 64, 64)  
 group:insert(button)  
...  
end  
function scene:enterScene()  
 button.x = 100  
 button.y = 100  
end  

for any thing I need to be visible to each of the functions.
But like I said, I’m guessing since I haven’t seen your code.
[import]uid: 19626 topic_id: 25317 reply_id: 102270[/import]

Thanks robmiracle,

I got this issue fixed (still not sure exactly how, but it was scope related). Now another question:

I want to have a menu button on each page, you know, so users can return to the main menu. I have a file, aptly named menu.lua. No problem loading menu the first time (from what I call start.lua, basically just calls menu.lua). Here’s the problem - once I have made a selection from the menu and am in the next screen(in this case a file named blendsLevel.lua), when I want to go back to the menu, the touch event generates the following error.

local function onSceneTouch(self, event)  
 if event.phase == "began" then  
 storyboard.gotoScene("menu", "fade", 400)  
 return true  
 end  
end  

the error I get is this (lua.26 in the error message equates to line 2 in the example)

Runtime error ...j\_7\_1xry7m0000gn/T/TemporaryItems/83/blendsLevel.lua:26: attempt to index local 'event' (a nil value) stack traceback: [C]: ? ...j\_7\_1xry7m0000gn/T/TemporaryItems/83/blendsLevel.lua:26: in function <...j_7_1xry7m0000gn><br> ?: in function <?:218><br><br>Runtime error<br> ...j_7_1xry7m0000gn/T/TemporaryItems/83/blendsLevel.lua:26: attempt to index local 'event' (a nil value)<br>stack traceback:<br> [C]: ?<br> ...j_7_1xry7m0000gn/T/TemporaryItems/83/blendsLevel.lua:26: in function <...j_7_1xry7m0000gn><br> ?: in function <?:218><br>

Just wondering if anything jumps out at you that I’m doing wrong to be able to call a scene a second time…

Many thanks!
chris [import]uid: 97836 topic_id: 25317 reply_id: 102465[/import] </…j_7_1xry7m0000gn></…j_7_1xry7m0000gn>

I’d like to see the code where you do the “addEventListener” to the button.

[import]uid: 19626 topic_id: 25317 reply_id: 102496[/import]

Hi Rob,

I put it in the scene:enterScene with all the other event listeners. Removal is under the scene:exitScene

function scene:enterScene(event)  
 local sceneGroup = self.view  
 ---------ADD EVENT LISTENERS--------  
 home:addEventListener ("touch", onSceneTouch)  
 --getWords:addEventListener ("touch", onSceneTouch)  
 b[1]:addEventListener ("touch", selectPosition)  
 b[2]:addEventListener ("touch", selectPosition)  
 b[3]:addEventListener ("touch", selectPosition)  
 b[4]:addEventListener ("touch", selectPosition)  
 b[5]:addEventListener ("touch", selectPosition)  
 b[6]:addEventListener ("touch", selectPosition)  
 b[7]:addEventListener ("touch", selectPosition)  
 medium:addEventListener ("touch", selectLevel)  
 difficult:addEventListener ("touch", selectLevel)  
 easy:addEventListener ("touch", selectLevel)  
 mixed:addEventListener ("touch", selectLevel)  
  
  
 garbagePrinting()  
end  

I’m wondering perhaps I should be using something other than storyboard.gotoScene to call a scene once it’s already been displayed? Like some kind of reloadScene command? Just grabbing at straws though, really, as my understanding is that each scene is destroyed when you leave it and simply recreated when you call it again.

Many thanks for taking the time to help me work through this!

Chris [import]uid: 97836 topic_id: 25317 reply_id: 102530[/import]

This is easy. May be a bit hard to grasp, but its pretty easy once you understand it.

When you setup an event listener like this:

  
 object:addEventListener("touch", someFunction)  
  

you are setting up the listener in “Function Mode”

You can also set up event listeners as “Table Listeners”

  
 object.touch = someFunction  
 object:AddEventListener("touch", object)  

Notice in the first one you are passing a function to call. In the second one you pass a reference to the object/table that has the function assigned to it.

In the first form, “someFunction” looks like this:

  
local function someFunction(event)  
...  
end  

In the second form, someFunction looks like this:

  
local function someFunction(self, event)  
...  
end  
  

You are using the second form of the function but using the first form of the AddEventListener.

So in effect, when your selectPosition function is called now, the variable “self” is holding the event table that’s passed in and “event” parameter is empty.

You need to standardize on one way or the other of calling event handlers. Personally I rarely need “self” in touch handlers because event.target == self anyway, so its easier to code up the function version of event handlers.

[import]uid: 19626 topic_id: 25317 reply_id: 102538[/import]

Interesting. Of course it works when I simply take out the word “self”. What’s curious to me is that I was copying this from the sample code and actually have home.touch = onSceneTouch when I created the button. I certainly appreciate the explanation, and will do some reading on function vs. table listeners.

Would be curious to hear your thoughts on why the sample code works (https://github.com/ansca/Storyboard-Sample/blob/master/scene1.lua), although it’s probably some small difference that I’m just not seeing :slight_smile:

In any case, many, many thanks!

Chris [import]uid: 97836 topic_id: 25317 reply_id: 102576[/import]

In the sample code they are using a table listener:

line 34: image.touch = onSceneTouch
line 68: image:addEventListener( “touch”, image )

Since Jonathan is passing the object as the 2nd parameter to the addEventListener call, onSceneTouch needs both “self” and “event”.

[import]uid: 19626 topic_id: 25317 reply_id: 102577[/import]