Display an image on collision of two objects - coronasdk

I am working with CI and want to display an image when two objects collide with each other, at that time a specific image is displayed and that image should displayed be only for the very short duration in which the collusion occurred.
It seems like a poof function or something like this. But I cannot understand how to solve this problem.

i will give some sample code for your question.one animated boy collision with other object(coll1).
animatedboy.collision=function(self,event)
if event.other.name == "coll1" then
picture.isVisible=true
function updateTime()
display.remove(picture) or picture.isVisible=false
end
timeTaken = timer.performWithDelay( 1000, updateTime, timerTicks )
end
animatedboy:addEventListener( "collision", animatedboy )

Related

I need help or atleast a pointer with collision in a game im making for fun

So I made a game, made a map, and everything is working fine. The problem is I made a very dumb collision system that worked first, but I am running into problems.
I am using player's X and Y positions to draw character, and using players tileX and tileY (x/32 and y/32) to detect collision. Heres a picture which explains my problem:
The Red box is players tileX and tileY cordinate.
Player still moves beyound the wall where the collision should happen.
The TileX doesnt let increases/decreases happen if they collide with a solid tile, BUT player's X and Y (sprite) still moves beyond that box for 31 more pixels. I have no idea how to fix this. My player image is not centered, its drawn on the top-right corner.
This is the current code im using:
for i=1, #lsx_map1 do
if math.floor(player.fx/32) == lsx_map1[i] and math.floor(player.fy/32) == lsy_map1[i] then
player.speedx = 0
player.speedy = 0
print("COLISSION DETECTED ON "..player.x.." "..player.y)
else
print(colVar)
colVar = colVar+1
end
end
if colVar == #lsx_map1 then
player.x = player.fx
player.y = player.fy
end
lsx_map1 is the number of solid tiles, and colVar should equal to that number if collision doesnt happen. In case collision happens, that number doesnt increase by one, and then nothing happens. Ask for any more details you need if you want to help me but you need more information.
Any help or tips would be appreciated. Thank you.

Corona SDK: Moving a player Up and Down

I have a player who is navigating in space. Since it is space, there is not gravity therefore no parabolic trajectory. The player is just going in a horizontal line from left to right. The player is not actually moving, but the background is, so it looks like he is. The x value is fixed.
I have 2 buttons that help the player avoid obstacles like asteroids. One button gives the player upward force, the other one downward force. The following are the functions called when those buttons are pressed.
function moveUp( event )
if event.phase == "ended" then
player:applyForce(0, 8, player.x, player.y)
player:setSequence("jump")
jumpChannel = audio.play(jumpSound)
end
return true
end
function moveDown( event )
if event.phase == "ended" then
player:applyForce(0, -8, player.x, player.y)
player:setSequence("jump")
jumpChannel = audio.play(jumpSound)
end
return true
end
The problem with this implementation is that whenever a force is applied the player keeps going in that direction. Then you have to apply force in the opposite direction and he will keep going in that direction forever. That is not what I want. What I want is :
when UP is pressed, player goes up for certain value (say 50 px) in Y. Then the player keeps going in the horizontal direction from left to right in the new altitude.
When DOWN is pressed, player goes down certain value. Then the player keeps going in the horizontal direction from left to right in the new altitude.
What is the best way to accomplish this? Can I do this using the applyForce function or is there another method? Thanks in advance for your time!
You have to remember that F=ma: if force applied is 0, a is 0, which means velocity does not change. You have two choices:
apply an opposite force that will decrease the speed, and stop when speed is 0. Every Body has myBody.linearDamping factor which you could set to non-zero. If that doesn't work, you can apply your own damping: you make it proportional to Body velocity, so you need an enterFrame event handler that updates the force based on current velocity:
function enterFrame(e)
local v = player:getLinearVelocity()
player:applyForce(- a * v)
end
Here the "a" is damping, some arbitrary number (0 is what you have now; the larger it is, the faster the player will return to 0 velocity). I haven't checked whether the applyForce() is additive (adds to existing forces) or absolute (replaces existing). But you get the idea.
directly move the player:
function moveUp( event )
if event.phase == "ended" then
player:setLinearVelocity(0, 8) -- pixels/sec
You will still need an enterFrame handler to monitor position and setLinearVelocity(0,0) when desired position reached.
You will get smoother, more "realistic" motion with option 1.

Lua: When does it call functions?

