What's Wrong With My Game?

I’ve been working on this game for quite some time, but at this point I am thinking about giving up and starting a new game with cleaner code and better organization. In my game, the player uses their finger to slash at the falling soccer balls, kicking them away.

On the simulator, the game works fine for about 15 seconds, but then I get the same problem as I do on my iPod Touch. The soccer balls start to fall very choppy and the swiping doesn’t work. ON the device, when I go to swipe, the game pauses until I release, which doesn’t happen in the simulator.

Does anybody have any ideas?
Project – http://www.mediafire.com/?pnhp09s0200b8

Device Demo Video: http://vimeo.com/26720272 [import]uid: 7116 topic_id: 12723 reply_id: 312723[/import]

Without looking at the code, my first thought is whether the soccer balls are being removed from memory after they’ve been kicked.

Jon Beebe has a great tutorial about memory management, have you read this?
http://jonbeebe.net/corona-sdk-memory-management-101-tag-memory-m [import]uid: 14218 topic_id: 12723 reply_id: 46647[/import]

Looks like MediaFire only allows you to download 1 file at a time unless you upgrade to Pro? [import]uid: 31262 topic_id: 12723 reply_id: 46710[/import]

Here’s the entire code:

[code]

-------------------------------Openfeint----------------------------------

local openfeint = require “openfeint”

local of_product_key = “COza6MZtxdo5rUHeeKBzQ”
local of_product_secret = “WFegg74pguDaJDlxmgFlhbpZmsEU3MtI2RX4GKfNLpA”
local of_app_id = “326703”
local display_name = “Soccer Samurai”

openfeint.init (“COza6MZtxdo5rUHeeKBzQ”, “WFegg74pguDaJDlxmgFlhbpZmsEU3MtI2RX4GKfNLpA”, “Soccer Samurai”, “326703”)
openfeint.launchDashboard ()
–> Sets OpenFeint up


display.setStatusBar(display.HiddenStatusBar)

local ui = require(“ui”)
– Load and start physics
local physics = require(“physics”)
physics.start()
physics.setGravity(0, 15)

local textureCache = {}
textureCache[1] = display.newImage(“regBall.png”); textureCache[1].isVisible = false;
textureCache[2] = display.newImage(“star.png”); textureCache[2].isVisible = false;
textureCache[3] = display.newImage(“shakeImage.png”); textureCache[3].isVisible = false;
local gameIsActive = false

local halfballWidth = textureCache[1].contentWidth * .5
local halfStarWidth = textureCache[2].contentWidth * .5
local scoreText
local score = 0
local starCount = 0
local starCountText
local lives = 5
local timeInterval = 1000
local timeStarInterval = 5000
local timeLastball = 0
local timeLastStar = 0

local maxPoints = 5
local lineThickness = 20
local lineFadeTime = 300
local endPoints = {}

local shake = {}
local shakeImage

local ball
local star

local hugeBallsBar
local tenPointsBar
local scoreDoubleBar
local scoreTripleBar
local extraLifeBar

local doubleScoreMode = false
local triplescoreMode = false
local hugeBallsMode = false

local loseMenu
local mainMenuButton
local highScoresButton
local retryButton

local playButton
–local hsButton
–local achievementsButton
local ofButton

lbID = “836546”
numScore = score

local mainMenuBG = display.newImage(“mainMenuBG.png”)
mainMenuBG.isVisible = false
–create the goal, paddle, monster, and background
local background = display.newImage(“grassBackground.png”)
local statBar = display.newImage(“statBar.png”)

local goalBounds = display.newRect(0, 470, 320, 10)
physics.addBody(goalBounds, “static”, {bounce = 0})
goalBounds.name = “goalBounds”

–make the text to display the score
scoreText = display.newText(score, 0, 0, “HelveticaNeue”, 20)
scoreText:setTextColor(255, 255, 255)
scoreText.x = 25
scoreText.y = 15

scoreLabel = display.newText(“ptz”, 0, 0, “HelveticaNeue”, 10)
scoreLabel.x = scoreText.x + 30
scoreLabel.y = scoreText.y

–make the text to display the number of stars collected
livesText = display.newText(lives, 0, 0, “HelveticaNeue”, 20)
livesText:setTextColor(255, 255, 255)
livesText.x = 250
livesText.y = 15

–make the text to display the number of stars collected
starCountText = display.newText(starCount, 0, 0, “HelveticaNeue”, 20)
starCountText:setTextColor(255, 255, 255)
starCountText.x = 250
starCountText.y = 15
starCountText.isVisible = false

starIndic1 = display.newImage(“starIndic.png”)
starIndic1.x = 100
starIndic1.y = 15
starIndic1.alpha = .2
starIndic2 = display.newImage(“starIndic.png”)
starIndic2.x = 125
starIndic2.y = 15
starIndic2.alpha = .2
starIndic3 = display.newImage(“starIndic.png”)
starIndic3.x = 150
starIndic3.y = 15
starIndic3.alpha = .2

