Hi guys, I’ve a problem with my code to rilieve collision between a bullet and an enemy.
With this code , the bullets appears and moves correctly, but they don’t rilieve any collision.
Can you help me pls? :wacko: :wacko:
This code is the bullet function.
-- Heart bar module local physics=require("physics") local blob=require("scene.game.lib.blob") -- Define module local M = {} function M.new( options ) local projectile = display.newImageRect( "scene/game/img/seme.png", w, h ) local direction function projectile:fireLeft() projectile = display.newImageRect( "scene/game/img/seme.png", w, h ) projectile.alpha = 1 projectile.x=self.x-100 projectile.y=self.y projectile.isBullet=true physics.addBody( projectile, "dynamic", {isSensor=true, bounce=0, density=2} ) projectile.gravityScale = 0 direction=self.flip print(direction) projectile:setLinearVelocity( -800, 0 ) timer.performWithDelay(2000, function() display.remove(projectile) end) end function projectile:fireRight() projectile = display.newImageRect( "scene/game/img/seme.png", w, h ) projectile.alpha = 1 projectile.x=self.x projectile.y=self.y projectile.isBullet=true physics.addBody( projectile, "dynamic", { isSensor=true, bounce=0, density=2} ) projectile.gravityScale = 0 direction=self.flip print(direction) projectile:setLinearVelocity( 800, 0 ) timer.performWithDelay(2000, function() display.remove(projectile) end) end function projectile:collision( event ) print("sssssssss") local phase = event.phase local other = event.other -- local y1, y2 = self.y + 50, other.y - ( other.type == "enemy" and 25 or other.height/2 ) -- local vx, vy = self:getLinearVelocity() if phase == "began" then print("sjsjjsjsjsjsjs") if self and ( other.type == "blob" or other.type == "enemy" ) then print("aaaaa") other:die() end end end local function onLocalCollision( self, event ) if ( event.phase == "began" ) then if self and ( other.type == "blob" or other.type == "enemy" ) then print("aaaaa") other:die() end end end function projectile:finalize() -- On remove, cleanup instance display.remove( projectile ) end projectile:addEventListener( "finalize" ) projectile.collision = onLocalCollision projectile:addEventListener( "collision") -- Return instance projectile.name="bullet" projectile.type="bullet" return projectile end return M
and this a function inside the Hero file that implements the joystick and recall the bullet function when the user push c button or a display button.
local function key( event ) local phase = event.phase local name = event.keyName if ( phase == lastEvent.phase ) and ( name == lastEvent.keyName ) then return false end -- Filter repeating keys if phase == "down" then if "left" == name or "a" == name then left = -acceleration flip = -0.133 end if "right" == name or "d" == name then right = acceleration flip = 0.133 elseif "space" == name or "buttonA" == name or "button1" == name or "c" == name or "buttonB" == name or "button2" == name then if ("space" == name or "buttonA" == name or "button1" == name) then instance:jump() end if ("c" == name or "buttonB" == name or "button2" == name) then print("fire") print(flip) instance:fireLaser() end end if not ( left == 0 and right == 0 ) and not instance.jumping then instance:setSequence( "walk" ) instance:play() end elseif phase == "up" then if "left" == name or "a" == name then left = 0 end if "right" == name or "d" == name then right = 0 end if left == 0 and right == 0 and not instance.jumping then instance:setSequence("idle") end end lastEvent = event end function instance:fireLaser() if flip == 0.133 then self.bullet:fireRight() elseif flip == -0.133 then self.bullet:fireLeft() end end
pls help me!!! this is the last thing and i finish…I can’t find the solution :unsure:
Thank you!