Hello,
I developed a part of the game that involves to shot balloons by bullet.
in the main I called the function newBullet() to create the bullet.
and I want to add a bullet when the last enter in collision with wall or ballon.
So I want instantiate a new objet when I have collision .
But I have 2 pbs:
-
bullet.lua:14: physics.addBody() cannot be called when the world is locked and in the middle of number crunching, such as during a collision event.
-
bullet.lua:27: attempt to call method ‘setLinearVelocity’ (a nil value)-
This is below the bullet.lua
Thks for help. how can I add a new bullet when collision is done with last bullet.
Bullet.lua :
module( …, package.seeall);
force_multiplier = 10;
shot = {};
pop = {};
function newBullet()
local bullet = display.newImage( “images/bullet.png”,20,20);
bullet.x = 100 ; bullet.y = _H - 200;
bullet.type = “bullet”;
physics.addBody( bullet, “kinematic”, {density=0.2, friction=0.2, bounce=0.5, radius= 20});
bullet.linearDamping = 0.3;
bullet.angularDamping = 0.8;
bullet.isBullet = true;
bullet.isSensor = true;
function bullet:touch(e)
local t = e.target;
if(e.phase == “began”)then
display.getCurrentStage( ):setFocus( t);
self.isFocus = true;
self.bodyType = “kinematic”;
self:setLinearVelocity( 0,0 );
self.angularVelocity = 0;
self.myLine = display.newLine( self.x, self.y, e.x, e.y)
elseif(self.isFocus) then
if(e.phase == “moved”) then
if(self.myLine ) then
display.remove( self.myLine )
end
self.myLine = display.newLine( self.x, self.y, e.x, e.y)
self.myLine:setStrokeColor( 1,1,1);
self.myLine.strokeWidth = 8
elseif(e.phase == “ended” or e.pahse == “cancelled”) then
display.getCurrentStage( ):setFocus( nil );
self.isFocus = false
audio.play( shot )
if(self.myLine ) then
display.remove( self.myLine )
end
self.bodyType = “dynamic”;
self:applyForce( (self.x-e.x)*force_multiplier, (self.y-e.y)*force_multiplier, self.x, self.y);
end
end
end
function bullet:collision(e)
newBullet();
if(e.phase == “began”)then
if(e.other.type==“balloon”)then
local s = e.other.points;
local m = e.other.multiplier;
elseif(e.other.type==“wall”)then
– Do something
end
elseif(e.phase == “ended”) then
if(e.other.type == “balloon”)then
audio.play(pop);
e.other:pop();
elseif(e.other.type == “wall”) then
self:removeSelf( );
self = nil;
end
end
end
bullet:addEventListener( “collision”, bullet )
bullet:addEventListener( “touch”, bullet );
return bullet;
end