Dragging object in corona - coronasdk

I have an object that I just wanted to move up and down. How could I make that object still move up and down without actually touching the object? Here is my codes so far:
function Scientist:touch( event )
if event.phase == "began" then
display.getCurrentStage():setFocus( nil)
self.markY = self.y
self.isFocus = false
elseif event.phase == "moved" then
local y = (event.y - event.yStart) + self.markY
self.y = y
elseif event.phase == "ended" or event.phase == "cancelled" then
display.getCurrentStage():setFocus(nil)
end
return true
end
Scientist:addEventListener('touch', Scientist)

Just create a large rectangle, set the alpha to 0, and make the width display.contentWidth and the height display.contentHeight. When you touch that rectangle, have that move the Scientist in a similar way you are doing now.

Make a rectangle on entire page with alpha set to 0.01:
local background = display.newRect(display.contentWidth/2, display.contentHeight/2, display.contentWidth, display.contentHeight)
then write a touch listener like you did background:addEventListener('touch', background)
here is the complete code for you:
function Scientist:touch( event )
if event.phase == "began" then
display.getCurrentStage():setFocus( nil)
Scientist.markY = Scientist.y
self.isFocus = false
elseif event.phase == "moved" then
local y = (event.y - event.yStart) + Scientist.markY
Scientist.y = y
elseif event.phase == "ended" or event.phase == "cancelled" then
display.getCurrentStage():setFocus(nil)
end
return true
end
Scientist:addEventListener('touch', Scientist)

Related

Making a character running when holding a button while 2 seconds

I've a character moving right and left correctly. I want now the hero to run while the button is pressed during 2 seconds.
This is my actual code:
function LeftArrow(event)
motionx = -hero.speed
end
function RightArrow(event)
motionx = hero.speed
end
local function MoveHero (event)
hero.x = hero.x + motionx
end
Runtime:addEventListener("enterFrame", MoveHero)
-- Stop character movement when no arrow is pushed
local function StopHero (event)
if event.phase =="ended" then
motionx = 0
end
end
Runtime:addEventListener("touch", StopHero )
Any suggestion?
Okay I think i found the answer !
function GoFastLeft()
hero.speed=10
motionx = -hero.speed
end
function GoFastRight()
hero.speed=10
motionx = hero.speed
end
function LeftArrow(event)
if ( event.phase == "began" ) then
motionx = -hero.speed
FastTimer = timer.performWithDelay( 1000, GoFastLeft, 1 )
elseif ( event.phase == "ended" ) then
hero.speed=2
timer.cancel( FastTimer )
motionx = 0
end
return true
end
function RightArrow(event)
if ( event.phase == "began" ) then
motionx = hero.speed
FastTimer2 = timer.performWithDelay( 1000, GoFastRight, 1 )
elseif ( event.phase == "ended" ) then
hero.speed=2
timer.cancel( FastTimer2 )
motionx = 0
end
return true
end

Move a object by flicking

I can move the box by touching it and dragging it along the x-axis, but i like to be able to flick it from one side to the other. is there a simple solution to do this?
local box = display.newRect( 0, 0, 50, 50)
box:setFillColor( math.random(0,255), math.random(0,255), math.random(0,255) )
physics.addBody( box, { density=3.0, friction=0.5 } )
box.gravityScale = 0.0
function box:touch( event )
if event.phase == "began" then
self.markX = self.x
elseif event.phase == "moved" then
local x = (event.x - event.xStart) + self.markX
self.x = x
end
return true
end
box:addEventListener( "touch", box )
You could use transition.to, but you'll have to decide by how much to move. I'll assume you want to move it all the way to other side:
function box:touch( event )
if event.phase == "began" then
self.markX = self.x
elseif event.phase == "end" then
local targetX = 0 -- if flick left
if event.x > self.markX then -- flick right
targetX = display.contentWidth - self.width
end
local duration = 1000 -- 1 second
transition.to(self, {duration, x=targetX})
end
return true
end
Have not tested, could be syntax or other errors, but this should give you a good idea of how to proceed (if not put a comment).

Remove and then adding runtime event listener again in corona

