Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Bullet collision problem! Help me pls!
Started by andrea.marrocco95 Aug 12 2019 01:59 AM

2 replies to this topic
[TOPIC CONTROLS]
This topic is locked This topic is locked
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

andrea.marrocco95

[GLOBAL: userInfoPane.html]
andrea.marrocco95
  • Observer

  • 6 posts
  • Corona SDK

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
		
		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
		
		projectile:setLinearVelocity( 800, 0 )
		

		timer.performWithDelay(2000, function()
			display.remove(projectile)
		end)
	end

	function projectile:collision( event )
		
		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
			
			if self and ( other.type == "blob" or other.type == "enemy" ) then
				
				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
				
				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've finished...I can't find the solution   :unsure:

 

Thank you!

 

 

 



[TOPIC: post.html]
#2

XeduR @Spyric

[GLOBAL: userInfoPane.html]
XeduR @Spyric
  • Contributor

  • 934 posts
  • Corona SDK

You already made this exact post yesterday. On forums like these, it may take several hours to even a day or two to respond, especially over weekends. There's no need to double post.



[TOPIC: post.html]
#3

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 26,196 posts
  • Enterprise

Lets keep the conversation on the other post:

 

https://forums.coronalabs.com/topic/76028-help-me-with-a-bullet-collision-problem-pls/

 

Rob




[topic_controls]
[/topic_controls]