If object is touching another object then arguement

I have looked at http://developer.anscamobile.com/reference/index/eventphase but haven’t worked out how to use it as an argument.

such as:

[lua]if object1 is touching object2 then
-blah blah
end[/lua] [import]uid: 79135 topic_id: 14740 reply_id: 314740[/import]

I’m not sure what you’re actually asking, how to use event phases or how to detect when two objects are touching?

If you mean event phases then imagine you wanted to print “pressed” when touched and “released” when the touch was lifted, you’d do this;

[lua]function testFunction (event)
if event.phase == “began” then
print “pressed”
elseif event.phase == “ended” then
print “released”
end
end[/lua]

Was that what you were asking about? (Sorry, it could have been either but with the link you posted I’m hoping this is what you wanted to know ;)) [import]uid: 52491 topic_id: 14740 reply_id: 54527[/import]

I was asking whether two objects were touching, not a touch event [import]uid: 79135 topic_id: 14740 reply_id: 54557[/import]

It is to make a jump function, where you can only jump if touching the ground [import]uid: 79135 topic_id: 14740 reply_id: 54558[/import]

Oh right, OK - your link/wording confused me.

The easiest way is to use physics bodies, or sensors - then when the hero touches the ground set something like canJump to true, when you press the jump button set canJump to false - then you’d check if canJump was true or false and only allow jumping when it was true.

Peach :slight_smile: [import]uid: 52491 topic_id: 14740 reply_id: 54698[/import]

Hey.

I saw your post last night but didn’t have time to post anything.

Here is some drag and drop code which does exactly what you asked.

The bit of interest to you is around line 130.

Feel free to ask any questions if you get stuck or anything.

  
display.setStatusBar(display.HiddenStatusBar)  
  
physics = require("physics")  
physics.start()  
physics.setGravity(-9.8,0) -- change gravity to control the jump curve.  
   
\_H = display.contentHeight;  
\_W = display.contentWidth;  
mRand = math.random;  
  
canJump = false  
   
   
--set up display groups  
local background = display.newGroup()  
local paralax1 = display.newGroup()  
local paralax2 = display.newGroup()  
local paralax3 = display.newGroup()  
local groundgroup = display.newGroup()  
local buttons = display.newGroup()  
local enemies = display.newGroup()  
local player = display.newGroup()  
  
   
local player1 = display.newRect (0, 0, 50, 50)  
player1.x = \_W / 2 - player1.contentWidth / 2  
player1.y = \_H / 4 - player1.contentHeight  
--player1:setFillColor(0, 0, 255)  
physics.addBody(player1, {density = 1.0, friction = 0, bounce = 0.2})  
player1.myName = "player1"  
player:insert(player1)   
   
local function jumpPlayer1 (event)  
 if canJump == true then  
 player1:applyLinearImpulse( 20, 0, player1.x, player1.y ) -- the first number in brakets is the jump height  
 end  
canJump = false  
end  
  
--press red to shoot an arrow  
local redButton = display.newCircle (0, 0, 30, 30)  
redButton.x = \_W / 6  
redButton.y = \_H - \_H / 10  
redButton:setFillColor(255, 0, 0)  
redButton.alpha = .75  
  
--press blue to shoot an arrow  
local blueButton = display.newCircle (0, 0, 30, 30)  
blueButton.x = \_W / 6  
blueButton.y = redButton.y - blueButton.contentHeight / 2 - \_H / 10  
blueButton:setFillColor(0, 0, 255)  
blueButton.alpha = .75  
  
blueButton:addEventListener ("touch", jumpPlayer1)  
   
local function shoot (event)  
 local arrow = display.newRect(0, 0, 5, 20)   
 arrow.x = player1.x + player1.contentWidth / 3  
 arrow.y = player1.y + player1.contentHeight / 2 + 3  
 arrow:setFillColor(255, 0, 0)  
 physics.addBody(arrow, "kinematic", {density = 10.0, friction = 0, bounce = 0})  
 arrow:setLinearVelocity( 0, 250)  
 arrow.myName = "arrow"  
 player:insert(arrow)  
   
 arrow.collision = onLocalCollision  
 arrow:addEventListener( "collision", arrow )  
end  
   
redButton:addEventListener ("tap", shoot)  
   
local function spawnEnemy()  
 local enemy = display.newRect(0, 0, 50, 50)  
 enemy.x = \_W / 2 - enemy.contentWidth / 2 - 2  
 enemy.y = \_H --/ \_H - 50  
 enemy:setFillColor(255, 0, 0)  
 physics.addBody(enemy, {density = 0.01, friction = 0, bounce = 0})  
 enemy.myName = "enemy"  
 enemy:setLinearVelocity( 0, -100)  
 enemies:insert(enemy)  
   
 enemy.collision = onLocalCollision  
 enemy:addEventListener( "collision", enemy )  