what i am trying to achieve here is... if i touch one image it gets disabled. then i want to enable it by touching another image. but after disabling i can't re-enable it.
i have a touch event for first image which i call using
Runtime:addEventListener( "touch", diceFunction2 )
and i remove it using
Runtime:removeEventListener( "touch", diceFunction2 )
but i am trying to add it again using this touch event handler
local function guti11touched( event )
if event.phase == "began" then
if playerTurn%2==0 then
if rand == 6 and gutiStatus.guti11.status == false then
gutiStatus.guti11.status = true
gutiStatus.guti11.count = 1
local i = gutiStatus.guti11.count
transition.moveTo(guti11, {x=background.x+player1Sequence[i][1],y=background.y-player1Sequence[i][2]})
elseif gutiStatus.guti11.status == true and gutiStatus.guti11.count+rand <= 57 then
gutiStatus.guti11.count = gutiStatus.guti11.count + rand
local i = gutiStatus.guti11.count
transition.moveTo(guti11, {x=background.x+player1Sequence[i][1],y=background.y-player1Sequence[i][2]})
if i == 57 then
guti11.isClickable = false
end
end
end
elseif event.phase == "ended" then
Runtime:addEventListener( "touch", diceFunction2 )
end
return true
end
can someone please tell me what is the correct way to do it and what i'm doing wrong.
EDIT
this is my first image eventhandler. i already tried by adding dice:addEventListener( "touch", diceFunction2 ) , but in that case the image just keep rotating and never stop, and as i'm choosing images randomly so can i do it in place of runtime?
--for rotating dice
local function diceFunction2( event )
if event.phase == "began" then
local laserChannel = audio.play( laserSound )
rand=math.random(6)
dice = display.newImage("images/dice3droll.png")
dice.x = 75
dice.y = 480
dice:scale(1/scale_factor,1/scale_factor)
display.getCurrentStage():setFocus( dice )
dice.isFocus = true
Runtime:addEventListener( "enterFrame", rotateDice )
elseif dice.isFocus then
if event.phase == "moved" then
elseif event.phase == "ended" then
if rand == 1 then
Runtime:removeEventListener( "enterFrame", rotateDice)
dice:removeSelf()
local dice1 = display.newImage("images/ek.png")
dice1.x = 75
dice1.y = 480
dice1:addEventListener( "touch", diceFunction2 )
display.getCurrentStage():setFocus( nil )
dice.isFocus = false
elseif rand == 2 then
Runtime:removeEventListener( "enterFrame", rotateDice)
dice:removeSelf()
local dice2 = display.newImage("images/dui.png")
dice2.x = 75
dice2.y = 480
display.getCurrentStage():setFocus( nil )
dice.isFocus = false
elseif rand == 3 then
Runtime:removeEventListener( "enterFrame", rotateDice)
dice:removeSelf()
local dice3 = display.newImage("images/tin.png")
dice3.x = 75
dice3.y = 480
display.getCurrentStage():setFocus( nil )
dice.isFocus = false
elseif rand == 4 then
Runtime:removeEventListener( "enterFrame", rotateDice)
dice:removeSelf()
local dice4 = display.newImage("images/charr.png")
dice4.x = 75
dice4.y = 480
display.getCurrentStage():setFocus( nil )
dice.isFocus = false
elseif rand == 5 then
Runtime:removeEventListener( "enterFrame", rotateDice)
dice:removeSelf()
local dice5 = display.newImage("images/pach.png")
dice5.x = 75
dice5.y = 480
display.getCurrentStage():setFocus( nil )
dice.isFocus = false
elseif rand == 6 then
Runtime:removeEventListener( "enterFrame", rotateDice)
dice:removeSelf()
local dice6 = display.newImage("images/chokka.png")
dice6.x = 75
dice6.y = 480
display.getCurrentStage():setFocus( nil )
dice.isFocus = false
end
end
end
end
this is my rotatedice function
local function rotateDice()
dice.rotation = dice.rotation + 200
end
Runtime touch handlers cover the whole screen. If you want a touch event on an object, you normally add the touch event directly to that object:
someImage:addEventListener("touch", myTouchFunction)
Then I would not worry about adding and removing the listeners but instead set some flags that would indicate if the touch was allowed to happen or not.

how to do continuous action on corona in touch function?

function left:touch(e)
if(e.phase == "ended") then
boy:applyLinearImpulse(-0.1, .5, boy.x, boy.y)
end
end
function right:touch(e)
if(e.phase == "ended") then
print("right");
boy:applyLinearImpulse(0.1, .5, boy.x, boy.y)
end
end
left:addEventListener( "touch", left );
right:addEventListener( "touch" , right );
in my game I used applyLinearImpulse t0 give force to the handstand man. When I click right and left button to change x and y direction. How to increase different force for every touch?
Here is what you can do.
Save the initial x,y at start move event as ix and iy.
For every move event,
calculate the difference between ix and event.x and apply the difference dx.
Do the same for y-axis.
If touch event ended, nil the initial x,y ix and iy.
local function left:touch(event)
if event.phase == "began" then
--save the initial position of boy
boy.ix,boy.iy = event.x,event.y
elseif event.phase == "moved" then
if boy.ix and boy.iy then
--calculate the initial x,y with current event x,y difference
local dx = (event.x-boy.ix)*0.4
local dy = (event.y-boy.iy)*0.4
boy:applyLinearImpulse(dx,dy,boy.x,boy.y)
--boy:applyForce(dx,dy,boy.x,boy.y)
boy.ix,boy.iy = boy.x,boy.y
end
elseif event.phase == "ended" or event.phase == "cancelled" then
boy.ix,boy.iy = nil,nil
end
end

playing sprite Instance at the move phase of touch event

I want to move frames of sprite sheet with the drag event.The sprite Instance must play only when the user drags .If he lifts his hand it must get paused.Is there any way to do this?
require "sprite"
-- A sprite sheet with a green dude
local sheet = sprite.newSpriteSheet( "greenman.png", 128, 128 )
local spriteSet = sprite.newSpriteSet(sheet, 1, 15)
sprite.add( spriteSet, "man", 1, 15, 200, 0 ) -- play 15 frames every 200 ms
local instance = sprite.newSprite( spriteSet )
instance.x = display.contentWidth / 2
instance.y = display.contentHeight / 2
instance:prepare("man")
local function startDrag( event )
local t = event.target
local phase = event.phase
if "began" == phase then
display.getCurrentStage():setFocus( t )
t.isFocus = true
-- Store initial position
t.x0 = event.x - t.x
t.y0 = event.y - t.y
elseif t.isFocus then
if "moved" == phase then
t.x = event.x - t.x0
t.y = event.y - t.y0
t:play()
elseif "ended" == phase or "cancelled" == phase then
display.getCurrentStage():setFocus( nil )
t.isFocus = false
t:pause()
end
end
-- Stop further propagation of touch event!
return true
end
instance:addEventListener( "touch", startDrag )

Resources