How to make an object move into position by clicking once without transition to.

I am very new to corona but not new to coding as such. (I used to use basic and many years ago Delphi.)

I am struggling just trying to do the following.

I have two objects/images on screen. I am trying to do the following. I want to click and hold on one of the objects and for as long as I am touching the screen, the second object will move towards the first object and stop when I stop touching the screen or the xy co-ordinates of both are the same.

What is currently happening is that The code is working within my function but only by a single step at a time. I have to keep touching then lift of then repeat etc etc.

Nothing I have tried works.

Any help please?

Cheers

Mike R [import]uid: 9950 topic_id: 3475 reply_id: 303475[/import]

I wanted to add that I do not want to use transition to as I want more direct control over speed etc of the item.

Cheers

Mike R [import]uid: 9950 topic_id: 3475 reply_id: 10439[/import]

set a flag on touch down eg isTouched = true, then reset it on touch up, isTouched = false

then in your Runtime enterframe for instance check for isTouched true and move accordingly [import]uid: 6645 topic_id: 3475 reply_id: 10440[/import]

You can check for a stationary touch, as below. I’ve always used “began” or “ended” but it’s in the API:
http://developer.anscamobile.com/content/events-and-listeners

So maybe something like this:

[lua]local function backgoundTouch( event )
local t = event.target
local phase = event.phase

if “stationary” == phase then
display.getCurrentStage():setFocus( t )
t.isFocus = true

– set flag to move object in moveObjects function
end
end

local function moveObjects( event )

– move your object

end

–add listeners
backgroundImage:addEventListener( “touch”, backgoundTouch )
Runtime:addEventListener( “enterFrame”, moveObjects )[/lua] [import]uid: 1560 topic_id: 3475 reply_id: 10444[/import]

I guess I need more sleep as my brain is not taking this in. The joys of having two kids under 20 months old in the house doesn’t help. :wink:

This is what I have. Perhaps someone can show me where I am going wrong. Its very simple and I can get it to work in basic but no joy in corona.

Cheers

Mike R

display.setStatusBar( display.HiddenStatusBar )
local friction = 0.5
local speed = 0
local accelerate = 0.05

local physics = require( “physics” )
physics.start()
physics.setGravity( 0, 0 )
local sky = display.newImage( “bkg_clouds.png” )
sky.x = 160; sky.y = 195

local runner = display.newImage( “Runner.png” )
runner.x = 200; runner.y = 450;

local man = display.newImage( “Man.png” )
man.x = 200; man.y = 66;
local listener = {}

local function listener(event)
speed = speed + accelerate
if speed > 3 then speed = 3
if runner.x > man.x then
runner.x = runner.x - speed
elseif runner.x < man.x then
runner.x = runner.x + speed
if runner.y > man.y then
runner.y = runner.y - speed
elseif runner.y < man.y then
runner.y = runner.y + speed
end
end
end
end

local listenerMan = {}

function listenerMan:touch(event)
transition.to ( man, { time=500, x=(event.x), y=(event.y) } )
end

Runtime:addEventListener( “touch”, listener )

Runtime:addEventListener( “touch”, listenerMan )

[import]uid: 9950 topic_id: 3475 reply_id: 10488[/import]

Sorry I meant to get rid of the physics bits as in this experiment its not used.

Mike [import]uid: 9950 topic_id: 3475 reply_id: 10489[/import]

This works…and it turns out that “stationary” does not work as an event phase. You’ll need to supply your own background.png
[lua]local onScreenGroup = display.newGroup()
local s = display.newImage(“background.png”)
onScreenGroup:insert(s)

local myRectangle = display.newRect(0, 0, 150, 50)
myRectangle.strokeWidth = 3
myRectangle:setFillColor(140, 140, 140)
myRectangle:setStrokeColor(180, 180, 180)

myRectangle.x = 50
myRectangle.y = 50

local function moveObjects( xx, yy )

transition.to ( myRectangle, { time=2000, x=xx, y=yy } )

return true

end

local function backgoundTouch( event )

local t = event.target
local phase = event.phase

if “began” == phase then
display.getCurrentStage():setFocus( t )
t.isFocus = true

moveObjects(event.x, event.y)

end

if “ended” == phase then
print(“Ended”)
end
end

–add listeners
onScreenGroup:addEventListener( “touch”, backgoundTouch )[/lua] [import]uid: 1560 topic_id: 3475 reply_id: 10494[/import]

Sorry, Perhaps I was not clear on what I was having an issue with. The transition code works for the first character. Its the other character that does not work. the one where I am NOT using the transition to code.

