Problem with an update

[lua]---------------------------------------------------------------------------------

– scene9.lua


local storyboard = require( “storyboard” )
local scene = storyboard.newScene()


– BEGINNING OF YOUR IMPLEMENTATION

local background

local function myimage2( self, event )
if event.phase == “began” then

x=x-1
textObject.text = x
return true
end
end

– Called when the scene’s view does not exist:
function scene:createScene( event )
local screenGroup = self.view

background = display.newImage( “background.png” )
screenGroup:insert( background )

myimage = display.newImage(“myimage.png”)
myimage.x = display.contentWidth / 5
myimage.y = display.contentHeight / 2
screenGroup:insert( myimage )

myimage.touch = myimage2
level1_apple:addEventListener( “touch”, apple )

print( “\n9: createScene event”)

x=5
local textObject = display.newText( x, 4, -1, “GoodDog Plain”, 20 )
textObject:setTextColor( 255,255,255 )
screenGroup:insert( textObject )

end
– Called immediately after scene has moved onscreen:
function scene:enterScene( event )
storyboard.printMemUsage()
print( “9: enterScene event” )

end
– Called when scene is about to move offscreen:
function scene:exitScene( event )

print( “9: exitScene event” )

end
– Called prior to the removal of scene’s “view” (display group)
function scene:destroyScene( event )

print( “((destroying scene 9’s 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
[/lua]

How can I update the x to 4, 3 ,2 ,1 when the image is clicked?
Currently, it remains 5 whatever I try to do…

Thanks for the help! [import]uid: 208318 topic_id: 37090 reply_id: 67090[/import]

I’m really having a hard time figuring out this…

I’m getting the following error message in the console:

" attempt to index upvalue ‘textObject’ " [import]uid: 208318 topic_id: 37090 reply_id: 145491[/import]

The problem is that your variable textObject is local to the scene:createScene function. So when you attempt to update the text value for it in your myImage2 function, lua doesn’t know what you are referring to when you reference textObject. Here’s a quick fix:

Up in the beginning of your implementation (around where you declare the local variable background ), type in the following line:

local textObject

Then simply remove the word local before you create textObject at line 45. That should fix things.

A helpful habit to get into when working with Storyboard scenes is to declare all of your local variables up near the top of your lua file, so that you can work with those variables in any block of code. As you can see, if you create all your display objects as local to the createScene function, you’ll have a hard time working with those objects in functions that exist outside of the createScene function. I personally declare all my variables in category blocks, sort of like this:

----------  
-- VARIABLE DECLARATIONS:  
----------  
  
-- display groups:  
local group, someOtherDisplayGroup, yetAnotherDisplayGroup  
  
-- display objects:  
local object1, object2, object3  
  
-- functions:  
local function1, function2, function3  
  
-- timers:  
local timer1, timer2, timer3  
  
-- transitions:  
local trans1, trans2, trans3  
  
-- other:  
local var1, var2, var3  

Obviously those variable names are just placeholders, but I find this an easy way to keep track of my variables, and since I’m declaring all my locals at the very top of my lua file, I no longer have to worry about typing “local” before I define those variables further down in my code - and I know that I’ll be able to refer to those variables from any block of code within my lua file. I hope that makes sense, and is helpful.

Best,
Jason
[import]uid: 27636 topic_id: 37090 reply_id: 145496[/import]

@jasonschroeder
Thank you so much for making this clear to me! I finally managed to understand some basics about the storyboard API. Now the app works perfectly.

Again, thank you so much! [import]uid: 208318 topic_id: 37090 reply_id: 145501[/import]

I’m really having a hard time figuring out this…

I’m getting the following error message in the console:

" attempt to index upvalue ‘textObject’ " [import]uid: 208318 topic_id: 37090 reply_id: 145491[/import]

The problem is that your variable textObject is local to the scene:createScene function. So when you attempt to update the text value for it in your myImage2 function, lua doesn’t know what you are referring to when you reference textObject. Here’s a quick fix:

Up in the beginning of your implementation (around where you declare the local variable background ), type in the following line:

local textObject

Then simply remove the word local before you create textObject at line 45. That should fix things.

A helpful habit to get into when working with Storyboard scenes is to declare all of your local variables up near the top of your lua file, so that you can work with those variables in any block of code. As you can see, if you create all your display objects as local to the createScene function, you’ll have a hard time working with those objects in functions that exist outside of the createScene function. I personally declare all my variables in category blocks, sort of like this:

----------  
-- VARIABLE DECLARATIONS:  
----------  
  
-- display groups:  
local group, someOtherDisplayGroup, yetAnotherDisplayGroup  
  
-- display objects:  
local object1, object2, object3  
  
-- functions:  
local function1, function2, function3  
  
-- timers:  
local timer1, timer2, timer3  
  
-- transitions:  
local trans1, trans2, trans3  
  
-- other:  
local var1, var2, var3  

Obviously those variable names are just placeholders, but I find this an easy way to keep track of my variables, and since I’m declaring all my locals at the very top of my lua file, I no longer have to worry about typing “local” before I define those variables further down in my code - and I know that I’ll be able to refer to those variables from any block of code within my lua file. I hope that makes sense, and is helpful.

Best,
Jason
[import]uid: 27636 topic_id: 37090 reply_id: 145496[/import]

@jasonschroeder
Thank you so much for making this clear to me! I finally managed to understand some basics about the storyboard API. Now the app works perfectly.

Again, thank you so much! [import]uid: 208318 topic_id: 37090 reply_id: 145501[/import]