how to do continuous action on corona in touch function? - lua

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

Related

How to stop an object from moving past a point

I was wondering if there was a way to stop an object from moving off the screen. In my code, I have a controllable character (player) and I want to prevent it from moving to the left when it's x coordinate is < 1
I have tried to do this in the code below, however, the player will not be stopped if you hold down the left arrow key.
Is there a way to fix this? If I had a guess, I would somehow need my program to continuously check for the case where player.x < 1
motionx = 0; -- Variable used to move character along x axis
speed = 10; -- Set Walking Speed
local function moveplayer (event)
player.x = player.x + motionx;
end
Runtime:addEventListener("enterFrame", moveplayer)
local function onKeyEvent( event )
if ( event.phase == "down" ) then
if ( event.keyName == "left" ) and player.x > 1 then
motionx = -speed
return true
elseif ( event.keyName == "right" ) then
motionx = speed
return true
end
end
end
Runtime:addEventListener( "key", onKeyEvent )
You need to check the boundaries and set the motionx to zero:
local player = display.newCircle(display.contentCenterX, display.contentCenterY, 10)
local motionx = 0; -- Variable used to move character along x axis
local speed = 10; -- Set Walking Speed
local function moveplayer( event )
player.x = player.x + motionx;
if player.x < 0 then
player.x = 0
motionx = 0
elseif player.x > display.contentWidth then
player.x = display.contentWidth
motionx = 0
end
end
Runtime:addEventListener( "enterFrame", moveplayer )
local function onKeyEvent( event )
if event.phase == "down" then
if event.keyName == "left" then
motionx = -speed
return true
elseif event.keyName == "right" then
motionx = speed
return true
end
end
end
Runtime:addEventListener( "key", onKeyEvent )
Simply implement a function that checks your objects position vs your screen boundaries. Whenever it reaches the border of your screen you stop it.
To check your objects position for every frame add your function as an event listener for the event "enterFrame".
Read this for details:
https://docs.coronalabs.com/guide/events/detectEvents/index.html
Of course you can also use the collision engine by placing invisible walls around your screen so your object will bounce back whenever it hits the border.
You were fairly close but may need your whole code to test it. I only changed this player.x < 1 and moved )
motionx = 0; -- Variable used to move character along x axis
speed = 10; -- Set Walking Speed
local function moveplayer (event)
player.x = player.x + motionx;
end
Runtime:addEventListener("enterFrame", moveplayer)
local function onKeyEvent( event )
if ( event.phase == "down" ) then
if ( event.keyName == "left" and player.x < 1) then
motionx = -speed
return true
elseif ( event.keyName == "right" ) then
motionx = speed
return true
end
end
end
Runtime:addEventListener( "key", onKeyEvent )

Dragging object in corona

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)

Cannot get functions to work in Corona SDK (With TabButton)

I am completely new to Corona SDK and I am just looking at the example projects just to see how things work. I am looking at the TAB example however I have a problem.
I have a page like so (this is page2)
local composer = require( "composer" )
local scene = composer.newScene()
function scene:create( event )
local sceneGroup = self.view
-- Called when the scene's view does not exist.
--
-- INSERT code here to initialize the scene
-- e.g. add display objects to 'sceneGroup', add touch listeners, etc.
-- create a white background to fill screen (things go in here like pictures etc)
local bg = display.newRect( 0, 0, display.contentWidth, display.contentHeight )
bg.anchorX = 0
bg.anchorY = 0
bg:setFillColor( 0 ) -- white
-- this will create the thing that you drag (the function is after)
local tracker = display.newRect( 568, 340, 50, 50 )
tracker:setFillColor( 1 )
-- all objects must be added to group (e.g. self.view)
sceneGroup:insert( bg )
sceneGroup:insert( tracker )
end
function scene:show( event )
local sceneGroup = self.view
local phase = event.phase
if phase == "will" then
-- Called when the scene is still off screen and is about to move on screen
elseif phase == "did" then
-- Called when the scene is now on screen
--
-- INSERT code here to make the scene come alive
-- e.g. start timers, begin animation, play audio, etc.
end
end
function scene:hide( event )
local sceneGroup = self.view
local phase = event.phase
if event.phase == "will" then
-- Called when the scene is on screen and is about to move off screen
--
-- INSERT code here to pause the scene
-- e.g. stop timers, stop animation, unload sounds, etc.)
elseif phase == "did" then
-- Called when the scene is now off screen
end
end
function scene:destroy( event )
local sceneGroup = self.view
-- Called prior to the removal of scene's "view" (sceneGroup)
--
-- INSERT code here to cleanup the scene
-- e.g. remove display objects, remove touch listeners, save state, etc.
end
function tracker:touch( event )
if event.phase == "began" then
self.markX = self.x --stores x location
self.markY = self.y --stores y location
elseif event.phase == "moved" then
local x = (event.x - event.xStart) + self.markX
local y = (event.y - event.yStart) + self.markY
self.x, self.y = x, y -- moves the object from things above
end
end
---------------------------------------------------------------------------------
-- Listener setup
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )
tracker:addEventListneer( "touch", tracker)
-----------------------------------------------------------------------------------------
return scene
Anyway to simplify things the changes I have done are:
local tracker = display.newRect( 568, 340, 50, 50 )
tracker:setFillColor( 1 )
This will create a new box which I am trying to make so you can drag it around the screen (I have used this function):
function tracker:touch( event )
if event.phase == "began" then
self.markX = self.x --stores x location
self.markY = self.y --stores y location
elseif event.phase == "moved" then
local x = (event.x - event.xStart) + self.markX
local y = (event.y - event.yStart) + self.markY
self.x, self.y = x, y -- moves the object from things above
end
end
So overall it creates a box and I am trying to add a function to it so that you can drag it around the screen. However it does not work and gives me an error saying that tracker on that start of the function line function tracker:touch( event ) is wrong? Any help, because I think that this is in the wrong place.
P.S I also have a tracker:addEventListneer( "touch", tracker) listener.
-Thanks
You are creating the local tracker as a local variable inside your scene:create function. This means this variable will only be available within the scope of said function.
You need to move the function tracker:touch(event) inside the scene:create function for the tracker to be reachable.
And you need to move the listener to the bottom of the scene:create function.
Never mind. I have fixed it. I should always check my spelling xD (I spelled the tracker:addEventListener( "touch", tracker) wrong). Such a noob mistake. Thanks for the help guys.

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).

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