end  
   
local firstground = display.newRect(0, 0, \_W / 3, \_H )  
firstground.x = \_W / 3 - firstground.contentWidth / 2  
firstground.y = \_H / 2  
firstground:setFillColor(0, 255, 0)  
physics.addBody(firstground, "kinematic", {density = 1.0, friction = 1, bounce = 0.2})  
firstground.myName = "firstground"  
firstground:setLinearVelocity( 0, -100)  
groundgroup:insert(firstground)  
   
local ground = display.newRect(0, 0, \_W / 3, \_H)  
ground.x = \_W / 3 - ground.contentWidth / 2  
ground.y = \_H + ground.contentHeight - \_H / 4  
ground:setFillColor(0, 230, 0)  
physics.addBody(ground, "kinematic", {density = 1.0, friction = 0, bounce = 0.2})  
ground.myName = "ground"  
ground:setLinearVelocity( 0, -100)  
groundgroup:insert(ground)  
   
   
function onLocalCollision( self, event )  
 if ( event.phase == "began" ) then  
 ----------------------------------player hits enemy---------------------------------------------------------------------------  
 if (self.myName == "player1" and event.other.myName == "enemy") then  
 print("death")  
 local function deleteSelf()  
 player1:removeSelf()  
 end  
 Timer1 = timer.performWithDelay(1,deleteSelf, 1)   
 end  
 ---------------------------------arrow hits enemy--------------------------------------------------------------------------   
 if (self.myName == "arrow" and event.other.myName == "enemy") then  
 print("ZAP!")  
 local function deleteBoth()  
 display.remove(self)  
 display.remove(event.other)  
 end  
 Timer1 = timer.performWithDelay(1,deleteBoth, 1)   
 end  
 ---------------------------------player hits ground-------------------------------------------------------------------------------   
 if (self.myName == "player1" and event.other.myName == "ground") then  
 canJump = true   
 end  
 print( self.myName .. ": collision began with " .. event.other.myName )   
 ---------------------------------player hits firsdtground-------------------------------------------------------------------------------   
 if (self.myName == "player1" and event.other.myName == "firstground") then  
 canJump = true   
 end  
 print( self.myName .. ": collision began with " .. event.other.myName )   
 -----------------------------------------------------------------------------------------------------------------------------------------   
 end  
end  
   
player1.collision = onLocalCollision  
player1:addEventListener( "collision", player1 )  
   
Timer1 = timer.performWithDelay(mRand(3000, 4500),spawnEnemy, 0)   
  

Hope that helps :slight_smile:

Dan [import]uid: 67933 topic_id: 14740 reply_id: 54715[/import]

without physics:

try using Millerszone code from here

http://developer.anscamobile.com/forum/2011/09/05/best-method-collision-detection

[blockcode]
– Ball object
local ball = display.newImageRect(“ballSmall.png”, 12, 12)
ball.x = 240; ball.y = 160
ballSizeX = 12; ballSizeY = 12

– Paddle object
local leftPaddle = display.newImageRect(“paddleSmall.png”, 12, 60)
leftPaddle.x = 35; leftPaddle.y = 160
paddleSizeX = 12; paddleSizeY = 60

– Collision detection
local function collision(box1x, box1y, box1w, box1h, box2x, box2y, box2w, box2h)
if (box1x > (box2x + box2w)) then return false
elseif ((box1x + box1w) < box2x) then return false
elseif (box1y > (box2y + box2h)) then return false
elseif ((box1y + box1h) < box2y) then return false
else return true
end
end

– Check if ball hit paddle
if collision(ball.x, ball.y, ballSizeX, ballSizeY, leftPaddle.x, leftPaddle.y, paddleSizeX, paddleSizeY) then
– if ball hit paddle then do something
end
[/blockcode] [import]uid: 7911 topic_id: 14740 reply_id: 54718[/import]

Thanks guys :slight_smile: excellent [import]uid: 79135 topic_id: 14740 reply_id: 54734[/import]

@spider n
Nice code, I learned a few things from it.

Added to my “tool box” hehe. That was very cool!

ng [import]uid: 61600 topic_id: 14740 reply_id: 54810[/import]

@nic

No worries, glad you found it useful. (wait until I have the random terrain generator up and running;) [import]uid: 67933 topic_id: 14740 reply_id: 54891[/import]