Hi there Codea forums,
I have a puzzling bug in my game and I have not been able to resolve it and I’ expectations been trying to fix it for weeks on end. My game is an Angry Bird like game where you fling a ball and try to hit the tower and the enemies inside. I would like it when all the enemies are destroyed to go to a Victory screen and have button appear that restarts the game. I have been successful in the creation of the screen and detecting number of enemies on screen. It is not working though when I program the button to restart the game. I used the restart() code to restart the game, but it loads it up only to have an error with my ball’s self.velocity.
I have tried to make it pause the physics in my ball class, stop loading the code of ball movement when my Gameover variable is true, and even destroying the ball in main class when Gameover = true.
It has an error stating:
Error [string “Ball = class()…”].51: attempt to index a nil value (field ‘velocity’)
My code for my Ball class is here:
Ball = class()
function Ball:Setup(x,y,radius,r)
-- you can accept and set parameters here
self.x = x
self.y = y
self.radius=radius
self.body=physics.body(CIRCLE,radius-52)
self.body.x=x
self.body.y=y
self.body.restitution=0.5
self.body.angle=r
self.dx=0
self.dy=0
self.applyf=false
self.velocity=nil
end
function Ball:Update()
self:draw()
end
function Ball:draw()
pushMatrix()
translate(self.body.x, self.body.y)
rotate(self.body.angle)
sprite("Documents:owl2",0,0,self.radius)
popMatrix()
end
function Ball:touched(touch)
-- Codea does not automatically call this method
self.tx = touch.x
self.ty = touch.y
if touch.state == BEGAN then
inTouch = true
self.velocity = {}
end
if touch.state == MOVING then
table.insert(self.velocity,1,vec2(touch.deltaX,touch.deltaY))
end
if touch.state == ENDED then
self.dx = 0
self.dy = 0
self.n = 0
for i = 1,10 do
if self.velocity [i]then
self.n = self.n + 1
self.dx = self.dx + self.velocity[i].x
self.dy = self.dy + self.velocity[i].y
end
end
if self.n >0 then
self.dx = self.dx /self.n
self.dy = self.dy /self.n
self.applyf = true
end
inTouch = false
self.body.type = DYNAMIC
self.body:applyForce(vec2(self.dx*250,self.dy*250))
end
sound(SOUND_JUMP, 27350)
end
Line 51 is:
If self.velocity [i] then
I hope someone can help me with this and if any more information is required is required, don’t hesitate to ask me.