I have a game app I am working on that seems to be doubling up the scores after you play it one time. I.E., every level that comes after the first level the scores for collecting a points object increases twice what it did during the first level. So an item that is equal to 100 points is equal to 200 points on the second level, 300 points the third level, etc. However, the points value should not change. I tried resetting all the variables and it is still doing this. Anyone have any ideas what to look at? [import]uid: 30898 topic_id: 6196 reply_id: 306196[/import]
have you created multiple versions of a listener? [import]uid: 6645 topic_id: 6196 reply_id: 21268[/import]
I think the listener gets created again when the level starts over. I can’t figure out how to turn the listener off at the end of the level. I have been suspecting that is the problem but am stumped as to how to rectify it. [import]uid: 30898 topic_id: 6196 reply_id: 21272[/import]
post your code [import]uid: 12108 topic_id: 6196 reply_id: 21290[/import]
Thanks for the help by the way, I spent all weekend moving things around to no avail…
[blockcode]
local physics = require(“physics”)
physics.start()
local ui = require( “ui” )
local movieclip = require( “movieclip” )
require “sprite”
local function playGame( self, event )
local background = display.newImage( “carpet.png” )
local game=display.newGroup()
local points=display.newGroup()
local sheet1 = sprite.newSpriteSheet( “tieguy.png”, 40, 117 )
local spriteSet1 = sprite.newSpriteSet(sheet1, 1, 4)
sprite.add( spriteSet1, “tieguy”, 1, 4, 200, 0 )
local instance1 = sprite.newSprite( spriteSet1 )
instance1.x = display.contentWidth / 2
instance1.y = display.contentHeight - 64
physics.addBody( instance1, “kinematic”, { friction=0.7 } )
instance1.isTieGuy = true
instance1:prepare(“tieguy”)
local scoreBoard = display.newRect( points, 0, 0, 320, 70 )
scoreBoard:setFillColor( 0, 0, 0 )
local logo = display.newImage( “topbar_logo.png”, 23,0 )
local scoreDisplay = ui.newLabel{
bounds = { display.contentWidth - 120, 10 + display.screenOriginY, 100, 24 }, – align label with right side of current screen
text = “0”,
font = “Arial-Bold”,
textColor = { 255, 0, 0, 255 },
size = 24,
align = “right”
}
score = 0
scoreDisplay:setText( score )
life = 100
dropTally = 0
local lifeStats = display.newRect( points, 190, 45, life, 10 )
lifeStats:setFillColor( 0, 255, 0 )
lifeStats:setStrokeColor ( 140, 140, 140 )
lifeStats.strokeWidth=2
local lifeDisplay = ui.newLabel{
bounds = { 90, 43, 100, 24 },
text = “ENERGY REMAINING:”,
font = “Arial-Bold”,
textColor = { 0, 255, 0, 255 },
size = 12,
align = “right”
}
energy = “ENERGY REMAINING:”
lifeDisplay:setText( energy )
local function onBadCollision( self, event )
if ( event.phase == “began” ) then
score = score - 25
scoreDisplay:setText( score )
self:play{ startFrame=2, endFrame=2, loop=7, remove=true }
life = life - 2
blkot = 100 - life
local blackout = display.newRect( points, (290 - blkot), 45, blkot, 10)
blackout:setFillColor( 0, 0 ,0 )
end
end
local function onGoodCollision( self, event )
if ( event.phase == “began” ) then
score = score + 100
scoreDisplay:setText( score )
self:play{ startFrame=2, endFrame=2, loop=7, remove=true }
end
end
local function onFlameCollision( self, event )
if ( event.phase == “began” ) then
life = life - 5
blkot = 100 - life
local blackout = display.newRect( points, (290 - blkot), 45, blkot, 10)
blackout:setFillColor( 0, 0 ,0 )
end
end
local function startDrag( event )
local t = event.target
local phase = event.phase
if “began” == phase then
display.getCurrentStage():setFocus( t )
t.isFocus = true
– Store initial position
t.x0 = event.x - t.x
– Make body type temporarily “kinematic” (to avoid gravitional forces)
event.target.bodyType = “kinematic”
– Stop current motion, if any
event.target:setLinearVelocity( 0, 0 )
event.target.angularVelocity = 0
elseif t.isFocus then
if “moved” == phase then
t.x = event.x - t.x0
instance1: play()
elseif “ended” == phase or “cancelled” == phase then
display.getCurrentStage():setFocus( nil )
t.isFocus = false
instance1: pause()
if ( not event.target.isTieGuy ) then
event.target.bodyType = “dynamic”
end
end
end
return true
end
function newDrop()
rand = math.random( 100 )
if (rand < 10) then
j = movieclip.newAnim{ “object.png”, “minusPoints25.png” }
game:insert (j);
j.x = 60 + math.random( 160 )
j.y = -100
physics.addBody( j, { density=1, friction=0.3, bounce=0.1} )
j.collision = onBadCollision
j:addEventListener( “collision”, j )
elseif (rand < 25) then
j = movieclip.newAnim{ “objectB.png”, “minusPoints25.png” }
game:insert (j);
j.x = 60 + math.random( 160 )
j.y = -100
physics.addBody( j, { density=1, friction=0.3, bounce=0.1} )
j.collision = onBadCollision
j:addEventListener( “collision”, j )
elseif (rand < 35) then
j = movieclip.newAnim{ “objectC.png”, “minusPoints25.png” }
game:insert (j);
j.x = 60 + math.random( 160 )
j.y = -100
physics.addBody( j, { density=1, friction=0.3, bounce=0.1} )
j.collision = onBadCollision
j:addEventListener( “collision”, j )
elseif (rand < 55) then
j = movieclip.newAnim{ “objectD.png”, “plusPoints100.png” }
game:insert (j);
j.x = 60 + math.random( 160 )
j.y = -100
physics.addBody( j, { density=1, friction=0.3, bounce=0.1} )
j.collision = onGoodCollision
j:addEventListener( “collision”, j )
elseif (rand < 70) then
j = movieclip.newAnim{ “objectE.png”, “plusPoints100.png” }
game:insert (j);
j.x = 60 + math.random( 160 )
j.y = -100
physics.addBody( j, { density=1, friction=0.3, bounce=0.1} )
j.collision = onGoodCollision
j:addEventListener( “collision”, j )
elseif (rand < 80) then
j = movieclip.newAnim{ “objectF.png”, “minusPoints25.png” }
game:insert (j);
j.x = 60 + math.random( 160 )
j.y = -100
physics.addBody( j, { density=1, friction=0.3, bounce=0.1} )
j.collision = onBadCollision
j:addEventListener( “collision”, j )
elseif (rand < 90) then
j = movieclip.newAnim{ “objectG.png”, “minusPoints25.png” }
game:insert (j);
j.x = 60 + math.random( 160 )
j.y = -100
physics.addBody( j, { density=1, friction=0.3, bounce=0.1} )
j.collision = onBadCollision
j:addEventListener( “collision”, j )
else
j = movieclip.newAnim{ “objectH.png”, “minusPoints25.png” }
game:insert (j);
j.x = 60 + math.random( 160 )
j.y = -100
physics.addBody( j, { density=1, friction=0.3, bounce=0.1} )
j.collision = onBadCollision
j:addEventListener( “collision”, j )
end
dropTally = dropTally + 1
if (dropTally > 99) then
local yourDead = timer.performWithDelay( 1500, endGame )
end
end
function newFlame()
rand = math.random( 100 )
if (rand < 30) then
f = movieclip.newAnim{ “rightFlame06.png”, “rightFlame05.png”, “rightFlame04.png”, “rightFlame03.png”, “rightFlame02.png”, “rightFlame01.png”, “rightFlame02.png”, “rightFlame03.png”, “rightFlame04.png”, “rightFlame05.png”, “rightFlame06.png”, “rightFlame07.png”, }
game:insert (f);
f:play{ startFrame=1, endFrame=12, loop=1, remove=true }
f.x = 280
f.y = 420
physics.addBody( f, {isSensor = true} )
f.collision = onFlameCollision
f:addEventListener( “collision”, f )
elseif (rand > 70) then
f = movieclip.newAnim{ “leftFlame06.png”, “leftFlame05.png”, “leftFlame04.png”, “leftFlame03.png”, “leftFlame02.png”, “leftFlame01.png”, “leftFlame02.png”, “leftFlame03.png”, “leftFlame04.png”, “leftFlame05.png”, “leftFlame06.png”, “leftFlame07.png”, }
game:insert (f);
f:play{ startFrame=1, endFrame=12, loop=1, remove=true }
f.x = 20
f.y = 420
physics.addBody( f, {isSensor = true} )
f.collision = onFlameCollision
f:addEventListener( “collision”, f )
end
end
function endGame()
local background = display.newImage( “endGame.png” )
local scoreDisplay = ui.newLabel{
bounds = { 10, 100, 200, 24 },
text = “0”,
font = “Arial-Bold”,
textColor = { 255, 0, 0, 255 },
size = 24,
align = “right”
}
scoreDisplay:setText( score )
local lifeDisplay = ui.newLabel{
bounds = { 10, 140, 200, 24 },
text = “0”,
font = “Arial-Bold”,
textColor = { 255, 0, 0, 255 },
size = 18,
align = “right”
}
lifeDisplay:setText( life )
local roundedRect = display.newRoundedRect( 60, 432, 200, 40, 8 )
roundedRect:setFillColor( 255, 0, 0, 225 )
local t = ui.newLabel{
bounds = { 10, 442, 300, 40 },
text = “PLAY AGAIN”,
font = “Arial-Bold”,
textColor = { 0, 0, 0, 255 },
size = 18,
align = “center”
}
t:addEventListener( “touch”, playGame )
end
local gameLogic = {}
function gameLogic:timer( event )
if (dropTally < 99) then
local popFlame = timer.performWithDelay( 10, newFlame )
local dropObjects = timer.performWithDelay( 10, newDrop )
dropTally = dropTally +1
local gameFunction = timer.performWithDelay( 1500, gameLogic )
else
timer.cancel( event.source )
local yourDead = timer.performWithDelay( 500, endGame )
end
if (life < 4) then
life = 0
timer.cancel( event.source )
local yourDead = timer.performWithDelay( 500, endGame )
end
end
instance1:addEventListener( “touch”, startDrag )
local gameFunction = timer.performWithDelay( 1500, gameLogic )
end
local background = display.newImage( “how2.png” )
local roundedRect = display.newRoundedRect( 60, 432, 200, 40, 8 )
roundedRect:setFillColor( 255, 0, 0, 225 )
local t = ui.newLabel{
bounds = { 10, 442, 300, 40 },
text = “TOUCH TO PLAY”,
font = “Arial-Bold”,
textColor = { 0, 0, 0, 255 },
size = 18,
align = “center”
}
t:addEventListener( “touch”, playGame )
[/blockcode] [import]uid: 30898 topic_id: 6196 reply_id: 21298[/import]
i think maybe your game ends when objects are still active. and then you keep adding more objects without removing existing ones. are you sure you’ve removed everything on game end? eg maybe there’s a timer running from the end of the first game that carries through to the next one and spawns an object that shouldnt be getting spawned [import]uid: 6645 topic_id: 6196 reply_id: 21316[/import]
I know that all the objects that collide get removed but those that do not have a collision I do not remove so that may be the problem. Also, I am having a hard time with timers. What is the best method for ending a timer or making sure all the timers are done? [import]uid: 30898 topic_id: 6196 reply_id: 21324[/import]