@starcrunch
I figured out what’s causing the problem!
When the issue occurs, the console prints out that the ball color is a different color than it appears on screen!
This means that for some reason the ball is displaying a different color for us to see than actually in the code.
Now the question is, why is this occurring? And, how can I fix it?
Here is the majority of the code in the game that would be causing the problem:
[lua]local colors = { “red”, “green”, “purple”, “yellow” }
–colors in game
local colorValues = {
red = {1, 0, 0},
yellow = {1, 1, 0},
purple = {.5, 0, .5},
green = {0, 1, 0}
}
–change balls color function
local function SetColor(object) – Update our own private object color
local current = object.m_color
repeat
object.m_color = colors[math.random(4)]
print(object.m_color)
until object.m_color ~= current – just to ensure variety, but optional
object:setFillColor(unpack(colorValues[object.m_color]))
print(unpack(colorValues[object.m_color]))
end
–collision of ball and wheel
local function onCollision(event)
if (event.phase == “began”) then
local ball = event.target
local posx, posy = event.x - wheel.x, event.y - wheel.y
–keep track of positions of color quadrents
local angle = math.atan2(posy, posx) --pos = collision point
local degrees = math.deg(angle)
degrees = (degrees - wheel.rotation) % 360 – The wheel may have rotated ahead, so what are we really
– hitting? Normalize the result to the (0, 360) range.
if (degrees < 90 ) then
– purple
quadrantColor = “purple”
physics.setGravity( 0, -3)
elseif (degrees < 180) then
– green
quadrantColor = “green”
physics.setGravity( 0, -3)
elseif (degrees < 270) then
– red
quadrantColor = “red”
physics.setGravity( 0, -3)
else
– yellow
quadrantColor = “yellow”
physics.setGravity( 0, -3)
end
if (event.target.m_color ~= quadrantColor) then – This is the color you found earlier
storyboard.gotoScene( “gameover” )
wheel:removeEventListener(‘touch’, rotateWheel)
ball:removeEventListener( “collision”, onCollision )
audio.stop()
end
elseif (event.target.m_color == quadrantColor) then
SetColor(ball) – pick new color
audio.play(collisionSound)
updateScore()
currentScore = 0
local currentScoreFilename = “currentScore.text”
saveValue( currentScoreFilename, tostring(currentScore))
currentScore = score
local currentScoreFilename = “currentScore.text”
saveValue( currentScoreFilename, tostring(currentScore))
print(“QUADRANT COLOR”, quadrantColor)
print(“BALL COLOR”, event.target.m_color)
print(“ANGLES”, angle, wheel.rotation)
print("") – dummy line, just to add a space in the console (if able to test multiple collisions)
if (score > highScore) then
highScore = score
local highScoreFilename = “highScore.text”
saveValue( highScoreFilename, tostring(highScore))
end
end
if (score > 10) then
print(“making gravity stronger”)
physics.setGravity( 0, 5)
end
end
ball:addEventListener( “collision”, onCollision )
SetColor(ball)
end[/lua]
-Max