What's wrong with the code?

Do you know what’s wrong with this code?

[code]require( “ice” )

local scores = ice:loadBox( “scores” )

local onballTap = function( event )
scores:increment( “ball”, 1 )
scores:save()
end

local onEnterFrame = function( event )
ball.score.text = scores:retrieve( “ball” ) or 0
end

Runtime:addEventListener( “enterFrame”, onEnterFrame )

ball = display.newImageRect( “SkySoccer Ball.gif”, 100, 100 )
ball.x = display.contentCenterX
ball.y = 429
ball.myName = “ball”

ball.score = display.newText( “0”, 0, 0, native.systemFont, 20 )
ball.score.x = 305
ball.score.y = 15

ball:addEventListener( “tap”, onballTap )
screenGroup:insert( ball )[/code] [import]uid: 203192 topic_id: 34553 reply_id: 334553[/import]

Simply that you need to declare ball ahead of the functions. Right now onEnterFrame can’t see “ball” because “ball” is declared below.

When you have problems like this but don’t want to rewrite code, just declare ball ahead of time.

[code] require(“ice”)
local ball – it’s nil, but it’s now declared

– functions here

ball = display.newImageRect( “SkySoccer Ball.gif”, 100, 100)
– etc.[/code]

Also not sure if you can use image names with a space in them but I’ve never tried. [import]uid: 41884 topic_id: 34553 reply_id: 137375[/import]

Thank you, that worked for me [import]uid: 203192 topic_id: 34553 reply_id: 137376[/import]

One more question:

If I go to the next scene, the score stays displayed at x=305 y=15.
How can I remove that if I go to the next scene? [import]uid: 203192 topic_id: 34553 reply_id: 137379[/import]

An object is only part of a display group if either:

  1. You :insert() it
  2. You specify during creation (ie: local ball = display.newImage(group, "ball.png") )

In this case you didn’t add the score to screenGroup so it will remain onscreen. You should either add it to screenGroup or :remove() on exitScene [import]uid: 41884 topic_id: 34553 reply_id: 137381[/import]

Hi again,
At first glance, it looks like you’re adding “ball” to the scene’s view, but then you create “ball.score” (a new text object on line 21), but that won’t be inserted into the scene’s view just because it’s a child of “ball”. This is likely why it remains on the screen when you change scenes.

EDIT: you beat me to the punch Richard. Thanks for answering! :slight_smile:

Best regards,
Brent [import]uid: 200026 topic_id: 34553 reply_id: 137384[/import]

thanx richard and brent [import]uid: 203192 topic_id: 34553 reply_id: 137393[/import]

Simply that you need to declare ball ahead of the functions. Right now onEnterFrame can’t see “ball” because “ball” is declared below.

When you have problems like this but don’t want to rewrite code, just declare ball ahead of time.

[code] require(“ice”)
local ball – it’s nil, but it’s now declared

– functions here

ball = display.newImageRect( “SkySoccer Ball.gif”, 100, 100)
– etc.[/code]

Also not sure if you can use image names with a space in them but I’ve never tried. [import]uid: 41884 topic_id: 34553 reply_id: 137375[/import]

Thank you, that worked for me [import]uid: 203192 topic_id: 34553 reply_id: 137376[/import]

One more question:

If I go to the next scene, the score stays displayed at x=305 y=15.
How can I remove that if I go to the next scene? [import]uid: 203192 topic_id: 34553 reply_id: 137379[/import]

An object is only part of a display group if either:

  1. You :insert() it
  2. You specify during creation (ie: local ball = display.newImage(group, "ball.png") )

In this case you didn’t add the score to screenGroup so it will remain onscreen. You should either add it to screenGroup or :remove() on exitScene [import]uid: 41884 topic_id: 34553 reply_id: 137381[/import]

Hi again,
At first glance, it looks like you’re adding “ball” to the scene’s view, but then you create “ball.score” (a new text object on line 21), but that won’t be inserted into the scene’s view just because it’s a child of “ball”. This is likely why it remains on the screen when you change scenes.

EDIT: you beat me to the punch Richard. Thanks for answering! :slight_smile:

Best regards,
Brent [import]uid: 200026 topic_id: 34553 reply_id: 137384[/import]

thanx richard and brent [import]uid: 203192 topic_id: 34553 reply_id: 137393[/import]