Using the Love framework by the way.
Ok, so I'm looking to create a random map generation. I obviously only want it drawn once, so I tried to set up a very basic structure. Which isn't working and I can't figure out why.
function love.load()
testVar = 1
end
function love.draw()
if testVar == 1 then
testFunction()
love.graphics.print("Update", 20, 200)
end
love.graphics.print(testVar, 100, 100)
end
function testFunction()
love.graphics.print("Success", 20, 300)
testVar = 0
end
What that is doing is only printing "0", thanks to the command to print testVar up in the draw function. So it seems that it's updating the testVar value without actually running testFunction. Is this something to do with Lua?
For those unfamiliar with Love, love.draw is called every frame, love.load only initially.
This code works as expected. It's just love.draw is called every frame (multiple times per second), so the output from the first frame is quickly overwritten by next frames. Usually you use love.update when you need to make changes to your state (for example, based on user input) and love.draw to draw that state on the screen (every frame).

Corona masking, how to add 2 or more masks to display group

Hy all, i'm develope 1 little game both for android and iOS in Corona.
I need to set a mask to an image when users touch the group.
Here is my code:
local function eat( event )
if event.phase == "began" the
local mask = graphics.newMask( "file/mask.png" )
local playSound = audio.play( biteSound )
onDishGroup:setMask( mask )
onDishGroup.maskX= event.x
onDishGroup.maskY = event.y
return true
end
The question is, how can i add multiple mask to that group ?
Its work well, but this will set only 1 mask at a time, the old will remove or it will be just moving to the new x y.
You cannot add multiple masks like that...
But you can put several groups one inside the other and add mask to them... Yes it is a ugly hack but...

Corona/Box2D detect collision with non-moving static objects

For posting reasons here's a simple version of what I'm trying to do.
On the screen I have a simple circle object that is static and doesn't move. The user can then drag and drop a straight line. If the line passes through that circle I'm hoping for the collision event to be triggered.
It appears that unless one of the objects are moving the collision is never detected. Can I detect the collision when the line is drawn?
Collision Event
function onHit(e)
print("hit");
end
Runtime:addEventListener("collision", onHit)
Touch Event
local startX = 0;
local startY = 0;
local endX = 0;
local endY = 0;
function onTouch(e)
if(e.phase == "began") then
startX = e.x
startY = e.y
elseif(e.phase == "moved") then
endX = e.x
endY = e.y
elseif(e.phase == "ended") then
local line = display.newLine(startX, startY, endX, endY)
line:setColor(100, 100, 100)
line.width = 2
physics.addBody(line, "static", { })
end
end
Runtime:addEventListener("touch", onTouch)
Create circle
local c = display.newCircle(50, 50, 24)
physics.addBody(c, "static", { radius = 24 })
This page from the Corona SDK docs describes the bodyType property about halfway down the page. When describing "static" bodies, it says (my emphasis):
static bodies don't move, and don't interact with each other;
examples of static objects would include the ground, or the walls of a
pinball machine.
That means that one of the objects has to be something other than static.
Here's an idea, although I haven't tried it myself: (See update below.) Make the line dynamic when you first create it. Set it to static a few milliseconds later using a timer.performWithDelay function. If a collision event occurs in the meantime, you will know that you've got an overlap, and can set the bodyType back to static immediately. If you don't get a collision event, the bodyType will still be dynamic in the delayed routine, and you'll know you didn't have an overlap. In this case, you'll still need to set the line to static in the delayed routine.
UPDATE: Tested this, using your code as the starting point
I changed the collision event to always set both objects' bodyType to static:
function onHit(e)
print("hit")
e.object1.bodyType = "static"
e.object2.bodyType = "static"
end
Then I changed the addBody call for the line to add it as a dynamic body, with new code to setup a timer.PerformWithDelay function to check after a short time:
physics.addBody(line, "dynamic", { })
timer.performWithDelay(10,
function()
if line.bodyType == "dynamic" then
print ("NO OVERLAP")
line.bodyType = "static"
end
end)
The results were, unfortunately, mixed. It works most of the time, perhaps 95%, but fails occasionally when drawing a line that starts outside the circle and ends inside, which should be an overlap, but is sometimes reported as no overlap. I wasn't able to figure out why this was happening. I'm posting this anyway, hoping that it gets you going, and also thinking that someone may be able to figure out the inconsistent behavior and educate us both.
Failing that, you could add an additional check for the "no overlap" case to check if either endpoint of the line is closer than the radius of the circle away from the center. That would be make things work, but I suppose it misses the whole point of letting the physics engine to the work.
Anyway, good luck!
Perform a raycast when you release your mouse press. This way you can keep both objects static, and know that they intersect via the raycast callback.
(I know that this is an old post, but it was the first hit on my Google search and is incorrect afaic)
Set the body type as kinematic, set it as a sensor, and update its position to the entity it's tied to every frame. Unlike static, kinematic can interact with static.

Resources