Game Dev Nation
  • Twitter
  • RSS
Game Dev Tools, Tips, and Tutorials
  • Home
    • Indie Game Dev and Scrum
    • Robotronish – A Mobile Game Inspired by Robotron 2084
    • About
    • Blog
    • Contact
  • Featured
  • Solar2D Tutorials
  • GDN Membership
    • Members
      • Audio Editing Secrets
      • Programming Profits
  • Unity 2D Game Dev
    • Pixelnest Tutorial
Search the site...

Transition.to() Based on Distance, Not Time

February 2, 2014 / Jay / Lua, Solar2D Tutorials
0

[Updated Jul 30, 2020]

Using transition.to() for animation is one of the most awesome things about Solar2D. It makes simple animation quick and easy. However, it’s based on time — move the object from Point A to Point B over a specified amount of time. Which means if you want to move something different distance at the same speed, it’s not automatic.

But it’s also not rocket science.

Here’s a sample project that shows how it’s done. Create a main.lua file and paste in the following code. Then run it in the simulator and you’ll see a dot show on the screen. Tap anywhere else on  the screen and the dot will move to that location. Tap close and tap far — the dot will always move at the same speed, no matter how far away you tap.

Lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
local player
local mSqrt = math.sqrt
 
local function distanceBetween( pos1, pos2 )
local factor = { x = pos2.x - pos1.x, y = pos2.y - pos1.y }
return mSqrt( ( factor.x * factor.x ) + ( factor.y * factor.y ) )
end
 
local function movePlayer(event)
transition.cancel ( player.trans )
local totDist = distanceBetween(player, event)
local travTime = totDist / player.speed
player.trans = transition.to ( player, {time=travTime, x=event.x, y=event.y} )
end
 
player = display.newCircle( 50, 50, 15 )
player.speed = .1 -- bigger numbers are faster
 
Runtime:addEventListener("tap", movePlayer)

The secret is the distanceBetween() function and two lines inside the movePlayer() function. To determine the time it should take to go from point A to Point B we need to know how far between the two points.

The distance between() function takes two parameters — tables that contain x and y properties. Which means you can pass in a display object since it’s (under the hood) a Lua table and all objects have x and y properties. Your second parameter can be the event record that’s passed to a listener from the Runtime listener — event records are also Lua tables and they have x and y properties that tell where the tap took place.

So inside movePlayer() we call distanceBetween() and pass the current location of the dot on the screen as well as the location of the tap. It passes back the distance between the two in pixels (not technically pixels, but it’s okay to think of them that way) and then all it takes to get the time we need is to divide the distance by our player speed.

Lua
1
local travTime = totDist / player.speed

The player.speed is set to .1 right after we create the player (dot) and by choosing a larger number we can make the dot move faster.

Now we have a variable called travTime that can be used for the time parameter in transition.to() and our player dot will always move the same speed, no matter whether it’s going 10 pixels or 500.

Previously published referencing Corona SDK, the old name of Solar2D.

Please share:

  • Share
  • Tweet
  • Email

Like this:

Like Loading...

Leave a Reply Cancel reply

Solar2D Game Course Bundle

Unity FPS Creator Bundle Asset

Get More Game Dev Info!

Recent Features

  • Murder and Mayhem with Solar2D

    August 4, 2020
  • Solar2D Composer Book Online

    July 29, 2020
  • How to Become a Part-Time Indie Game Developer

    September 7, 2016
  • Firing Up A “Tired” Game Type

    August 5, 2016
  • Birth of a New Game Dev Video Course

    July 31, 2016
  • My Favorite Sites for 2D Art

    November 6, 2015

Site Sponsors

Ad Square WordPress Arcade Plugin

Lo Shu

Ad Square

Categories

  • Adventure Game Machine
  • Articles
  • Blog
  • Featured
  • Game Design
  • Game Dev Online
  • Game Development
  • Game Tools
  • General
  • Horse Crazy
  • HTML5
  • Indie Game Dev and Scrum
  • Latest News (Members)
  • Lua
  • Members Only
  • Moai
  • Robotronish
  • Solar2D
  • Solar2D Tutorials
  • Stencyl
  • Unity
  • Unity 2D
  • Video Tutorials
  • Zombies Ate My Poodle

Recommended

Game Development Stuff

While GDN started as a tutorial site for Corona SDK (now called Solar2D), it kind of morphed into a general game dev site. It’s focused mainly on news of interest to game developers. New tools, tutorials, industry shifts, etc. But the site also contains tutorials for Solar2D and Unity 2D game developers.

Recent Posts

  • #Devtober 2020 – Day 11
  • #Devtober 2020 – Day 1
  • Head to the Playground
  • Release Solar2D 2020.3611
  • Why Solar2D for Game Dev?
  • Murder and Mayhem with Solar2D
  • Solar2D Composer Book Online

Recommended Sites

  • Solar2D Game Dev Framework
  • Solar2D Video Tutorial Courses
Copyright 2014-2020 Three Ring Ranch

Disclosure

This site contains affiliate links, which means we may receive a commission if you click a link and purchase something that we have recommended. While clicking these links won’t cost you any money, they will help me fund my development projects while recommending great game dev assets!

Jetpack has locked your site's login page.

Jetpack has locked your site's login page.

Your IP address 192.42.116.52 has been flagged for potential security violations. You can unlock your login by sending yourself a special link via email. Learn More

Get help unlocking your site