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.
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
I would like to build a fuel bar (from full tank to empty). When the user touch the screen, the fuel bar dicrease.
Here is my code :
lifeBar = display.newImage("fuel_bar1.png")
lifeBar.anchorX=0
lifeBar.anchorY=0.6
lifeBar.x = fuel_title.x +114+13.5
lifeBar.y = 37
screenGroup:insert(lifeBar)
Then, i used a function called FuelConsumption() to reduce the fuel tank each time the variable "pressed" is equal to "true". When Pressed==true, it means that the user is touching the screen :
function FuelConsumption()
if lives > 0 and pressed==true then
lives = lives - 1
lifeBar.width=lifeBar.width-1
livesValue.text = string.format("%d", lives)
end
end
This function is active on the enterFrame event as follow :
Runtime:addEventListener( "enterFrame", FuelConsumption )
It works very well, but my question is : How is possible to reduce the lifebar width each 5 secondes (once the variable pressed==true) ?
I tried to add this to my touch event function :
function flyUp(event)
if event.phase == "began" then
pressed=true
if gameStarted == false then
gameStarted = true
pauseBtn.isVisible=true
opt_btn.isVisible=true
Runtime:addEventListener("enterFrame", enterFrameListener)
Runtime:addEventListener("enterFrame",scrollGrasses)
timer.performWithDelay( 5000, function() Runtime:addEventListener( "enterFrame", FuelConsumption ) end,-1 )
end
elseif event.phase == "ended" then
pressed = false
timer.performWithDelay( 1000, function() Runtime:removeEventListener( "enterFrame", FuelConsumption ) end,-1 )
end
end
The problem is that code is consumming a lot of memory and the game is going so slow ! So, is there any other way to do ?
Thank you :)
Instead of using a single image use a sprite sheet.That might boost up the game.
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".
How can I detect if screen is being touched? Seems to me, touch events are not generated while screen is touched and the finger is not moving.
Yes, only changes to the finger movement is recorded. Put down finger, lift up finger and dragging around triggers event.
However, you could do
e.phase == "began"
in your event function. That would be triggered when the user puts his finger on the screen.
Touch events are handled in phases. So the event being generated by the touch has "began", "moved", "ended" and "cancelled" phases. You could use detection, therefore, by doing this:
self.isTouched = false;
function defaultTouchHandler(e)
if(e.phase == "began") then
print("Tapped")
self.isTouched = true;
--User has touched the screen (not moving). Do "onMouseDown" things here
elseif(e.phase == "moved") then
print("Moved")
--User is moving their finger wile touching. Do "onMouseMoved" things here
elseif(e.phase == "cancelled" or e.phase == "ended") then
print("End of touch")
self.isTouched = false;
--User lifted their finger, or an interrupt happened. Do "onMouseUp" things here
end
end
self:addEventListener("touch", defaultTouchHandler)
When you then need to check if the screen is being touched, simply do:
if(isTouched) then
--Screen is being touched
else
--Screen is not being touched
end
EDIT: Obviously you can change "self" on the addEventListener line to be any object you wish to listen for touch events on
local object = display.newImage( "ball.png" )
object.id = "ball object"
local function onObjectTouch( event )
if ( event.phase == "began" ) then
print( "Touch event began on: " .. event.target.id )
elseif ( event.phase == "ended" ) then
print( "Touch event ended on: " .. event.target.id )
end
return true
end
object:addEventListener( "touch", onObjectTouch )