Help with collision needed

So change the onPlayerCollision to the code above ?

Yes. But you would change the first line to:

function player:collision(event)

I have this :

function player:collision(event) if event.phase == "began" then print("Touched") end end function scene:show(event) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then print("show started") elseif ( phase == "did" ) then print("show showing objects") player:addEventListener("collision") Runtime:addEventListener("touch", onTouch) end end function scene:hide(event) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then print("hide started") composer.removeScene( "start" ) elseif ( phase == "did" ) then print("hide removing objects") player:removeEventListener("collision") Runtime:removeEventListener("touch", onTouch) end end

No changed results .

Here is my entire game.lua code :

-- requires local composer = require( "composer" ) local scene = composer.newScene() local physics = require("physics") physics.start() local ball local player -- background local gradient = { type="gradient", color1={ 0, 2, 2 }, color2={ 0.1, 1.8, 0.3 }, direction="left" } local text = display.newText("", 230, 20, native.systemFontBold, 20) text:setFillColor( gradient ) transition.fadeOut( text, { time=5000 } ) function scene:create(event) local screenGroup = self.view local background = display.newImageRect("terrain.jpg",display.contentWidth,display.contentHeight) background.x = display.contentCenterX background.y = display.contentCenterY screenGroup:insert(background) ceiling = display.newImage("invisibleTile.png") ceiling.x = 0 ceiling.y = -29 physics.addBody(ceiling, "static", {density=.1, bounce=0.2, friction=.2}) screenGroup:insert(ceiling) theFloor = display.newImage("invisibleTile.png") theFloor.x = 0 theFloor.y = 347 physics.addBody(theFloor, "static", {density=.1, bounce=0.2, friction=.2}) screenGroup:insert(theFloor) end local function onTouch(event) if(event.phase == "ended") then local transitionBall = transition.to(ball, {x=event.x, y=event.y, time=125} ) end end ball = display.newImage("ball1.png") ball.x = 400 ball.y = 160 ball.myName = "ball" player = display.newImage("player.png") player.x = 100 player.y = 300 player.myName = "player" local function loopObject() local objectDown= function() transition.to(player, { time=600, y=20, onComplete=loopObject }) end transition.to(player, { time=600, y=300, onComplete=objectDown }) end loopObject() local options = { effect = "fade", time = 400 } function player:collision(event) if event.phase == "began" then print("Touched") end end function scene:show(event) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then print("show started") elseif ( phase == "did" ) then print("show showing objects") player:addEventListener("collision") Runtime:addEventListener("touch", onTouch) end end function scene:hide(event) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then print("hide started") composer.removeScene( "start" ) elseif ( phase == "did" ) then print("hide removing objects") player:removeEventListener("collision") Runtime:removeEventListener("touch", onTouch) end end function scene:destroy(event) end scene:addEventListener("create", scene) scene:addEventListener("show", scene) scene:addEventListener("hide", scene) scene:addEventListener("destroy", scene) return scene

Hmmm…

Is your player’s variable name called player?

For example is it like this: 

local player = ......

It is, nevermind.

function player:enterFrame() end player:addEventListener("collision") or local function enterFrame() end Runtime:addEventListener("enterFrame", enterFrame) 

Try adding an enterFrame with a print statement and see if that works.

function player:collision(event) if event.phase == "began" then print("Touched") end end function player:enterFrame() print("Touched") end function scene:show(event) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then print("show started") elseif ( phase == "did" ) then print("show showing objects") player:addEventListener("collision") Runtime:addEventListener("touch", onTouch) end end function scene:hide(event) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then print("hide started") composer.removeScene( "start" ) elseif ( phase == "did" ) then print("hide removing objects") player:removeEventListener("collision") Runtime:removeEventListener("touch", onTouch) end end

I have that and nothing changed .

You did not add the event listener. 

I put this and nothing changed .

function player:collision(event) if event.phase == "began" then print("Touched") end end player:addEventListener("collision")

Hi @H.R.H,

In none of your previous posted code do I see that you’ve made the player into a physical object by the “physics.addBody()” command. That’s most likely the issue here.

Brent

I just did this :

 player = display.newImage("player.png") player.x = 100 player.y = 300 physics.addBody(player, "static", {density=.1, bounce=0.2, friction=.2, radius=12}) player.myName = "player"

and nothing changed .

It helped I just had to add another body to the ball too . Thanks a lot.

When I do this :

 ball = display.newImage("ball1.png") ball.x = 400 ball.y = 160 physics.addBody(ball, "dynamic") ball.myName = "ball"

The ball is falling from the screen . How can I fix it ?

You won’t get a collision response between static+static or static+kinematic or kinematic+kinematic. One body should be “dynamic” type to register actual collision responses.

Brent

Is your game like a “zero gravity” type of thing? Like top-down view where you don’t want gravity factoring into things? If so, set the gravity to 0 for the entire physical simulation:

https://docs.coronalabs.com/api/library/physics/setGravity.html

I put this at the top of the file :

local physics = require("physics") physics.start() physics.setGravity( 0, 6 )

But the same thing is happening with the ball.

And yes , my game is top down and I don’t want gravity affecting anything .

Right, so you’ve set downward gravity of 6. That means things will fall. Make it 0.

I have this at the top and nothing changed :

physics.setGravity( 0 )