function shakePhonePlease()
shakeImage = display.newImage(“shakeImage.png”)
shakeImage.y = 40
shakeImage.isVisible = true
shakeCount = 0

function shake:accelerometer(e)
if (shakeCount == 0) then
if(e.isShake == true) then
shakeImage.isVisible = false
local randomPowerUp = {1,2,3,4,5}
local powerUpNumber = randomPowerUp[math.random(1,5)]

if (powerUpNumber == 1) then
scoreDoubleBar = display.newImage(“scoreDoubleBar.png”)
scoreDoubleBar.y = 40
transition.from(scoreDoubleBar, {alpha = 0})
doubleScoreMode = true
function removeDoubleMultiplier()
scoreDoubleBar:removeSelf()
doubleScoreMode = false
end
timer.performWithDelay( 10000, removeDoubleMultiplier, 1 )
end

if (powerUpNumber == 2) then
scoreTripleBar = display.newImage(“scoreTripleBar.png”)
scoreTripleBar.y = 40
transition.from(scoreTripleBar, {alpha = 0})
tripleScoreMode = true
function removeTripleMultiplier()
scoreTripleBar:removeSelf()
tripleScoreMode = false
end
timer.performWithDelay( 10000, removeTripleMultiplier, 1 )
end

if (powerUpNumber == 3) then
extraLifeBar = display.newImage(“extraLifeBar.png”)
extraLifeBar.y = 40
transition.from(extraLifeBar, {alpha = 0})
lives = lives + 1
end

if (powerUpNumber == 4) then
tenPointsBar = display.newImage(“tenPointsBar.png”)
tenPointsBar.y = 40
transition.from(tenPointsBar, {alpha = 0})
score = score + 10
end

if (powerUpNumber == 5) then
hugeBallsBar = display.newImage(“hugeBallsBar.png”)
hugeBallsBar.y = 40
transition.from(hugeBallsBar, {alpha = 0})
hugeBallsMode = true
function removeHugeBalls()
hugeBallsBar:removeSelf()
hugeBallsMode = false
end
timer.performWithDelay( 10000, removeHugeBalls, 1 )
end

shakeCount = 1
end
end
end
Runtime:addEventListener(“accelerometer”, shake)
end

local function newLoseMenu()

local loseMenu = display.newImage(“loseMenu.png”)
loseMenu.x = display.contentWidth/2
loseMenu.y = 240
Runtime:removeEventListener(“accelerometer”, shake)
if shakeImage then
shakeImage.isVisible = false
end

if (powerUpNumber == 1) then
scoreDoubleBar:removeSelf()
end

if (powerUpNumber == 2) then
scoreTripleBar:removeSelf()
end

if (powerUpNumber == 3) then
extraLifeBar:removeSelf()
end

if (powerUpNumber == 4) then
tenPointsBar:removeSelf()
end

if (powerUpNumber == 5) then
hugeBallsBar:removeSelf()
end

local function backToMainMenu()
mainMenuBG.isVisible = true
playButton.isVisible = true
ofButton.isVisible = true
loseMenu:removeSelf()
mainMenuButton:removeSelf()
highScoresButton:removeSelf()
retryButton:removeSelf()
finalScoreText:removeSelf()
score = 0
starCount = 0
lives = 5
if ball then
ball:removeSelf()
end
if star then
star:removeSelf()
end
end

mainMenuButton = ui.newButton{
default = “loseMenu-mainMenu.png”,
over = “loseMenu-mainMenu.png”,
–onPress = button1Press,
onRelease = backToMainMenu,
}
mainMenuButton.x = display.contentWidth/2- 90
mainMenuButton.y = 360

highScoresButton = ui.newButton{
default = “loseMenu-highScores.png”,
over = “loseMenu-highScores.png”,
–onPress = button1Press,
–onRelease = button1Release,
}
highScoresButton.x = display.contentWidth/2
highScoresButton.y = 360

local function restartGame()
physics:start()
gameIsActive = true
end

local function retry()
score = 0
lives = 5
if ball then
ball:removeSelf()
end
if star then
star:removeSelf()
end
starCount = 0
loseMenu:removeSelf()
mainMenuButton:removeSelf()
highScoresButton:removeSelf()
retryButton:removeSelf()
finalScoreText:removeSelf()
timer.performWithDelay( 2000, restartGame)
end

retryButton = ui.newButton{
default = “loseMenu-retry.png”,
over = “loseMenu-retry.png”,
–onPress = button1Press,
onRelease = retry,
}
retryButton.x = display.contentWidth/2 + 90
retryButton.y = 360

finalScoreText = display.newText(score, 0, 0,“HelveticaNeue”, 40)
finalScoreText:setTextColor(0, 0, 0)
finalScoreText.x = display.contentWidth/2
finalScoreText.y = 196

end

local function startGame()
mainMenuBG.isVisible = false
playButton.isVisible = false
ofButton.isVisible = false
gameIsActive = true
physics:start()
end

local function openDash()
openfeint.launchDashboard()
end

local mainMenu = display.newGroup();

mainMenuBG.isVisible = true
mainMenu:insert(mainMenuBG)

