module(...,package.seeall) force\_multiplier=4 state={} ready=false pop={} thresholds={} function newBullet() local sceneGroup=display.newGroup() local bullet=display.newImage(sceneGroup,"bulleth.png") bullet.x=\_W-240 bullet.y=\_H-50 bullet.xScale=.09 bullet.yScale=.09 bullet.type="bullet" physics.addBody(bullet,"kinematic",{density=0.2, friction=.2, bounce=.5, radius=20}) bullet.linearDampling=.3 bullet.angularDampling=.8 bullet.isBullet=true --forces continous collison detection bullet.isSensor=true function bullet:touch(event) if (ready) then if(event.phase=="began") then display.getCurrentStage():setFocus( event.target ) self.isFocus=true self.bodyType="kinematic" --Stop current motion if any self:setLinearVelocity(0,0) self.angularVelocity=0 myLine=nil elseif(self.isFocus) then if(event.phase=="moved") then if (myLine) then myLine.parent:remove(myLine) --erase previous line, if any end --Check this myLine=display.newLine(sceneGroup,self.x,self.y, event.x, event.y) myLine:setStrokeColor(255,255,255,50) myLine.alpha=.3 myLine.strokeWidth=8 elseif(event.phase=="ended" or event.phase=="cancelled") then display.getCurrentStage():setFocus(nil) self.isFocus=false --audio.play(shot) if(myLine) then myLine.parent:remove(myLine) end --Launch bullet self.bodyType="dynamic" self:applyForce((self.x-event.x)\*force\_multiplier,(self.y-event.y)\*force\_multiplier, self.x, self.y) self.timer=timer.performWithDelay(1000,function(event) state:dispatchEvent({name="change",state="fire"}) if(event.count==1) then timer.cancel(self.timer) self.timer=nil end end, 1) end end end end function bullet:collision( event ) if(event.phase=="began") then if(event.other.type=="balloon") then local s=event.other.points local m=event.other.multiplier state:dispatchEvent({name="change",state="score", points=s, multiplier=m}) elseif(event.other.type=="wall") then end elseif(event.phase=="ended") then if(event.other.type=="balloon") then audio.play(pop1) event.other.pop() elseif(event.other.type=="wall") then self:removeSelf() self=nil end end end bullet:addEventListener("collision", bullet) bullet:addEventListener("touch", bullet) return bullet end
Error i get:
bullet.lua:52: attempt to index field ‘parent’ (a nil value)
In my game i drag the bullet back and a line appears behind the bullet to show how much power is going into the shot (longer the line higher the power) and the trajectory of the bullet. You can think of the way the line forms behind a bird in Angry Birds. Sometimes it works perfectly but if i pull back sometimes very quickly or just normally, in no hurry, i get that error. I want to make sure that error never comes so what do i do?
The code the error applies to is where it says myLine=nil and from then on, the variable that makes up the line is myLine.