Hello, I am new and I have a problem with understanding collision in Corona, I am sorry my English is not so good.
In the game, I have a player who throws fireballs at skeletons, if any fireballs hit any skeleton, I would like to remove that skeleton, but when fireball hits him, it stuck in “skeleton’s body”. Collision is not detected, and there is no warning message.
I have two groups inserted into sceneGroup.
sceneGroup:insert(fireballs);
sceneGroup:insert(runningSkeletons);
This is how I create fireballs on touch event:
fireball = display.newImageRect(“images/fireball.png”, 32, 32);
fireball.x = player.x + 20;
fireball.y = player.y;
physics.addBody( fireball, ‘dynamic’,{bounce= 0});
fireball.gravityScale = 0
fireball.type = “fire”
fireballs:insert(fireball) – insert into group
I use timer function to create new skeleton, it’s a spritesheet:
runningSkeleton = display.newSprite( runningSkeletonSheet, runningSkeletonSequenceData )
runningSkeleton.x = display.contentWidth + 200;
runningSkeleton.y = 200;
runningSkeleton:setSequence(“running”)
runningSkeleton:play();
runningSkeleton.type = “skeleton”;
physics.addBody(runningSkeleton, “dynamic”, {bounce=0});
runningSkeletons:insert(runningSkeleton)
I move these objects in enterFrame function:
for i = 1, runningSkeletons.numChildren, 1 do
runningSkeletons[i].x = runningSkeletons[i].x - speed;
end
for i = 1, fireballs.numChildren, 1 do
fireballs[i].x = fireballs[i].x + 8;
– I have also tried to setLinearVelocity instead, it did not help
end
Global collision function:
local function globalCollision(event)
if (event.phase == “began”) then
if (event.object1.type == “fire”
and event.object2.type == “skeleton”) then
print (“collision”) – not showing
event.object2:removeSelf();
– I have tried to change alpha instead of removing object, does not work
event.object2.alpha = .5;
end
end
end
Runtime:addEventListener( “collision”, globalCollision );
Actually, it sometimes remove the skeleton and I can see “collision” in output, but let’s say it works one time, than next five times collision is not detected.
What am I doing wrong?
