Need Collission on Movieclip object

I have a game application. so far I have created two separate movie objects i.e. Player1 and Player2. Now when Player 1 punches to player2 then I want collission event /action but my below code is not detecting collission event. please help.
local physics = require(“physics”)
physics.start()
physics.setGravity(0, 20)
display.setStatusBar( display.HiddenStatusBar )

– Load external Lua libraries (from project directory)
local movieclip = require( “movieclip” )
local ui = require( “ui” )

– Load background
local background = display.newImage(“Test2.png”)
background:scale(0.48, 0.64 )
background.x = 250
background.y = 160
foreground = display.newGroup()
– Initialize a second cube anim (this time by creating the image table dynamically)
imageTable = {}
for i = 1,3 do
table.insert( imageTable, “Hero” … i … “.png” )
end

myAnim = movieclip.newAnim( imageTable )
foreground:insert( myAnim )

myAnim.x = 195
myAnim.y = 230
myAnim:scale(0.35, 0.35 )

local myAnim1 = movieclip.newAnim{ “Enemy1.png”, “Enemy2.png”, “Enemy3.png”, “Enemy4.png”, “Enemy5.png”, “Enemy6.png”, “Enemy7.png”,“Enemy8.png”,“Enemy9.png”,“Enemy10.png”, “Enemy11.png”, “Enemy12.png”, “Enemy13.png”, “Enemy14.png”, “Enemy15.png”, “Enemy16.png”,“Enemy17.png”,“Enemy18.png”,“Enemy19.png”,“Enemy20.png”,“Enemy21.png”}
myAnim1.x = 210
myAnim1.y = 210
myAnim1:scale(0.4, 0.4 )

local function launchAttack1()
myAnim:playTest{ startFrame=1, endFrame=3, loop=1}
myAnim1:play{ startFrame=1, endFrame=21, loop=1}
end

local resetButton = ui.newButton{
default = “coconut.png”,
over = “coconut.png”,
x = 400,
y = 280,
onPress = launchAttack1,
text = “P”,
emboss = true
}

myAnim1.collision = onLocalCollision
myAnim1:addEventListener( “collision”, myAnim1 )

– Take care of collisions
local function onLocalCollision( self, event )
if ( event.phase == “began” ) then

print( self.myName … ": collision began with " … event.other.myName )

elseif ( event.phase == “ended” ) then

print( self.myName … ": collision ended with " … event.other.myName )

end
end

thanks in advance [import]uid: 80396 topic_id: 13592 reply_id: 313592[/import]

Hey,

Firstly, you need to post your code in < lua > tags, otherwise it’s a headache to read.

Have you actually given your objects names? As in, obj.myName = “player”?

I can’t see it above anywhere so there is nothing to print. [import]uid: 52491 topic_id: 13592 reply_id: 49874[/import]

Thanks peach for reply please find the below correct code and let me know is collision detection posible with movieclip object?
if posible then please share an example with us.

[lua]local physics = require(“physics”)
physics.start()
physics.setGravity(0, 20)
display.setStatusBar( display.HiddenStatusBar )

– Load external Lua libraries (from project directory)
local movieclip = require( “movieclip” )
local ui = require( “ui” )
– Load background
local background = display.newImage(“Test2.png”)
background:scale(0.48, 0.64 )
background.x = 250
background.y = 160
foreground = display.newGroup()
– Initialize a second cube anim (this time by creating the image table dynamically)
imageTable = {}
for i = 1,3 do
table.insert( imageTable, “Hero” … i … “.png” )
end

player1 = movieclip.newAnim( imageTable )
foreground:insert( player1 )

player1.x = 195
player1.y = 230
player1:scale(0.35, 0.35 )

local player2 = movieclip.newAnim{ “Enemy1.png”, “Enemy2.png”, “Enemy3.png”, “Enemy4.png”, “Enemy5.png”, “Enemy6.png”, “Enemy7.png”,“Enemy8.png”,“Enemy9.png”,“Enemy10.png”, “Enemy11.png”, “Enemy12.png”, “Enemy13.png”, “Enemy14.png”, “Enemy15.png”, “Enemy16.png”,“Enemy17.png”,“Enemy18.png”,“Enemy19.png”,“Enemy20.png”,“Enemy21.png”}
player2.x = 210
player2.y = 210
player2:scale(0.4, 0.4 )

local function launchAttack1()
player1:playTest{ startFrame=1, endFrame=3, loop=1}
player2:play{ startFrame=1, endFrame=21, loop=1}
end

local resetButton = ui.newButton{
default = “coconut.png”,
over = “coconut.png”,
x = 400,
y = 280,
onPress = launchAttack1,
text = “P”,
emboss = true
}
player2.collision = onLocalCollision
player2:addEventListener( “collision”, player2 )

– Take care of collisions
local function onLocalCollision( self, event )
if ( event.phase == “began” ) then
print( self.myName … ": collision began with " … event.other.myName )
elseif ( event.phase == “ended” ) then
print( self.myName … ": collision ended with " … event.other.myName )

end

end[/lua] [import]uid: 80396 topic_id: 13592 reply_id: 49886[/import]

Line 26, add player1.myName = “player1”

Line 31, add player2.myName = “player2”

You also need to add physics to the players. Physics work fine with sprites.

Check out Corona for Newbies part 4 on Techority - it might help you with physics/collisions :slight_smile: [import]uid: 52491 topic_id: 13592 reply_id: 50151[/import]