I actually don’t want to use transition to as it does not do what I would need it to do. Basically I have in very simple (certainly not final) code that if I touch the man character the runner character will move towards the man at an ever increasing speed based on speed and acceleration. If the speed is greater than 3 then keep the speed of that character at 3. So the runner character will accelerate towards the man character. If I can get that to work I will then have it slow down over a distance when the touch is no longer happening.

As I said before it works but only if I click and release then repeat until the runner reaches the man.

Its a pain in the butt and if I cant do anything as simple as this in Corona then I may have made a mistake. I can do this and much more complex things in basic in about ten minutes so am totally flabbergasted that I can’t work this out at all.

By the way this is far and away from what I have tried. Its just the latest attempt to get an object to accelerate towards another object while a touch event is happening without having to use transition to.

Cheers

Mike R

This part.
local listener = {}

local function listener(event)
speed = speed + accelerate
if speed > 3 then speed = 3
if runner.x > man.x then
runner.x = runner.x - speed
elseif runner.x < man.x then
runner.x = runner.x + speed
if runner.y > man.y then
runner.y = runner.y - speed
elseif runner.y < man.y then
runner.y = runner.y + speed
end
end
end
end

Runtime:addEventListener( “touch”, listener ) [import]uid: 9950 topic_id: 3475 reply_id: 10501[/import]

This is ugly but I think it gets to what you’re asking about. You need to use an enterFrame event listener to move the object repeatedly.

Run this and when you click on the simulator screen, the rectangle will move slowly to where you clicked.

[lua]local myRectangle = display.newRect(0, 0, 10, 10)
myRectangle.strokeWidth = 3
myRectangle:setFillColor(140, 140, 140)
myRectangle:setStrokeColor(180, 180, 180)

myRectangle.x = 50
myRectangle.y = 50

local xdist = 0
local ydist = 0
local tx = 0
local ty = 0
local speed = 1

local function moveObjects(event)
if tx > 0 then

myRectangle.x = myRectangle.x + ((tx - myRectangle.x)/(xdist))*speed
xdist = math.abs (myRectangle.x - tx)
if myRectangle.x == tx then
– need to adjust this check for different speeds
tx = 0
end
end

if ty > 0 then
myRectangle.y = myRectangle.y + ((ty - myRectangle.y)/(ydist))*speed
ydist = math.abs (myRectangle.y - ty)
if myRectangle.y == ty then
– need to adjust this check for different speeds
ty = 0
end

end

return true

end

local function backgroundTouch( event )

local phase = event.phase

if “began” == phase then
print (“Began”)
tx = event.x
ty = event.y
xdist = math.abs (myRectangle.x - tx)
ydist = math.abs (myRectangle.y - ty)
end

if “ended” == phase then
print(“Ended”)

end
end

–add listeners
Runtime:addEventListener( “touch”, backgroundTouch )
Runtime:addEventListener (“enterFrame”, moveObjects )[/lua] [import]uid: 1560 topic_id: 3475 reply_id: 10563[/import]

Thank you for that. Its easy when you know how. Finally I am a step further on and with both yourself and Walters help I understand that side of things now.

Thank you

Mike R [import]uid: 9950 topic_id: 3475 reply_id: 10571[/import]

Hi…

I hv one object that move from middle top to bottom and in between i want to stop transition on that touch listener of object so how can i stop that transition and object stop at that location.

all answer is appreciate.
Thankx
Krunal [import]uid: 33757 topic_id: 3475 reply_id: 24883[/import]

Aehm… Your title mentions without transition and then you ask how to stop a transition???

For the later you store the tween of your transition.to call and cancel it when needed with transition.cancel(yourTween). [import]uid: 5712 topic_id: 3475 reply_id: 24886[/import]

Hi Mike,

I am new in Lua, can you please one things for me.

I want to draw a line when i have move from the starting point to ending point on our background, i have go with polyine but not getting success and how to do that.
Thankx
Krunal [import]uid: 33757 topic_id: 3475 reply_id: 25079[/import]

Check out the pool example. It shows how to draw a line/line object dynamically and in certain directions. [import]uid: 5712 topic_id: 3475 reply_id: 25256[/import]

Hi Mike,

I have go with this example but its give me straight line but i want to draw line as per my movement of pointer…

is there any way…
Thankx
Krunal [import]uid: 33757 topic_id: 3475 reply_id: 25456[/import]

I’m pretty new to this too, but it seems to me that you’re declaring

local listener = {}

which is creating an empty table, then turning around and creating a function

local function listener()

I wonder if that’s creating problems for you?

Also your listener function isn’t checking for what touch event happened. It may not mean that much in your case, but its going to fire when you let up, when you touch and drag, etc.

I’m new I might be wrong about it. [import]uid: 19626 topic_id: 3475 reply_id: 27482[/import]