If your “log” had meaning of console output:
22:25:50.976 zombie collising with hero phase: began
22:25:51.330 zombie collising with hero phase: ended
22:25:53.692 zombie collising with beam phase: began
22:25:53.692 beam
22:25:53.780 zombie collising with beam phase: began
22:25:53.780 beam
If your “log” had meaning of “what the problem is?”:
When hero and zombie collides everything is ok, collision starts and ends, but when beam and zombie collides collision starts, but it repeats few times and never ends…
Maybe I can just put some delay just ignore what collision never ends or repeats?
And in code some particle effects are just for testing.
Game scene code:
local composer = require( "composer" )
local joystickPlugin = require( "plugin.joystick")
local physics = require( "physics" )
local json = require( "json" )
local scene = composer.newScene();
function scene:create(event)
physics.start()
physics.setGravity(0,0)
local sceneGroup = self.view;
system.activate("multitouch")
local AttackButton = display.newImageRect("Images/mehaAttackButton.png",70,70)
AttackButton.x = 500
AttackButton.y = 300
local AttackAnimationSheetData = {
width = 400,
height = 400,
numFrames = 10,
sheetContentWidth = 800,
sheetContentHeight = 2000
}
local AttackAnimationSheet = graphics.newImageSheet("Images/mehaAttackAnimation.png", AttackAnimationSheetData)
local sequenceData = {
{ name = "Attack", start = 1, count = 10, time = 500, loopCount = 1}
}
local props = {
x = -30,
y = 300,
backgroundRadius = 40,
movedStickRadius = 20
}
local filePath = system.pathForFile( "beamParticleData.json" )
local f = io.open( filePath, "r" )
local beamParticleData = f:read( "*a" )
f:close()
local beamParticleParams = json.decode( beamParticleData )
local filePath = system.pathForFile( "chargeParticleData.json" )
local f = io.open( filePath, "r" )
local chargeParticleData = f:read( "*a" )
f:close()
local chargeParticleParams = json.decode( chargeParticleData )
local filePath = system.pathForFile( "blastParticleData.json" )
local f = io.open( filePath, "r" )
local blastParticleData = f:read( "*a" )
f:close()
local blastParticleParams = json.decode( blastParticleData )
local map = display.newImage("Images/survivalModeMap.png")
map.x = display.contentCenterX
map.y = display.contentCenterY
local hero = display.newImage("Images/mehaHat.png")
hero.x = display.contentCenterX
hero.y = display.contentCenterY
physics.addBody(hero,"static")
hero.direction = "up"
hero.hp = 100
hero.objType = "hero"
hero.selectedWeapon = "anti-alien"
hero.anotherWeapon = "empty"
local zombie = display.newImage("Images/zombie.jpg")
zombie.x = display.contentCenterX
physics.addBody(zombie,"dynamic")
zombie.y = -100
zombie.hp = 30
zombie.objType = "zombie"
local function shoot()
if hero.selectedWeapon == "anti-alien" then
if hero.direction == "up" then
local beamParticle = display.newEmitter( beamParticleParams )
beamParticle.x = display.contentCenterX + 50
beamParticle.y = display.contentCenterY - 170
local beam = display.newRect( display.contentCenterX + 50, display.contentCenterY - 130, 15, 100 )
beam.fill = {1,0,0}
beam.alpha = 0
beam.objType = "beam"
physics.addBody(beam,"static")
timer.performWithDelay(700, function()
beam:removeSelf()
end)
end
end
end
if hero.selectedWeapon == "anti-alien" then
if weapon then
weapon:removeSelf()
end
local weapon = display.newImageRect("Images/anti-alien.png",
35, 100)
weapon.x = display.contentCenterX + 50
weapon.y = display.contentCenterY - 30
AttackButton.isVisible = false
local shootButton = display.newImageRect("Images/shootButton.png", 70, 70)
shootButton.x = 500
shootButton.y = 300
shootButton:addEventListener("touch", shoot)
hero:toFront()
local chargeParticle = display.newEmitter( chargeParticleParams )
chargeParticle.x = display.contentCenterX + 50
chargeParticle.y = display.contentCenterY - 75
local blastParticle = display.newEmitter( blastParticleParams )
blastParticle.x = display.contentCenterX - 100
blastParticle.y = display.contentCenterY
end
local random = math.random(4)
local hp = display.newText("Hp: "..hero.hp,display.contentCenterX,300,
"Lobster-Regular", 50)
local function zombieSpawn()
end
local checkHeroCollision
checkHeroCollision = function()
if hero.isColliding then
hero.hp = hero.hp - 10
hp.text = "Hp:"..hero.hp
timer.performWithDelay(500, checkHeroCollision)
end
end
local function zombieCollision(self, event)
local other = event.other
local phase = event.phase
print( self.objType, " collising with ", other.objType, " phase: ", phase )
if( other.objType == "hero" ) then
if( phase == "began" ) then
hero.isColliding = true
checkHeroCollision()
elseif phase == "ended" then
hero.isColliding = false
end
elseif( other.objType == "beam" ) then
if( phase == "began" ) then
print("beam")
end
end
end
zombie.collision = zombieCollision
zombie:addEventListener("collision", zombie)
local function AttackDamage(self, event)
if event.other == zombie then
if event.phase == "began" then
zombie.hp = zombie.hp - 10
if zombie.hp <= 0 then
Runtime:removeEventListener("enterFrame", enemy)
timer.performWithDelay(1, function()
zombie:removeSelf()
end)
end
end
end
end
local Joystick = joystickPlugin.newJoystick(props)
Joystick.background:setFillColor(0.5)
Joystick.movedStick:setFillColor(0.7)
function enterFrame(event)
if Joystick.isActivated() then
local vector = Joystick.getVector()
if vector.x < -0.2 then
map.x = map.x + 2
zombie.x = zombie.x + 2
hero.rotation = 270
hero.direction = "left"
elseif vector.x > 0.2 then
map.x = map.x - 2
zombie.x = zombie.x -2
hero.rotation = 90
hero.direction = "right"
end
if vector.y < -0.2 then
map.y = map.y + 2
zombie.y = zombie.y + 2
hero.rotation = 0
hero.direction = "up"
elseif vector.y > 0.2 then
map.y = map.y - 2
zombie.y = zombie.y-2
hero.rotation = 180
hero.direction = "down"
end
end
end
local AttackCoolDown = 0
function Attack(event)
if(event.phase == "began") then
if AttackCoolDown == 0 then
local attackHitbox = display.newRect( display.contentCenterX, display.contentCenterY, 100, 90 )
attackHitbox.fill = {1,0,0}
attackHitbox.alpha = 0
physics.addBody(attackHitbox, "static")
attackHitbox.collision = AttackDamage
attackHitbox:addEventListener("collision", attackHitbox)
timer.performWithDelay( 200, function()
end)
local mehaAttackAnimation = display.newSprite( AttackAnimationSheet, sequenceData )
mehaAttackAnimation.x = display.contentCenterX
mehaAttackAnimation.y = display.contentCenterY
if hero.direction == "up" then
mehaAttackAnimation.rotation = 0
attackHitbox.x = display.contentCenterX
attackHitbox.y = display.contentCenterY - 70
attackHitbox.rotation = 0
elseif hero.direction == "right" then
mehaAttackAnimation.rotation = 90
attackHitbox.x = display.contentCenterX + 70
attackHitbox.y = display.contentCenterY
attackHitbox.rotation = 90
elseif hero.direction == "down" then
mehaAttackAnimation.rotation = 180
attackHitbox.x = display.contentCenterX
attackHitbox.y = display.contentCenterY + 70
attackHitbox.rotation = 180
elseif hero.direction == "left" then
mehaAttackAnimation.rotation = 270
attackHitbox.x = display.contentCenterX - 70
attackHitbox.y = display.contentCenterY
attackHitbox.rotation = 180
end
mehaAttackAnimation:play()
AttackCoolDown = 1
timer.performWithDelay( 500, function()
attackHitbox:addEventListener("collision", zombie)
attackHitbox:removeSelf()
mehaAttackAnimation:removeSelf()
AttackCoolDown = 0
end)
end
end
end
function hpDetection(event)
if hero.hp == 0 then
composer.removeScene( "scenes.survival_gamemode" )
Runtime:removeEventListener("enterFrame", enemy)
Runtime:removeEventListener("enterFrame", hpDetection)
AttackButton:removeEventListener("touch", Attack)
zombie:removeEventListener("collision", zombie)
Runtime:removeEventListener("enterFrame", enterFrame)
zombie:removeSelf()
hero:removeSelf()
AttackButton:removeSelf()
hp:removeSelf()
map:removeSelf()
Joystick = nil
end
end
local function enemy()
if zombie.x < display.contentCenterX then
zombie.x = zombie.x + 1
end
if zombie.y > display.contentCenterY then
zombie.y = zombie.y - 1
end
if zombie.x > display.contentCenterX then
zombie.x = zombie.x - 1
end
if zombie.y < display.contentCenterY then
zombie.y = zombie.y + 1
end
zombie.rotation = 90 + math.deg(math.atan2(zombie.y - hero.y, zombie.x - hero.x))
end
--Runtime:addEventListener("enterFrame", enemy)
Runtime:addEventListener("enterFrame", hpDetection)
AttackButton:addEventListener("touch", Attack)
Runtime:addEventListener("enterFrame", enterFrame)
end
function scene:destroy(event)
physics.stop()
composer.gotoScene("scenes.survival_gamemode")
end
scene:addEventListener("create", scene);
scene:addEventListener("destroy", scene);
return scene;