I am making a game with a guy that collects things, like coins. I want to detect the collision between these two, so I can remove the coin, but I don't want the coin to interact with the character, because right now it is slowing him down slightly. It should still interact with the ground before the collision though. Thanks for your help!
function createCoin()
for i = 1, 10 do
coin = display.newCircle(0, 0, 16)
coin.x = totallength - 1000 + i * 100
coin.y = totalheight - 200
physics.addBody(coin,
{bounce = 0, friction = 1, density = 0}
)
game:insert(coin)
coin.myName = "coin"
end
end
createCoin()
local function onCollision(event)
if event.phase == "began" then
if (event.object1.myName == "coin" and
event.object2.myName == "wheel") then
event.object1:removeSelf();
end
end
end
You cannot remove objects involved in a collision during the collision handling: see Modifying Objects" at Collision event page. Use timer.performWithDelay() as documented. This should prevent your coin from interacting with player. If that doesn't work, you could create a "ghost" object that follows the coin everywhere (same size placement etc but not visible) and is added to physics as a sensor. A sensor does not cause collision dynamics but the event is fired. You would also need to do a delayed removal of coin if removal is desired.
Related
I'm creating a 2D platform game using Corona SDK and I'm stuck with collisions.
Basically there is a character that runs over this ground made of blocks. This is because sometimes there can be holes in the ground.
The game is an endless one, so as the character moves forward new blocks (and holes) are dynamically added - and also removed if they goes off screen.
It works nicely but this approach works against the collision system, let me explain how.
Now that I have the ground in place I want the character to jump but only if it is touching the ground, to avoid jumping while in air.
Whenever a collision is detected between character and ground an event is fired - two times. The first time when the character is entering a ground block and the second time when the character leaves it. So when the character lands on the ground a isGround Boolean is set to true. And when - after a jump - it leaves it the flag is set to false. The problem is that every time it exits a block to enter another - walking along the ground without jumping - the flag get updated. This makes the jump based on the isGround flag less reliable. Sometimes it happens that you can't jump because isGround == false though the character is on the ground.
Ground block creation snippet
-- init() method set the sprite of the ground block and physic to that sprite
function GroundBlock:init()
self.sprite = display.newImageRect(self.path, self.width, self.height)
self.sprite.x = self.x
self.sprite.y = self.y
physics.addBody(self.sprite, 'static', {
density = 0,
friction = 0,
bounce = 0,
box = {
halfWidth = self.width / 2,
halfHeight = self.height / 2,
y = 16,
x = 0
}
})
local collisionObj = {
name = 'ground'
}
self._collision = collisionObj
self.sprite._collision = collisionObj
self.isShow = true
end
Ground placing GroundBlocks snippet
-- init() method initialize the ground with a fixed number of blocks
function Ground:init()
self.offsetX = 0
while self.offsetX < self.camera.borderRight * 2 do
self._createBlock(1)
end
self.lastCameraPos = self.camera.borderRight
end
-- update() is called once per frame
function Ground:update()
if (self.camera.borderRight - self.lastCameraPos > self._blockWidth) then
local rand = math.ceil(math.random() * 10) % 2
if self._skippedBlock >= 2 or rand == 0 then
self._createBlock(1)
self._skippedBlock = 0
else
self._createBlock(0)
self._skippedBlock = self._skippedBlock + 1
end
self.lastCameraPos = self.camera.borderRight
end
for i, block in ipairs(self.blocks) do
if block.sprite.x < self.camera.borderLeft - block.width then
table.remove(self.blocks, i)
self.camera:remove(block.sprite)
block:delete()
end
end
end
Collision detection snippet
function Character:collision(event)
if ( event.phase == "began" ) then
if event.other._collision.name == "ground" then
self.isGround = true
end
elseif ( event.phase == "ended" ) then
if event.other._collision.name == "ground" then
self.isGround = false
print('nope')
end
end
end
A solution would be to make a ground as a single imgRect but how to make holes in it?
You could simplify your code and prevent this issue from ever occurring by tracking if the character can jump instead of tracking if the character is on the ground.
For instance,
function jump( event )
if event.phase == "began" then
if canJump then
canJump = false
-- your code that makes the player jump
end
end
end
You probably use touches to determine whether the player character jumps, right? This way, you'll trigger the jump when the touch starts as long as the character has not already jumped.
You could then reset this value in your collision function by editing it slightly:
function Character:collision(event)
if event.phase == "began" then
if event.other._collision.name == "ground" then
canJump = true
end
end
end
This way, the character's ability to jump is determined by whether or not the player has pressed jump already and if the character has hit the ground since the last jump.
This kind of approach also gives you the ability to pivot towards implementing mechanics like double jump. If instead of using a boolean canJump variable you chose to use a number variable, e.g. jumpsLeft, you could reduce the number of jumps left every time the character jumps and only let the character jump if jumpsLeft is larger than 0. Then you'd simply reset the value back to 1 (or whatever you'd want upon hitting the ground).
I am a noob to this but anyway. I am writing an app in which you must tap in order to get an object up but at some point no matter how fast you tap the object keeps going down. it's like the gravity is getting stronger and stronger.
EDIT: I don't know what code samples to give because i don't know if any of it makes sense, but i will try:
this is how i created the object:
super=display.newImage("mine.png")
super:setReferencePoint(display.BottomLeftReferencePoint)
super.y=display.contentHeight+70
super.x=display.contentWidth/2.3
super.gravityscale=1
superIntro = transition.to(super,{time=2000, y=display.contentHeight/1.2, onComplete= supergo})
physics.addBody(super, "dynamic", {density=0, bounce=0, friction=0, radius=12})`
And this is a part of my script that i think it is relevant: `
function scrollCity(self,event)
print("touch")
if self.y > 930 then
self.y = 0
else
self.y=self.y+scrollspeed*6
end
end
function activateJets(self,event)
self.y=self.y-scrollspeed*6
print("run")
end
function touchScreen(event)
print("p")
if event.phase == "began" then
if super.y<display.contentHeight/1.2+6 then
super.y=display.contentHeight/1.2
background1.enterFrame = scrollCity
Runtime:addEventListener("enterFrame", background1)
background2.enterFrame = scrollCity
Runtime:addEventListener("enterFrame", background2)
else
super.enterFrame = activateJets
Runtime:addEventListener("enterFrame", super)
end
end
if event.phase == "ended" then
Runtime:removeEventListener("enterFrame", super)
Runtime:removeEventListener("enterFrame", background1)
Runtime:removeEventListener("enterFrame", background2)
end
end
EDIT2: The gravity is set:
physics.setGravity( 0, 1.5 )
In the beginning a few taps are enough to keep it on the screen, but after a few seconds it is impossible to maintain and it just falls. I want it to go down with the same speed not to accelerate.
The reason it keeps accelerating is because gravity applies a constant force. Anything affected by gravity undergoes acceleration, not a fixed velocity.
For constant speed motion
Cancel the effect of gravity by setting gravityscale to 0 or setting global gravity to 0 - whichever is more appropriate for you - then setting the velocity of super using super:setLinearVelocity( xVelocity, yVelocity ).
For motion with acceleration
If you prefer an accelerating gravity then the approach you are using now is fine but you might consider super:setLinearVelocity(0, 0) when the tap occurs so that the object is falling from rest rather than continuing to fall quickly.
I am currently trying to work out how I should go about shooting projectiles using Corona SDK. However, I don't know the best way to go about doing this. I am guessing that you have to spawn instances of the same object and apply force to them but I don't know the best way to do it or how I should handle each instance. I am still learning Lua and just need some guidance on how to do it, any help will be appreciated.
I want to be able to check if any of the bullets hit a sensor object ( I haven't implemented this but I know how to ) at the top of the screen and then destroy the bullet that hit the sensor but how do I check each instance and destroy them individually?
This is the basic structure that I have so far.
display.setStatusBar( display.HiddenStatusBar )
local physics = require( 'physics' )
physics.start()
local speed = -500
local contentW, contentH = display.contentWidth, display.contentHeight
-- Background
local bg = display.newRect( 0, 0, contentW, contentH )
bg.anchorX = 0
bg.anchorY = 0
bg:setFillColor( 0, 1, 1 )
-- Ground
local ground = display.newRect( 0, contentH - 50, contentW, 50 )
ground.anchorX = 0
ground.anchorY = 0
ground:setFillColor( 0, 0.8, 0 )
-- Hero
local hero = display.newRect( contentW / 2, contentH / 2, 40, 40 )
hero:setFillColor( 1, 0, 0 )
function shoot( event )
if ( event.phase == 'began' ) then
local projectile = display.newRect( hero.x, hero.y, 10, 30 )
physics.addBody( projectile, 'dynamic' )
projectile.gravityScale = 0
projectile.isBullet = true
projectile:setLinearVelocity( 0, -600 )
end
end
Runtime:addEventListener( 'touch', shoot )
This was suggested on the Corona forums by the Corona staff.
To remove each bullet that hits the sensor you need to give the projectile a 'type' of something along those lines. Note that you can use any word instead of 'type', but this is my preferred way.
projectile.type = 'bullet'
Then, you need to add an event listener to the sensor object that detects the collision, in this case it is an object called 'wall'. On collision you want to remove the other object that was in the collision ( the bullet ). You can do this like so.
local function wallCollision( event )
if event.phase == 'began' then
if event.other.type == 'bullet' then
display.remove( event.other )
event.other = nil
end
end
end
wall:addEventListener( 'collision', wallCollision )
'event.other' targets the other object involved in the collision event, in this case, the 'bullet'.
Not sure this is what you are after, but the strategy to handle evolving multiple objects which can be removed later as a result of collision is:
create the bullet display object, with a collision handler
in the collision handler, if object needs to be removed then use removeSelf; other changes may require delayed change as explained in Modifying Objects.
So in your shoot function you would add, after projectile:setLinearVelocity:
projectile.collision = function (event)
...
if remove then
self:removeSelf()
end
...
end
projectile:addEventListener( "collision", projectile)
This adds the handler to each bullet. You could instead add just one handler for the sensor, it would be similar code which you would put right after creating the sensor, except that you remove the event.other instead of self:
sensor.collision = function (event)
...
if remove then
event.other:removeSelf()
end
...
end
sensor:addEventListener( "collision", sensor)
I'm all very new to this and everything in this project is just placeholders and scrap work. Some of you may recognize some of the graphics used from other Corona tutorials, but it's to help me get better, so don't be too judgemental. Here's a link to the download for the Corona Simulator: http://www.mediafire.com/download/6a78bsewgwsiyp2/GooMan.rar
Anyways, here's my issue. The jump button seems fine at first. If I hold it down, the character constantly jumps. And if I let go, he stops. If I simultaneously hold down jump while pushing one of the arrow buttons, he'll jump in that direction. However, it seems as though if I jump once, then I hit the jump button again RIGHT as the character is making contact with the ground, that he won't jump. There's a sudden lack of responsiveness. I literally have to pause slightly and wait for the character to fully hit the ground before I can make another successful jump. Why?
And here's all the relevant code for you to look at:
I have this at the beginning to help define my goo character's position:
local spriteInAir = false
local yspeed = 0
local oldypos = 0
local holding = false
I then created the jump button.
local bJump = display.newImage("Images/jumpbutton.png")
bJump.x = 445
bJump.y = 265
bJump.xScale = 0.78
bJump.yScale = 0.78
Followed up by creating an enterFrame Runtime Event which will update my goo character's position in the Y. Running the game at 60fps if that's relevant.
function checkSpeed()
yspeed = sprite.y - oldypos
oldypos = sprite.y
if yspeed == 0 then
spriteInAir = false
else
spriteInAir = true
end
end
Runtime:addEventListener( "enterFrame", checkSpeed )
Then the meat of it all. Created a function called hold which tells the game to not only make my goo character jump, but to keep my goo character constantly jumping as long as the bJump button is held down. Works perfectly. The "jumping" function is a touch event that listens for the hold function, and all of it is executed by the bJump button listening to the jumping function.
local function hold()
if holding and spriteInAir == false then
sprite:applyForce( 0, -8, sprite.x, sprite.y )
sprite:setLinearVelocity(0, -350)
spriteInAir = true
return true
end
end
local function jumping( event )
if event.phase == "began" then
display.getCurrentStage():setFocus( event.target )
event.target.isFocus = true
event.target.alpha = 0.6
Runtime:addEventListener( "enterFrame", hold )
holding = true
elseif event.target.isFocus then
if event.phase == "moved" then
elseif event.phase == "ended" then
holding = false
event.target.alpha = 1
Runtime:removeEventListener( "enterFrame", hold )
display.getCurrentStage():setFocus( nil )
event.target.isFocus = false
spriteInAir = false
return true
end
end
return true
end
bJump:addEventListener("touch",jumping)
Anyone who can help me identify this problem, I'd greatly appreciate it!
You're using a velocity check to detect if the character is on the ground. It can take a short while to set it back to zero after colliding with Box2D, so the better way to do it is to use a collision sensor:
sprite.grounded = 0 -- Use a number to detect if the sprite is on the ground; a Boolean fails if the sprite hits two ground tiles at once
function sprite:collision(event)
if "began" == event.phase then
-- If the other object is a ground object and the character is above it...
if event.other.isGround and self.contentBounds.yMax < event.other.contentBounds.yMin + 5 then
sprite.grounded = sprite.grounded + 1 -- ...register a ground collision
end
elseif "ended" == event.phase then
-- If the other object is a ground object and the character is above it...
if event.other.isGround and self.contentBounds.yMax < event.other.contentBounds.yMin + 5 then
sprite.grounded = sprite.grounded - 1 -- ...unregister a ground collision
end
end
end
sprite:addEventListener("collision")
Then, in your jump function, just check to see if sprite.grounded > 0. If it is, the player is grounded.
I'm new to programming, and I wonder if you can help me out with this problem. I created a simple basketball game, where you pull back then release to fire the ball. I want to know how to use an image of an arrow to show the direction the ball will travel(It doesn't have to be the exact trajectory, just a straight arrow). Also, i want the yScale of the arrow to show how much linear impulse will be applied.Thanks in advance!
here is my function for shooting the ball
local function ballTouched(event)
if ball.x == 100 and ball.y == 100 then
if event.phase == "began" then
display.getCurrentStage():setFocus(ball)
arrow.alpha=1
elseif event.phase == "ended" then
physics.start()
ball:applyLinearImpulse((event.xStart - event.x)/2, (event.yStart - event.y)/2, ball.x, ball.y)
display.getCurrentStage():setFocus(nil)
arrow.alpha=0
man2.alpha=1
man.alpha=0
end
end
end
Runtime:addEventListener("touch", ballTouched)
You should know the angle you are pulling back. Just set the .rotation value of your arrow to the same angle.
arrow.rotation = aimAngle
To follow up to the comments below:
I'm assuming you are pulling the basketball back kind of like AngryBirds slingshot or you have to be touching and dragging somehow to have an angle. You can use some simple angle math where you know the length of two sides of a right triangle and you can use the arctangent function to compute the angle.
local function angleBetween( srcX, srcY, dstX, dstY )
local angle = ( math_deg( math_atan2( dstY-srcY, dstX-srcX ) )+90 )
--if ( angle < 0 ) then angle = angle + 360 end
return angle%360
end
where srcX and srcY would be the position (or start position of the basketball if you're dragging it) and dstX, dstY would be the event.x, event.y of where you let up during the ended phase. The event table has a xStart and yStart members that kept track of where the touch event started, so maybe
aimAngle = angleBetween(event.xStart, event.yStart, event.x, event.y)
You may need to play with some of these numbers to get them lined up the way you want them (i.e. you may need to add 180 or something)