Hi,
I’m a beginner with Lua and Codea. I have a basic understanding of tables. In this project, I need to have an arrow (shown as a sprite) shoot across the screen at my touch. My current problem is that the arrow resets at the assigned (x,y) position every time I touch the screen, instead of spawning a new arrow. How can I fix this? Alternative ways to do this? (And I’m aware that my code is very flawed other than this error, though not by anything big enough to prevent it from running.)
function setup()
B={} --table to hold balloons
S={}
dude=readImage("SpaceCute:Rocketship")
backgrnd=readImage("Documents:Galaxy yes")
star=readImage("Planet Cute:Star")
gameover=readImage("Documents:Game over")
dudeX=100 dudeY=HEIGHT/2 dudeSize=100 dudeSpeed=2
score=0
balloonFrequency=5 --number of balloons released per second
LaunchBalloon() --set off the first balloon
LaunchStar()
timer=0 --initialise timer
end
function LaunchBalloon()
b={} --a table to hold details of our new balloon
b.x=WIDTH
b.y=math.random(0,HEIGHT)
b.w = math.random(30,80) --width
--make height dependent on width, to keep the proportions reasonable
b.h = 50 --height
b.s = math.random(5,15) --speed in pixels/second
b.c=color(0, 0, 0, 255)
table.insert(B,b) --add to our table of balloons
end
function LaunchStar()
s={}
s.x=dudeX+dudeSize/2
s.y=dudeY
s.s=30
s.sp=7.5
table.insert(S,s)
end
function draw()
sprite(backgrnd,WIDTH/2,HEIGHT/2)
sprite(dude,dudeX,dudeY,dudeSize)
dudeY=dudeY-dudeSpeed
dudeSpeed=dudeSpeed+.25
sprite(star,s.x,s.y,s.s)
timer=timer+DeltaTime
if timer>1/balloonFrequency then
LaunchBalloon()
timer=0 --reset timer
end
for i,b in pairs(B) do
b.x=b.x-b.s --adjust y position using speed
if b.x-b.h/2>HEIGHT then table.remove(B,i) end --delete balloon if it goes off screen
fill(b.c) --set colour for this balloon
ellipse(b.x,b.y,b.w,b.h)
--check for collision with star
if s.x+s.s/2>b.x-b.w/2 --if front of star hits front of balloon and
and s.y+s.s/2>b.y-b.h/2 --if top of star hits bottom of balloon and
and s.y-s.s/2<b.y+b.h/2 --if bottom of star hits top of balloon then
then score=score+1 print("Hit!",score)
table.remove(B,i) --delete this balloon
b.x=-1 --reset balloon
end
if dudeX+dudeSize/2>b.x-b.w/2 --if front of star hits front of balloon and
and dudeY+dudeSize/2>b.y-b.h/2 --if top of star hits bottom of balloon and
and dudeY-dudeSize/2<b.y+b.h/2 then --(I know I need to finish this conditional)
end
end
for i,s in pairs(S) do
s.x=s.x+s.sp
if s.x-s.s/2>WIDTH then --if star leaves screen, remove from table
table.remove(S,i)
end
end
end
function touched(touch)
if touch.state==BEGAN then
dudeSpeed=dudeSpeed-8
LaunchStar()
end
end