Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Moving objects using assigned values?
Started by leofranzinribeiro Sep 28 2019 04:33 PM

- - - - -
6 replies to this topic
lua api help
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

leofranzinribeiro

[GLOBAL: userInfoPane.html]
leofranzinribeiro
  • Observer

  • 8 posts
  • Corona SDK

I want to set up a game where two characters rotate between 8 static spots whenever a function is called. So if character one is at position one, he moves to position number two, if he's at two he goes to three, and so on until the eigth position where it loops. I would assign each tile a number (1-8) and the two players would also have a number matching with the tile they are standing on.

 

My question is if there is a command that allows me to compare the value of a character and the tiles and act accordingly. For example: Player 1 is at position number 5 and therefore has the value 5, when the function is run, his value goes up to six and therfore transitions to position number 6. 

 

I would guess there is a way to check individual numbers for a match between two variables (that is, compare the player's position number to the tile's number) but I don't know how it would be done.

 

Thanks in advance for any help I can get.



[TOPIC: post.html]
#2

sporkfin

[GLOBAL: userInfoPane.html]
sporkfin
  • Contributor

  • 596 posts
  • Corona SDK

This will give you a solid start.  It uses a position table to coordinate the placement of tiles and movement of objects

 


local cx, cy = display.contentCenterX, display.contentCenterY
local position = {}
position[1]  = {x = cx, y = cy - 40}
position[2]  = {x = cx + 40, y = cy}
position[3]  = {x = cx, y = cy + 40}
position[4]  = {x = cx - 40, y = cy}
 
-- make tiles
local tile1 = display.newRect( position[1].x, position[1].y, 30,30 ) ; tile1.alpha = 0.4
tile1:setFillColor( 0.5, 1, 0.5 )
 
local tile2 = display.newRect( position[2].x, position[2].y, 30,30 ) ; tile2.alpha = 0.4
tile2:setFillColor( 0.5, 1, 0.5 )
 
local tile3 = display.newRect( position[3].x, position[3].y, 30,30 ) ; tile3.alpha = 0.4
tile3:setFillColor( 0.5, 1, 0.5 )
 
local tile4 = display.newRect( position[4].x, position[4].y, 30,30 ) ; tile4.alpha = 0.4
tile4:setFillColor( 0.5, 1, 0.5 )
 
-- create objects
local object1 = display.newCircle( position[1].x, position[1].y, 10 )
object1:setFillColor( 1, 0.5, 0.5 )
object1.pos = 1
 
local object2 = display.newCircle( position[3].x, position[3].y, 10 )
object2:setFillColor( 0.5, 0.5, 1)
object2.pos = 3
 
local function moveObject(o)
    if o.pos == 4 then 
        o.pos = 1
    else
        o.pos = o.pos + 1
    end
 
    transition.to( o, {time = 1000, x = position[o.pos].x, y = position[o.pos].y, transition = easing.outBack } )
end
 
local function objectsToMove()
    moveObject(object1)
    moveObject(object2)
end
 
local positionTimer = timer.performWithDelay( 2000, objectsToMove, 0)



[TOPIC: post.html]
#3

sporkfin

[GLOBAL: userInfoPane.html]
sporkfin
  • Contributor

  • 596 posts
  • Corona SDK

Next try adding more tiles and changing their positions by adjusting the position table.  After that, add more objects and be sure you also add them to the objectsToMove( ) function.  Have fun!



[TOPIC: post.html]
#4

leofranzinribeiro

[GLOBAL: userInfoPane.html]
leofranzinribeiro
  • Observer

  • 8 posts
  • Corona SDK

Next try adding more tiles and changing their positions by adjusting the position table.  After that, add more objects and be sure you also add them to the objectsToMove( ) function.  Have fun!

Ah I see what you did here, that's clever! I frankly was having a bit of trouble wrapping my head around tables and how they can be used. I'll mess around with this and add more stuff now, thanks man!



[TOPIC: post.html]
#5

sporkfin

[GLOBAL: userInfoPane.html]
sporkfin
  • Contributor

  • 596 posts
  • Corona SDK

You are very welcome!

 

Yes, tables can be very confusing at first since they can look like arrays but have much more flexibility - play around a little with the  table examples on lua.org

 

Basically, I used the key values [1], [2], [3] etc. so that you can call coordinates from the table by referencing the position number you assign to it.  There are many ways to construct tables and this method has its limitations so be wary of adding non-number keys to the table.



[TOPIC: post.html]
#6

sporkfin

[GLOBAL: userInfoPane.html]
sporkfin
  • Contributor

  • 596 posts
  • Corona SDK

This method will also work with physics if you set the linear velocity of the physics object in a direction toward the coordinates.  Of course you will have to work on the exact force to use and linear damping, and probably use counter force at some point - but it will work for physics too.



[TOPIC: post.html]
#7

leofranzinribeiro

[GLOBAL: userInfoPane.html]
leofranzinribeiro
  • Observer

  • 8 posts
  • Corona SDK

You are very welcome!

 

Yes, tables can be very confusing at first since they can look like arrays but have much more flexibility - play around a little with the  table examples on lua.org

 

Basically, I used the key values [1], [2], [3] etc. so that you can call coordinates from the table by referencing the position number you assign to it.  There are many ways to construct tables and this method has its limitations so be wary of adding non-number keys to the table.

Oh I will definitely check that out, thanks! It'll probably save me a lot of time to know how to properly use them.




[topic_controls]
[/topic_controls]

Also tagged with one or more of these keywords: lua, api, help