Moving objects without using physics?

Hi, what’s the best way to go about moving objects without using physics? (which api to use, best practices, etc.)

For example, tetris blocks just fall to the bottom of the screen at certain speed unless user interacts with it. 

Hi Andrew,

The easiest way would be to use transitions:

http://docs.coronalabs.com/api/library/transition/index.html

Alternatively, you could use a Runtime “enterFrame” function to update the position of these objects on each game cycle. Personally, I think the transition method is easier, but both options are available to you without using physics.

Regards,

Brent

Hi Brent, 

Thanks for the quick reply.

Yeah, I thought about using transition.to but how can you know if a tetris block has landed on the bottom of the screen? 

Is there a good way to check it using transition.to ? 

Hi Andrew,

Well, the best way would probably be to just use an “onComplete” call when the transition finishes. Just specify that as one of the parameters in the options table of the transition, and supply the function that you want to call when it completes…

[lua]

local myFunction( event )

   print( “brick with ID “…event…” reached the bottom!” )

end

transition.to( brick1, { time=20000, y=600, onComplete=myFunction } )

[/lua]

Also, note that in the function called on the “onComplete”, the “event” argument provided to you represents the actual Lua object ID, which is really useful to know so you can perform whatever actions upon that object necessary (remove it, do another transition on it, or whatever you want).

Hope that helps,

Brent

Thanks, Brent.  :smiley:

Hi Andrew,

The easiest way would be to use transitions:

http://docs.coronalabs.com/api/library/transition/index.html

Alternatively, you could use a Runtime “enterFrame” function to update the position of these objects on each game cycle. Personally, I think the transition method is easier, but both options are available to you without using physics.

Regards,

Brent

Hi Brent, 

Thanks for the quick reply.

Yeah, I thought about using transition.to but how can you know if a tetris block has landed on the bottom of the screen? 

Is there a good way to check it using transition.to ? 

Hi Andrew,

Well, the best way would probably be to just use an “onComplete” call when the transition finishes. Just specify that as one of the parameters in the options table of the transition, and supply the function that you want to call when it completes…

[lua]

local myFunction( event )

   print( “brick with ID “…event…” reached the bottom!” )

end

transition.to( brick1, { time=20000, y=600, onComplete=myFunction } )

[/lua]

Also, note that in the function called on the “onComplete”, the “event” argument provided to you represents the actual Lua object ID, which is really useful to know so you can perform whatever actions upon that object necessary (remove it, do another transition on it, or whatever you want).

Hope that helps,

Brent

Thanks, Brent.  :smiley:

Instead of starting a new post, I’ll just ask here…

I have a runtime event that positions my image on the screen. How would I animate it from say point A to Point B over a period of 3 seconds using the runtime event?

It would be easier to use a transition.to() call and not do it with a Runtime enterFrame listener, but . . .

You would need to know the distance of A to B.  Lets say A.x is 100 and B.x is 400.  The distance is 300.

d = B.x - A.x – i.e 300

t = 3 – seconds

frames = t * 30 – 30 fps, if your a 60fps app change it to 60

distancePerFrame = d / frames

A.x = A.x + distancePerFrame

or something like that.

Thanks I will try it out. And just for the record, I have tried transition.to method but I cant get it to work for some reason. I dont get an error, I just get no animation…Strange.

I may be putting the transition.to statement in the wrong place, but I have tried moving it around in different parts of my code and still get no animation. Like I said, no error but no animation either.

Without seeing some code, it’s hard to tell what is going on.  I can tell you with 100% certainty that transition.to works and works well in the build you are using (I’m assuming you are using 1202).  We just updated the transitions library in the daily builds and are ironing out a couple of backwards compatible issues.

Post some code where your  transition.to’s are not now working.

Okay, well here’s what I’m working with. I DID get things working with a runtime add event listener, but it doesnt work right. It actually gains speed over time, which is not what I was hoping would happen. See below.

local fNames = {"images/fg1.png","images/fg2.png","images/fg3.png"} local foreground = nil math.randomseed(os.time()) local function ChangeForeground() if foreground then foreground:removeSelf() foreground = nil end local imageNameFG = fNames[math.random(1, #fNames)] foreground = display.newImage(imageNameFG) foreground.isVisible = true foreground:setReferencePoint ( display.CenterReferencePoint ) foreground.xScale = .325 foreground.yScale = .325 foreground.x = 420 -- could also use display.contentWidth foreground.y = 500 -- could also use display.contentHeight local function animateFG (event) foreground.y = foreground.y -0.1 foreground.x = foreground.x -0.1 end Runtime:addEventListener ("enterFrame",animateFG) end ChangeForeground() timer.performWithDelay(10000, ChangeForeground, -1) --end FOREGROUND ; begin next -----------------

The part that says “foreground.y = foreground.y -0.1
foreground.x = foreground.x -0.1”  at lines 23, etc., just above the runtime add event listener works okay in the beginning but over time it keeps speeding up, which I dont want…That’s the only problem, otherwise it DOES work…

I would rather use a transition.to, but like I said it just doesn’t want to work for me… What I’m doing wrong concerning the transition.to, I just have no clue…

Can you post the code where you’re trying to use transition.to?

I have tried using it all the place in that code, if I dont get an error, I get no animation. So the transition.to statement either generates an error or it just does nothing at all…But I have placed it all over, and also tried changing the table/function call that I put inside the transition.to statement…all to no avail.

I don’t know what you’re trying to move or where the best place to do it is, but the core transition.to is:

local z = display.newRect(0,0,100,100)
transition.to(z, { time=1000,  x=200})

Can you try that and make sure it works for you?

Im trying to move the random display objects towards the top of the code (fg1, fg2, etc.).

I was using transition.to { z, time=500, x=0, y=0}

Maybe I was typing the statement wrong? I’ll give yours a try.

Nope. I get loads of errors when I try to place it anywhere. I have tried substituting “z”, for fNames, foreground, and a few others to no avail… If you want to see the error just ask I will post it but I dont see how it would help me at this point…

we really need to see the errors…  and the code.