I’ll post all of the code here so you can see what’s going on, but what I’m trying to do is in the last onCollsion function. There is a monsterHealth variable that is supposed to decrease by monsterHealthReduct, which is 50, whenever the pongBall hits the monster. The number is not decreasing. Unfortunately I’m getting kind of confused with how my code is organized, but I’ve gone too far to rewrite it all.
[code]display.setStatusBar(display.HiddenStatusBar)
– 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(“paddle.png”); textureCache[3].isVisible = false;
textureCache[4] = display.newImage(“monster.png”); textureCache[4].isVisible = false;
textureCache[5] = display.newImage(“pongBall.png”); textureCache[5].isVisible = false;
local halfballWidth = textureCache[1].contentWidth * .5
local halfStarWidth = textureCache[2].contentWidth * .5
local standardGameplay = true
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 monsterVelocity = 2
local monsterAltVelocity = 2
local monsterHealthText
local monsterHealth = 100
local monsterHealthReduct = 50
local background = display.newImage(“grassBackground.png”)
paddle = display.newImage(“paddle.png”)
paddle.x = display.contentCenterX
paddle.y = display.contentHeight - paddle.contentHeight
paddle.name = “paddle”
paddle.collision = onCollision
paddle:addEventListener(“collision”, paddle)
halfpaddleWidth = paddle.contentWidth * .5
paddle.isVisible = false
monster = display.newImage(“monster.png”)
monster.x = display.contentCenterX
monster.y = 100
monster.name = “monster”
halfmonsterwidth = monster.contentWidth * .5
monster.isVisible = false
local goalBounds = display.newRect(0, 470, 320, 10)
physics.addBody(goalBounds, “static”, {bounce = 0})
goalBounds.name = “goalBounds”
scoreText = display.newText(score, 0, 0, “HelveticaNeue”, 35)
scoreText:setTextColor(255, 255, 255)
scoreText.x = 50
scoreText.y = 25
starCountText = display.newText(starCount, 0, 0, “HelveticaNeue”, 35)
starCountText:setTextColor(255, 255, 255)
starCountText.x = 250
starCountText.y = 25
monsterHealthText = display.newText(monsterHealth, 0, 0, “HelveticaNeue”, 35)
monsterHealthText:setTextColor(255, 255, 255)
monsterHealthText.x = 150
monsterHealthText.y = 25
monsterHealthText.text = monsterHealth
– Game loop
local function gamePlay(event)
if standardGameplay then
if event.time - timeLastball >= math.random(timeInterval, (timeInterval + 400) ) then
local ball = display.newImage(“regBall.png”)
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()
score = score + 1
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
end
Runtime:addEventListener( “touch”, composeKickLine)
ball:addEventListener(“touch”, kickTheBall)
if event.time - timeLastStar >= math.random(timeStarInterval, (timeStarInterval + 400) ) then
local 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
end
–Runtime:addEventListener( “touch”, composeKickLine)
star:addEventListener(“touch”, collectTheStar)
end
end
end
if (starCount == 1) then
standardGameplay = false
timer.performWithDelay(3000, monsterAppear)
starCount = 0
function pongBallNew()
local pongBall = display.newImage(“pongBall.png”)
pongBall.isVisible = true
pongBall.x = 160
pongBall.y = 200
physics.addBody(pongBall, “dynamic”, {bounce = 1})
end
timer.performWithDelay(3010, pongBallNew)
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(255,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 paddleMovement(event)
if event.x >= halfpaddleWidth and event.x <= display.contentWidth - halfpaddleWidth then
paddle.x = event.x
end
end
paddle:addEventListener(“touch”, paddleMovement)
function monsterAppear()
starCountText.isVisible = false
scoreText.isVisible = false
score = score + 50
paddle.isVisible = true
monster.isVisible = true
physics.addBody(monster, “kinematic”, {bounce = 0})
physics.addBody(paddle, “kinematic”, {bounce = 0})
–transition.to(monster, {time = 3000, alpha = 1, width = 0})
–timer.performWithDelay(3000,monsterAppear)
moveMonster = function()
monster.x = monster.x + monsterVelocity
if (monster.x > 285) then
monsterVelocity = -monsterVelocity
end
if (monster.x < 35) then
monsterVelocity = -monsterVelocity
end
monster.y = monster.y + monsterAltVelocity
if (monster.y > 150) then
monsterAltVelocity = -monsterAltVelocity
end
if (monster.y < 65) then
monsterAltVelocity = -monsterAltVelocity
end
end
Runtime:addEventListener( “enterFrame”, moveMonster )
end
function onCollision(self, event)
if self.name == “monster” and event.other.name == “pongBall” then
monsterHealth = monsterHealth - monsterHealthReduct
if monsterHealth == 0 then
table.insert(toRemove, event.other)
monsterLives = 5
end
end
end
monster.collision = onCollision
monster:addEventListener(“collision”, monster)
[code] [import]uid: 7116 topic_id: 12168 reply_id: 312168[/import]