Object in createScene, not accessible in enterScene

Hi Guys,

I tried to switch from Director Class to Storyboard. If I understood it right, the display objects go in the createScene function / event and manipulations on those objects go in the enterScene function/event.
I used a forward declarations on the bar object to make it accessible from other event listeners but the but I get an error loading the game. The bar cannot be indexed.

Any help on how I can let the comeDown () function manipulate the bar object ?

I pasted the code below:

[lua]local storyboard = require “storyboard”
local scene = storyboard.newScene()
_W = display.contentWidth
_H = display.contentHeight

local physics = require(“physics”)
physics.start( true )
local function onSceneTouch(event )
if event.phase == “ended” then

storyboard.gotoScene( “menu” )

return true
end
end

local bar

function scene:createScene( event )
local group = self.view

scoreText = display.newText( " " … score,0, 15, Helvetica, 50)
group:insert(scoreText)

local back = display.newImageRect(“images/back.png”, _W/10,75)
back:setReferencePoint(display.CenterReferencePoint)
back.x = _W-50
back.y = _H-100
back:addEventListener(“touch”, onSceneTouch)
group:insert(back)

local bar = display.newImageRect(“images/bar.png”,10,100)
bar:setReferencePoint( display.CenterReferencePoint)
bar.x = _W-30
bar.y = 149
bar.myName = “bar”
physics.addBody(bar, “static”, {density = 0, bounce =0, friction =0})
bar:setLinearVelocity(0,velocity *-1)
group:insert(bar)

local basket = display.newImage(“images/basket.png”)
basket:setReferencePoint(display.BottomLeftReferencePoint)
basket.x = 100; basket.y = 200
basket.myName=“basket”
physics.addBody(basket, “kinematic”, {density = 0, bounce =0, friction =0})
basket.isSensor = “true”
group:insert(basket)
local board = display.newImage(“images/board.png”)
board:setReferencePoint(display.BottomRightReferencePoint)
board.x = basket.x; board.y = basket.y
physics.addBody(board, “static”, {density = 0, bounce =0, friction =0})
group:insert(board)

local ring = display.newRect(0, 0, 8, 80)
ring:setReferencePoint(display.CenterReferencePoint)
ring.x = basket.x + 85
ring.y = basket.y+23
physics.addBody(ring, “kinematic”, {density = 0, bounce =0, friction =0})
group:insert(ring)

end

function scene:enterScene( event )
local group = self.view

local function comeDown()
if bar.y < 150 then
bar:setLinearVelocity(0,velocity *1)
end
if bar.y > 500 then
bar:setLinearVelocity(0,velocity *-1)
end

end
Runtime:addEventListener(“enterFrame”,comeDown)[/lua]
Thanks !

P.S. the storyboard forum wasn´t listed as an option so I put it in this forum, sorry.
[import]uid: 126207 topic_id: 29764 reply_id: 329764[/import]

The problem is you are redefining the bar variable as local in your createScene event. Change the following line of code from
[lua] local bar = display.newImageRect(“images/bar.png”,10,100)[/lua]

to

[lua] bar = display.newImageRect(“images/bar.png”,10,100)[/lua] [import]uid: 147305 topic_id: 29764 reply_id: 119421[/import]