playButton = ui.newButton{
default = “mainMenu-play.png”,
over = “mainMenu-play.png”,
–onPress = button1Press,
onRelease = startGame,
}
playButton.x = display.contentWidth/2
playButton.y = 230
mainMenu:insert(playButton)

ofButton = ui.newButton{
default = “mainMenu-of.png”,
over = “mainMenu-of.png”,
–onPress = button1Press,
onRelease = openDash,
}
ofButton.x = display.contentWidth/2
ofButton.y = 310
mainMenu:insert(ofButton)


– Game loop

local function gamePlay(event)
if (gameIsActive == true) then
if event.time - timeLastball >= math.random(timeInterval, (timeInterval + 400) ) then

ball = display.newImage(“regBall.png”)
if (hugeBallsMode == true) then
ball:scale( 1.5, 1.5 )
end
ball.x = math.random(halfballWidth, display.contentWidth - halfballWidth)
ball.y = -ball.contentHeight

physics.addBody(ball, “dynamic”, {bounce = 0})
ball.name = “ball”

timeLastball = event.time

impulsey = ((score/10)*.01)

if impulsey >= 2 then
impulsey = 2
end

ball:applyLinearImpulse(0, impulsey, ball.x, ball.y)

function kickTheBall(event)
scoreText.text = score
–ball = event.target
–ball:removeSelf()
if (doubleScoreMode == true) then
score = score + 2

else if (tripleScoreMode == true) then
score = score + 3

else if (doubleScoreMode == false) and (triplescoreMode == false) then
score = score + 1
end
end
end
if ball.x >= 160 then
ball:applyLinearImpulse( 3, -5, ball.x, ball.y )

end
if ball.x < 160 then
ball:applyLinearImpulse( -3, -5, ball.x, ball.y )
end

ball:removeSelf()
ball = nil
end
Runtime:addEventListener( “touch”, composeKickLine)
ball:addEventListener(“touch”, kickTheBall)

if event.time - timeLastStar >= math.random(timeStarInterval, (timeStarInterval + 400) ) then
star = display.newImage(“star.png”)
star.x = math.random(halfStarWidth, display.contentWidth - halfStarWidth)
star.y = -star.contentHeight

physics.addBody(star, “dynamic”, {bounce = 0})
star.name = “star”

timeLastStar = event.time

function collectTheStar(event)
starCountText.text = starCount
star:removeSelf()
starCount = starCount + 1

if starCount == 1 then
starIndic1.alpha = 1
end
if starCount == 2 then
starIndic2.alpha = 1
end
if starCount == 3 then
starIndic3.alpha = 1
shakePhonePlease()
starCount = 0
end
if starCount == 0 then
starIndic1.alpha = .2
starIndic2.alpha = .2
starIndic3.alpha = .2

end
end
–Runtime:addEventListener( “touch”, composeKickLine)
star:addEventListener(“touch”, collectTheStar)

end
end
end
end
Runtime:addEventListener(“enterFrame”, gamePlay)

function composeKickLine(event)
– Insert a new point into the front of the array
table.insert(endPoints, 1, {x = event.x, y = event.y, line= nil})
– Remove any excessed points
if(#endPoints > maxPoints) then
table.remove(endPoints)
end
for i,v in ipairs(endPoints) do
local line = display.newLine(event.x - 20, event.y - 20, event.x + 20, event.y - 20)
line.width = lineThickness
line:setColor(180,0,0)
transition.to(line, {time = lineFadeTime, alpha = 0, width = 0, onComplete = function(event) line:removeSelf() end})
end
if(event.phase == “ended”) then
while(#endPoints > 0) do
table.remove(endPoints)
end
end

end

local function liveCollision(self,event)
livesText.text = lives

if self.name == “goalBounds” and event.other.name == “star” then
event.other:removeSelf()
end

if self.name == “goalBounds” and event.other.name == “ball” then
lives = lives - 1
event.other:removeSelf()
if lives == -1 then
physics:pause()
gameIsActive = false
newLoseMenu()
timerGO = timer.performWithDelay(2000, setHighScore, 1)
end
end
end
goalBounds.collision = liveCollision
goalBounds:addEventListener(“collision”, goalBounds)


local onDeviceGO=true
if string.sub(system.pathForFile(“Icon.png”, system.ResourceDirectory),1,6)==’/Users’ then
onDeviceGO=false
end
–> Leave this OUT if you are NOT using director as it will already be in your file from part 1

function setHighScore( lbID, points )
if onDeviceGO then
openfeint.setHighScore( 836546, numScore)
else
print(“highscore called”)
end
end
–> Set the highscore on OpenFeint
local monitorMem = function()

collectgarbage()
print( "\nMemUsage: " … collectgarbage(“count”) )

local textMem = system.getInfo( “textureMemoryUsed” ) / 1000000
print( "TexMem: " … textMem )
end

Runtime:addEventListener( “enterFrame”, monitorMem )
[/code] [import]uid: 7116 topic_id: 12723 reply_id: 46733[/import]