So I'm creating a game where an enemy is firing a bullet. The bullet eventrully hits a collision I made, when it hits the end of the screen. Im wanting at this point to remove it and clear the address back to a nil value. When checking the address before and after the collision, no change was made. please help with any insight you might have on this.
local onCollision = function(event)
if event.phase == "began" then
event.object2:removeSelf();
event.object2 = nil;
end
end
Runtime:addEventListener("collision",onCollision);
local onCollision = function(event)
if event.phase == "began" then
event.target:removeSelf();
event.target= nil;
end
end
Runtime:addEventListener("collision",onCollision);
Related
What is wrong with this code that it crashes the simulator?
I'm new to Corona SDK, but I know alot of Lua from Roblox.
local x,y,touching,active = 2,2,false,true
local background = display.newImage("Icon.png",(display.pixelWidth/2)+30,y)
background:scale(60,60)
print("Started")
function move()
y=y+1
end
function onObjectTouch(event)
if event.phase == "began" then
touching = true
while touching == true do
timer.performWithDelay( 1000, move)
end
elseif event.phase == "ended" then
touching = false
end
return true
end
background:addEventListener("touch",onObjectTouch)
Corona is event based. You essentially lock up the touch event in a while loop which prevents the ended phase from triggering to break your endless loop.
Corona has a Runtime event called "enterframe". It is called 30-60 times a second (probably less depending on the work it is doing) that is essentially an update method.
Using your logic:
function onObjectTouch(event)
if event.phase == "began" then
touching = true
elseif event.phase == "ended" then
touching = false
end
end
funtion EnterFrame(event)
if touching == true then
timer.performWithDelay( 1000, move)
end
end
There are other ways to accomplish this by setting a dynamic objects velocity when the touch is started and setting that velocity to 0 when the touch ends.
I've been having some trouble with player movement. The thing is, movement is controlled by four arrow-buttons and while one of them is pressed, the character moves in that direction. However, if the player doesn't lift his finger before moving away from the button, the player moves indefinitely. The code is the following:
function moveright(self,event)
self.x=self.x+2
end
function rightkeypressed(event)
if event.phase == "began" then
knight.enterFrame = moveright
Runtime:addEventListener("enterFrame",knight)
end
if event.phase == "ended" or event.phase == "cancelled" then
Runtime:removeEventListener("enterFrame",knight)
end
end
rightkey:addEventListener("touch",rightkeypressed)
This is the same for all four directions.
Very Simple once you understand the touch listeners
if event.phase == "ended" or event.phase == "cancelled" then
knight.enterFrame = moveright
Runtime:addEventListener("enterFrame",knight)
Runtime:removeEventListener("enterFrame",knight)
end
That should work if you have any questions or is not working just ask
So I have this cube which if the player clicks on a button moves. I also have this block which if the cube collides with, the cube will get sent back to a start position. I have tried my best but I can't seem to get both of them working correctly together. With my code, the cube get's glitchy and moves all over the screen between the block and start position. This is if the button is still pressed. If it is released at the same moment of collision it does work, but obviously the players won't be paying attention to that.
function touchHandler( event )
if event.phase == "began" then
display.getCurrentStage():setFocus( event.target )
event.target.isFocus = true
Runtime:addEventListener( "enterFrame", enterFrameListener )
holding = true
elseif event.target.isFocus then
if event.phase == "moved" then
elseif event.phase == "ended" then
holding = false
Runtime:removeEventListener( "enterFrame", enterFrameListener )
display.getCurrentStage():setFocus( nil )
event.target.isFocus = false
end
end
return true
end
leftbutton:addEventListener( "touch", touchHandler )
This is the code for the collision:
function onCollision( event )
if ( event.phase == "began" ) then
transition.cancel( )
transition.moveTo( cube, {time = 0, x = 35, y = 35} )
end
return true
end
redblock:addEventListener( "collision", onCollision )
Also: whenever the cube falls on an edge of the redblock and starts spinning and gets sent back to start. It keeps spinning and it starts moving on its own.
I hope someone can help!
Thanks.
You are still in the "moved" phase during the collision hence why the block tries to reset, but is then dragged around afterwards. Once you detect the collision, you need to remove the event handler from the "leftbutton" so that the touch event is no longer in progress.
I want to call removeBody function when i click on an object. As soon as i click
the object the removeBody function must be called on an object and after i release the
mouse then it must again behave like a physics object. These are the code that i
was trying. please give any suggestion
local function removephysics(objId)
physics.removeBody(objId)
end
local Bodyobject={density=3.0, friction=0.2, bounce=0.3, radius=20}
object1=display.newImage("img1.png")
object1.x=325
object1.y=200
object1.id="obj"
physics.addBody(object1,Bodyobject)
object1.addEventListener("touch",removephysics)
you can use event.phase to get the state of touch listener see this code
physics = require("physics")
physics.start()
local Bodyobject={density=3.0, friction=0.2, bounce=0.3, radius=20}
function removephysics(event)
if event.phase == "began" then
physics.removeBody(object1)
elseif event.phase == "ended" then
physics.addBody(object1,Bodyobject)
end
end
object1=display.newImage("img1.png")
object1.x=325
object1.y=200
object1.id="obj"
physics.addBody(object1,Bodyobject)
object1:addEventListener("touch",removephysics)
I was wondering how I would check if the user has touched the screen, but is holding their touch down and is not moving. Please help if you have anything I can go from. Ive been looking around and have yet to find anything to handle this.
You can use/modify this: (It's what Rob Miracle says)
local holding = false
local function enterFrameListener()
if holding then
-- Holding button
-- Code here
-- Code here
-- Code here
else
-- Not holding
-- Code here
-- Code here
-- Code here
end
end
local function touchHandler( event )
if event.phase == "began" then
display.getCurrentStage():setFocus( event.target )
event.target.isFocus = true
Runtime:addEventListener( "enterFrame", enterFrameListener )
holding = true
elseif event.target.isFocus then
if event.phase == "moved" then
elseif event.phase == "ended" then
holding = false
Runtime:removeEventListener( "enterFrame", enterFrameListener )
display.getCurrentStage():setFocus( nil )
event.target.isFocus = false
end
end
return true
end
I believe its obvious what touchHandler function is ^^
You will need a touch listener added to either an object covering your whole screen or you can add it to the system's RunTime.
See this guide: http://docs.coronalabs.com/guide/events/detectEvents/index.html#hit
Now, there are three "Phases" for these touch events. You get one when the press starts ("began"), one if the person moves their finger ("moved") and when they stop touching, there is an "ended" phase.
Depending on what you are trying to do, if you are say moving something while holding the button down, then you can set a flag like:
if event.phase == "began" then
pressed = true
elseif event.phase == "ended" then
pressed = false
end
Then where ever you're moving you can check to see "if pressed then move".