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:
1 2 3 | if object1 is touching object2 then -blah blah end |
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;
1 2 3 4 5 6 7 | function testFunction (event) if event.phase == "began" then print "pressed" elseif event.phase == "ended" then print "released" end end |
I was asking whether two objects were touching, not a touch event
It is to make a jump function, where you can only jump if touching the ground
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 :)
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | 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) |
without physics:
try using Millerszone code from here
http://developer.anscamobile.com/forum/2011/09/05/best-method-collision-detection
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | -- 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 |
Thanks guys :) excellent
@spider n
Nice code, I learned a few things from it.
Added to my "tool box" hehe. That was very cool!
ng
@nic
No worries, glad you found it useful. (wait until I have the random terrain generator up and running;)
send message and call to a person
Forum: Report Spam + PM
[INSTALL_PARSE_FAILED_NO_CERTIFICATES]
[SOLVED] Director ERROR: Failed to execute new( params ) function on 'screen1'
Too many (200) local variables
Corona SVG Level Builder released. Drag and drop physics level editor with Inkscape and SVG.
Small bug in Collectible Items example
Pivot Joints and apparent elasticity
Can't make removeSelf() delayed
pls support dofile() and loadstring() only for android..
params trouble
In alert event function, does have "event.target"?
Alpha testing eXp Jet 2.0
Android App Size Limit
★★★ROBOT 99★★★ "Promotional Code Available"
Getting Corona going in the classroom
How can I tell if two pictures collide onscreen?
RDM