I think the problem was that it just wasn’t inside of a function, so they din’t know what an arrow was.
Here’s my main and arrow class if you want to see them.
The only problem is that when an arrow hits a ballon, it no longer knows what arrowX is.
function setup()
displayMode(FULLSCREEN)
supportedOrientations(LANDSCAPE_ANY)
arrow = Arrow()
ballon1 = Ballon(math.random(120,WIDTH - 200))
ballon2 = Ballon(math.random(120,WIDTH - 200))
-- Random Y starting place
last_points = 0
best_points = 0
MENU = 0
PLAYING = 1
SCORING = 2
resetGame()
end
function resetGame()
gameState = MENU
missScore = 0
points = 0
end
function draw()
if gameState == MENU then
background(0, 0, 0, 255)
pushStyle()
font("AmericanTypewriter")
fontSize(100)
fill(187, 25, 27, 255)
text("Ballon Shoot",WIDTH/2,HEIGHT/2)
popStyle()
pushStyle()
fontSize(50)
fill(195, 92, 20, 255)
text("Tap to begin (:",WIDTH/2,HEIGHT/2 - 100)
popStyle()
pushStyle()
fontSize(20)
fill(195, 92, 20, 255)
text("Last Score: " .. last_points, 0 + 100, HEIGHT - 20)
popStyle()
pushStyle()
fontSize(20)
fill(195, 92, 20, 255)
text("Best Score: " .. best_points, WIDTH - 100, HEIGHT - 20)
popStyle()
elseif gameState == PLAYING then
background(35, 91, 31, 255)
arrow:draw()
ballon1:draw()
ballon1:update()
ballon2:draw()
ballon2:update()
ballon1:speedCheck()
hittCheck()
endCheck()
arrow:fly()
showPoints()
showMisses()
elseif gameState == SCORING then
background(0, 0, 0, 255)
pushStyle()
fill(166, 156, 204, 255)
font("Arial-ItalicMT")
fontSize(100)
text("Your Final Score:"..points, WIDTH/2,HEIGHT/2)
popStyle()
end
if arrow:missCheck() == true then
missScore = missScore + 1
end
end -- end draw
-- Shows points
function showPoints()
pushStyle()
fontSize(48)
fill(255, 255, 255, 255)
font("Arial-BoldMT")
text("Points:"..points,0 + 100,HEIGHT - 50)
popStyle()
end
-- Shows misses
function showMisses()
pushStyle()
fontSize(48)
fill(255, 255, 255, 255)
font("AmericanTypewriter-Light")
text("Misses:"..missScore,WIDTH - 100,HEIGHT - 50)
popStyle()
end
function hittCheck()
-- Checks to see if the ballon has been hit
if arrow:getFlying() then
if ballon1:hittCheck( arrow:getPos() ) or
ballon2:hittCheck( arrow:getPos() ) then
sound(SOUND_EXPLODE, 14044)
print("Hitt!")
points = points + 1
arrow:reset()
end
end
end
function endCheck()
-- Checks to see if the miss count is to high
if missScore >= 3 then
gameState = SCORING
last_points = points
if points > best_points then
best_points = points
end
end
end
function touched(touch)
if touch.state == BEGAN then
if gameState == MENU then
gameState = PLAYING
elseif gameState == PLAYING then
arrow:touched(touch)
elseif gameState == SCORING then
resetGame()
end
end
end
Now the arrow class
Arrow = class()
function Arrow:init()
-- you can accept and set parameters here
self.arrowFlying = false
self.arrowY = 100
self.arrowX = 0
self.ARROW_X = self.arrowX
self.arrowSpeed = 12
self.resetXvalue = 0
end
function Arrow:draw()
-- Codea does not automatically call this method
pushStyle()
smooth()
lineCapMode(ROUND)
stroke(65, 59, 33, 255)
strokeWidth(10)
line(self.arrowX,self.arrowY,self.arrowX + 100,self.arrowY)
sprite("Tyrian Remastered:Arrow Right",self.arrowX + 100,self.arrowY)
popStyle()
end
function Arrow:update()
if self.arrowX >= WIDTH then
self:reset()
end
end
function Arrow:fly()
-- Moves arrow if it's been relesed
if self.arrowFlying == true then
self.arrowX = self.arrowX + 8
end
end
-- Sets arrow back to starting posuiton
function Arrow:reset()
self.arrowFlying = false
self.arrowX = ARROW_X
self.arrowY = math.random(50,HEIGHT - 161)
end
function Arrow:getPos()
return vec2(self.arrowX, self.arrowY)
end
function Arrow:missCheck()
local ret_val = false
-- Checks if the arrow missed
if self.arrowX >= WIDTH and self.arrowFlying == true then
self:reset()
ret_val = true
end
return ret_val
end
function Arrow:getFlying()
return self.arrowFlying
end
function Arrow:touched(touch)
if touch.state == BEGAN then
self.arrowFlying = true
print("arrow is flying")
end
end