Restart functionality

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.

Editing went wrong with all the returns not being properly formatted. Tell me if there is a way to also fix that on this forum.

@Oliver76 - first, use three ~ at start and end of your code to format it correctly, as I’ve done above. I also fixed the indenting, which was crooked and making it hard to see which code belonged where.

You haven’t given us enough code to find the error, but it may be that you destroy the ball in the middle of a touch, after state==BEGAN and before state==ENDED, so the velocity table gets destroyed, causing the error when state==ENDED. But without more code, it’s hard to say.