Here is my Code.
Bullet Class which return Object of bullet type
[lua]
local bun_bullet = {
name = “bun”,
type = “bullet”,
density=0.2,
friction=0,
bounce=0,
size = 20,
rotation = 100
}
– Init bullet
local bullet = display.newImage(bun_bullet.name…".png");
– Place bullet
bullet.x = 170; bullet.y = _H + 20;
– Set up physical properties
physics.addBody(bullet, {density=bun_bullet.density, friction=bun_bullet.friction, bounce=bun_bullet.bounce, radius=bun_bullet.size});
bullet.gravityScale = 0
bullet.linearDamping = 0.3;
bullet.angularDamping = 0.8;
bullet.isBullet = true;
bullet.isSensor = true;
– Transition the bullet into position each time it’s spawned
transition.to(bullet, {time=600, y=_H - 140, transition = xyz});
return Bullet
[/lua]
And I am having two object which bullet type object will collide
[lua]
–Collision
local function onCollision(event)
if ( event.phase == “began” ) then
if(event.object1.myName==“xyz” and event.object2.myName==“bun”) then
event.object1:removeSelf()
event.object2:removeSelf()
end
if(event.object1.myName==“abcd” and event.object2.myName==“bun”) then
event.object1:removeSelf()
event.object2:removeSelf()
end
end
end
Runtime:addEventListener(“collision”, onCollision)
[/lua]
Above object with Myname=abcd is object of below type
[lua]
local ball = display.newImage(group, “Special.png” )
ball.x=300
ball.y=900
physics.addBody(ball, { bounce=0.0, friction=0.0 } )
ball.myName=“abcd”
ball:addEventListener( “collision”, ball )
ball:applyForce(10, -120, ball.x-20, ball.y)
[/lua]
Above Object with Myname=xyz is object of below type
[lua]
local myImage = display.newImageRect(view, “xyz.png”,50,50 )
myImage.x = x
myImage.y = y
physics.addBody( myImage, { density=0.0, friction=0.0, bounce=0.0,velocity=-40 } )
myImage:setLinearVelocity(0, -30)
myImage.gravityScale = 0
if(coli==“true”) then
myImage.collision = onCollision
myImage:addEventListener( “collision”, myImage );myImage.myName=“letter”;
end
local myImage1 = display.newImageRect(view, “xyz.png”,50,50 )
myImage1.x = x + 40
myImage1.y = y
physics.addBody( myImage1, { density=0.0, friction=0.0, bounce=0.0,velocity=-40 } )
myImage1:setLinearVelocity(0, -30)
myImage1.gravityScale = 0
if(coli==“true”) then
myImage1.collision = onCollision
myImage1:addEventListener( “collision”, myImage );
myImage1.myName=“letter”;
end
[/lua]
Logic is some object are moving up side with linearVelocity and other are coming from down side with force.
I am not allowing object to get in contact with this code
[lua]
local function preCollision( event)
event.contact.isEnabled = false
end
local function postCollision( event )
self:removeEventListener( “preCollision” )
self:removeEventListener( “postCollision” )
end
Runtime:addEventListener( “postCollision”,postCollision )
Runtime:addEventListener( “preCollision”,preCollision)
[/lua]
One condition is occurring in my game when both object are getting in same place at that time my bullet type object hits object with Myname=abcd than object with Myname=xyz are getting disturb.
I think now all things are Clear