If you’d prefer, feel free to email and/or skype me sometime.
Tdg_Photography@yahoo.com
Skype User-Name: Saerothir [import]uid: 148623 topic_id: 35544 reply_id: 142710[/import]
I chose Tiled because a) it’s simpler to use, b) we don’t need it to be extremely complicated, and c) it can do things extremely exactly.
So to start with, build a Tiled map. The tileset doesn’t really matter. It could even just be two images - a wall and your character.
Then, in your code, do an iteration so that if the data is 1 build a wall at that position, if the data is 2 build the character at that position.
There’s a simple framework.
I’ll build some code if you want me to 
Caleb [import]uid: 147322 topic_id: 35544 reply_id: 142921[/import]
Any thoughts on how to get it to work the way I want?
-Saer [import]uid: 148623 topic_id: 35544 reply_id: 142148[/import]
Just hang on for a while - I’m trying to code it out.
C [import]uid: 147322 topic_id: 35544 reply_id: 142158[/import]
No worries Caleb, didn’t mean to rush you. 
@ John - Thanks for the suggestion.
I’ve actually looked into bezier curves before, however I stopped looking into it because I didn’t like the idea of “drawing” my path. I want a very specific path and as stated above, there are only right angles in my game so I thought “drawing” my path would make the ‘drag’ motion of my user object look… wrong.
I’ll definitely take another look at the docs in the link/s and try what you suggested.
-Saer [import]uid: 148623 topic_id: 35544 reply_id: 142165[/import]
You weren’t rushing me 
So here it is - unoptimized code to clamp something to a grid. Of course, you’d need to refine it, etc, but here’s the shell:
[lua]
local square – Declare the grid table at the top
local function clamp(t, l, h) – Clamps a value to range (l-h)
if t return l
elseif t>h then
return h
else
return t
end
end
local function spaceToGrid(x,y,size) – Changes an (x, y) position to a grid’s (x, y) position
return math.ceil((x==0 and 1 or x)/size), math.ceil((y==0 and 1 or y)/size)
end
square={} – Table to hold grid
local squareGradient=graphics.newGradient( – Just to make things prettier
{255, 255, 0},
{255, 0, 0}
)
for w=1, 10 do
for h=1, 10 do
square[w…"."…h]=display.newRect(0, 0, 50, 50, 16) – Populate the grid with squares
square[w…"."…h].x, square[w…"."…h].y=w60, h60
square[w…"."…h].num=w*h
square[w…"."…h]:setFillColor(squareGradient)
end
end
local prevX, prevY=0, 0
local ball=display.newCircle(0, 0, 20) – Create the ball itself
ball.x, ball.y=square[“1.1”].x, square[“1.1”].y
ball:setFillColor(255, 255, 0)
ball.strokeWidth=4
ball:setStrokeColor(255, 0, 0)
ball.position=square[“1.1”]
ball.constraint=“X"
ball.hasPosition=true
local function touchBall(event)
if “began”==event.phase then
display.getCurrentStage():setFocus(ball) – Set focus to ball
ball.isFocus=true
prevX, prevY=event.x, event.y
elseif ball.isFocus then
if “moved”==event.phase then
ball.X, ball.Y=spaceToGrid(ball.x, ball.y, 60) – Find the square the ball is closest to
ball.position=square[clamp(ball.X, 1, 10)…”."…clamp(ball.Y, 1, 10)]
if ball.constraint==“Y” then – Constraint along the Y-axis
ball.y=clamp(event.y, 60, 600)
ball.x=ball.position.x
elseif ball.constraint==“X” then – Constraint along the X-axis
ball.x=clamp(event.x, 60, 600)
ball.y=ball.position.y
end
if ball.x<=ball.position.x+5 and ball.x>=ball.position.x-5 and ball.y<=ball.position.y+5 and ball.y>=ball.position.y-5 then – If the ball is near enough to the center of it’s square
ball.x, ball.y=ball.position.x, ball.position.y – Clamp it to the square’s position
ball.diffX, ball.diffY=math.abs(event.x-prevX), math.abs(event.y-prevY) – Difference between X dragging and Y dragging
ball.constraint=“Y” – Defaults to Y-axis constraint
if ball.diffX>ball.diffY then – If the X difference is greater than the Y difference (ie you dragged left or right instead of up or down)
ball.constraint=“X” – Constrain to X-axis
end
end
prevX, prevY=event.x, event.y – Reset prevX and prevY
end
end
return true
end
ball:addEventListener(“touch”, touchBall) – Add listener
[/lua]
Hope this helps!
Caleb [import]uid: 147322 topic_id: 35544 reply_id: 142184[/import]
Thanks Caleb!
Your code is pretty straight forward and easy to understand, however I do have a couple questions:
-
On line #26 [lua] square[w…"."…h]=display.newRect(0, 0, 50, 50, 16) [/lua] what does the 16 do?
-
Could you give an example of how the spaceToGride function works? Also, Say object1 starts at cell 3-1 and ends 3-9, object2 starts 4-9 , ends 7-9 etc… how would I specify those cells as the only cells the ball can move along?
-
When I run the simulator I can click on&interact with an object I have, but once I click on the ball, drag, and release the ball I can no longer interact with my other object and wherever I click on the display it starts to drag the ball…?
I changed the [lua] local function touchBall(event) [/lua] to [lua] function ball:touch(event) [/lua]
and added [lua]
if “ended”==event.phase then
display.getCurrentStage():setFocus(ball, nil)
ball.isFocus = false
end [/lua]
Thinking that would do the trick, but no such luck…
Thanks again for your help!! 
-Saer [import]uid: 148623 topic_id: 35544 reply_id: 142542[/import]
-
Sorry, I originally had it with roundedRects. That was the corner radius - it’s not needed.
-
SpaceToGrid takes an (x, y) position and converts it to grid coordinates (x, y). I don’t quite understand the last part of your question, though.
-
Change it to this:
[lua]
display.getCurrentStage():setFocus(nil)
ball.isFocus=false
[/lua]
Caleb [import]uid: 147322 topic_id: 35544 reply_id: 142547[/import]
lol no worries. thanks.
So if my track.x = 500, track.y = 375 it would take the x,y and match it with the corresponding grid cells?
okay, I have a line and it starts at grid cell (R = Right, D = down) 3R-1D and ends 3R-9D, a second line starts 3R-9D and ends 9R-9D … how would I define those grid cells as the only cells the ball can move along? - Hope that made sense.
Hmm, still doesn’t work…?
I have a circular platform that can rotate via the user touch&dragging. When I first run the simulator I can rotate the platform, but once I drag the ball it appears to constantly be the focus - wherever I click&drag within the display the ball ‘snaps’ instantly to that spot, and my rotating platform no longer registers when I go to rotate it…?
I think it may have something to do with this: [lua]
if “moved”==event.phase then
– Find the square the ball is closest to
ball.X, ball.Y=spaceToGrid(ball.x, ball.y, 30)
ball.position=square[clamp(ball.X, 1, 35)…"."…clamp(ball.Y, 1, 25)]
– Constraint along the Y-axis
if ball.constraint==“Y” then
ball.y=clamp(event.y, 75, 680)
ball.x=ball.position.x
– Constraint along the X-axis
elseif ball.constraint==“X” then
ball.x=clamp(event.x, 120, 925)
ball.y=ball.position.y
[/lua]
-Saer [import]uid: 148623 topic_id: 35544 reply_id: 142553[/import]
SpaceToGrid: Correct. That’s what it does 
You are making some sort of a thing with paths that the ball can move on - like a half-sliding puzzle, half-maze sort of thing?
More complex paths, in my opinion, should use something more complex. Something like… PHYSICS!!!
Walls on each side so that it can’t pass them, in other words. A simple way to do it would be to make a map in Tiled and create some sort of a simple map loading code that makes walls.
I’d be happy to help with that, too 
It’s odd - the un-setting focus works for me. Here’s the complete ball drag code for my previous post - you’ll probably need to change it, if you decide to go with the physics solution.
[lua]
local function touchBall(event)
if “began”==event.phase then
display.getCurrentStage():setFocus(ball)
ball.isFocus=true
prevX, prevY=event.x, event.y
elseif ball.isFocus then
if “moved”==event.phase then
ball.X, ball.Y=spaceToGrid(ball.x, ball.y, 60)
ball.position=square[clamp(ball.X, 1, 10)…"."…clamp(ball.Y, 1, 10)]
if ball.constraint==“Y” then
ball.y=clamp(event.y, 60, 600)
ball.x=ball.position.x
elseif ball.constraint==“X” then
ball.x=clamp(event.x, 60, 600)
ball.y=ball.position.y
end
if ball.x<=ball.position.x+5 and ball.x>=ball.position.x-5 and ball.y<=ball.position.y+5 and ball.y>=ball.position.y-5 then
ball.x, ball.y=ball.position.x, ball.position.y
ball.diffX, ball.diffY=math.abs(event.x-prevX), math.abs(event.y-prevY)
ball.constraint=“Y”
if ball.diffX>ball.diffY then
ball.constraint=“X”
end
end
prevX, prevY=event.x, event.y
elseif “ended”==event.phase then
display.getCurrentStage():setFocus(nil)
ball.isFocus=false
end
end
return true
end
[/lua]
Oh, and it’s better (I tried it out) to replace the spaceToGrid function with this (using math.round instead of math.ceil):
[lua]
local function spaceToGrid(x,y,size)
return math.round((x==0 and 1 or x)/size), math.round((y==0 and 1 or y)/size)
end
[/lua]
Caleb [import]uid: 147322 topic_id: 35544 reply_id: 142617[/import]
- Sorry for the delayed response -
Okay, sounds good.
I haven’t used Tiled before, so your assistance would be more than welcome! 
-Saer [import]uid: 148623 topic_id: 35544 reply_id: 143430[/import]
Ahh, I see. cool! 
Thanks, I made some changes and it works now.
Pretty much. It is a maze game that has a small line/track that the ball moves along, and there are some platforms that can rotate to connect with a different piece of track so the ball can continue until it reaches the goal/end-point etc… etc…
I have almost the entire game done (menus, scenes, sounds, and such ) and the only thing stopping me from finishing it is this whole ball-follow-path thing lol.
Actually, that was my first thought - create invisible rects that would border the entire track&platforms.
I would think you’d have to have the rect-borders partially overlap the the ball so there wouldn’t be any odd movement - such as the ball not moving in a straight line - when the user is dragging it…?
I’d very much appreciate your help with it. 
Would Tiled be a better choice over LevelHelper?
-Saer
[import]uid: 148623 topic_id: 35544 reply_id: 142679[/import]
If you’d prefer, feel free to email and/or skype me sometime.
Tdg_Photography@yahoo.com
Skype User-Name: Saerothir [import]uid: 148623 topic_id: 35544 reply_id: 142710[/import]
So here’s an example I put together:
https://www.dropbox.com/s/a3mb52ihagdlfqp/TiledMapDemo.zip
Let me know if there’s something you don’t understand 
C [import]uid: 147322 topic_id: 35544 reply_id: 143636[/import]
I chose Tiled because a) it’s simpler to use, b) we don’t need it to be extremely complicated, and c) it can do things extremely exactly.
So to start with, build a Tiled map. The tileset doesn’t really matter. It could even just be two images - a wall and your character.
Then, in your code, do an iteration so that if the data is 1 build a wall at that position, if the data is 2 build the character at that position.
There’s a simple framework.
I’ll build some code if you want me to 
Caleb [import]uid: 147322 topic_id: 35544 reply_id: 142921[/import]
Ok, I see now.
You could start with making a number of shapes that are built in certain paths/trails (like a bit of the Tiled maze example that I posted) and add them into their own group; when you want to rotate them, rotate the group. Or even make physics bodies that accomodate the ball/cube/whatever that you’re trying to move and use them.
C [import]uid: 147322 topic_id: 35544 reply_id: 144158[/import]
I have the rotating platforms with an image of what I want to use for the ‘track’ which rotates accordingly when I rotate the circular platform… The only two things I don’t have “coded” yet, are getting the ball to rotate if it is on the circular platfrom (I’m getting close to working that out), and getting the ball to follow the track when dragged.
Using your Tiled example, would I just place my ‘track’ objects where I want them and then code invisible walls to keep the ball moving steadily along the ‘track’?
-Saer
[import]uid: 148623 topic_id: 35544 reply_id: 144218[/import]
- Sorry for the delayed response -
Okay, sounds good.
I haven’t used Tiled before, so your assistance would be more than welcome! 
-Saer [import]uid: 148623 topic_id: 35544 reply_id: 143430[/import]
Quite right - you’d create as many “track” physics bodies as you need and add the track image to a group with that physics body attached to it.
When your player enters the track, add the player to that group and rotate the group.
At least I think that would work… You might get a problem with different-group physics.
C [import]uid: 147322 topic_id: 35544 reply_id: 144267[/import]
So here’s an example I put together:
https://www.dropbox.com/s/a3mb52ihagdlfqp/TiledMapDemo.zip
Let me know if there’s something you don’t understand 
C [import]uid: 147322 topic_id: 35544 reply_id: 143636[/import]