[Resolved]Need help with movement in the game

Hello Corona Community,

I am working on a game using Corona and Lua and I am having some trouble with the code to make the player move.

Basically, the player needs to be moving in a clockwise circle until the player taps on him, at which point he will move a bit to the right (crossing a river), and continue looping counterclockwise there. I have two functions thus far, one to deal with the circling movement (on either side of the river) and one to deal with the crossing of the river. I know the code is a bit crap at this point, so any pointers as to how to tidy it up would be welcome.

Here’s what I have so far:
[lua]function movePed(event)
pedxpos = pedxpos + ( pedxspeed * pedxdirection );
pedypos = pedypos + ( pedyspeed * pedydirection );

if ( pedxpos >= 100 and pedypos > 190 ) then
pedxdirection = -1
pedydirection = -1
pedxspeed = 0.5
pedyspeed = 0
transition.to (ped, {time = 1500, x = 80, y = 190})
end

if ( pedxpos <= 80 and pedypos > 190 ) then
pedxdirection = -1
pedxspeed = 0
pedyspeed = 0.5
end

if ( pedxpos <= 80 and pedypos < 35 ) then
pedxdirection = 1
pedyspeed = 0
pedxspeed = 0.5
print(“three”)
end

if ( pedxpos >= 100 and pedypos < 35 ) then
pedydirection = 1
pedxspeed = 0
pedyspeed = 0.5
end

if ( pedxpos >= 220 and pedxpos <= 239 and pedypos < 190 ) then
pedxspeed = 0
pedyspeed = 0.5
pedydirection = -1
end

if (pedxpos >= 220 and pedxpos < 239 and pedypos < 35 ) then
pedxdirection = 1
pedyspeed = 0
pedxspeed = 0.5
end

if (pedxpos >= 240 and pedypos < 35 ) then
pedxspeed = 0
pedydirection = 1
pedyspeed = 0.5
print(“seven”)
end

ped:translate( pedxpos - ped.x, pedypos - ped.y )
end

Runtime:addEventListener( “enterFrame”, movePed );

function animate(event)
pedxpos = pedxpos + ( pedxspeed * pedxdirection );
pedypos = pedypos + ( pedyspeed * pedydirection );

if ( pedxpos <= 100 and pedypos <= 190.5 ) then
pedxspeed = 1.5
pedyspeed = 0
pedxdirection = 1
moved = true
end

if (pedxpos >= 210 and pedypos <= 190.5) then
pedxspeed = 1.5
pedyspeed = 0
pedxdirection = -1
moved = true
end

ped:translate(pedxpos - ped.x, pedypos - ped.y )
end

ped:addEventListener( “tap”, animate )[/lua]
I tried doing it all with transition.to, but that function doesnt seem to alter the players x and y coordinates for some reason.

Any workaround guys? I’m absolutely swamped.

Thanks. [import]uid: 106739 topic_id: 18202 reply_id: 318202[/import]

Plug and play code or a download would let people run and test your code and possibly provide more help.

Please post code in < lua > tags - reading it as plain text is really, really difficult when you post a lot of it.

For the positioning, is that what it is returning? 16, 16? What is your reference point set at?

Peach :slight_smile:

[import]uid: 52491 topic_id: 18202 reply_id: 69645[/import]

Hello again,

I have worked out that I can move the player with the translate.to function, however, it doesn’t actually do anything. I got rid of most of my code and started over, so I can test translate.to but it still won’t move

This is what I have so far:

[lua]local pedxdirection = 1
local pedydirection = 1

local pedxspeed = 0
local pedyspeed = 0

local ped = display.newImage( “ped.png”, 90, 35 )
ped:setReferencePoint(display.CenterReferencePoint)

function movePed(event)
pedxpos = ped.x + ( pedxspeed * pedxdirection );
pedypos = ped.y + ( pedyspeed * pedydirection );

if ( pedxpos > 0 and pedypos > 0 ) then
transition.to ( ped, { time = 1500, x = 100, y = 190 } )
end[/lua]
Also, for some reason, everything is displayed 16 pixels left and downwards of where I set it. For example, I may have set the pedestrian to appear on 90, 35, but through passing ped.x and ped.y into a string and then displaying that on the screen, it shows me he is displayed on 16, 16. Does anyone know why that is? [import]uid: 106739 topic_id: 18202 reply_id: 69586[/import]

Hello Peach,

Before I begin explaining I probably have to mention I happened upon Techority in my search for answers before, and I really like it :slight_smile: I asked myself whether I should contact you directly but I thought, in her place, I wouldn’t appreciate someone contacting me directly for something like that, really (When I say directly, I mean Facebook). So, here we are.

Well, now that the preamble is over, let’s begin.

Initially I had the code I posted in the beginning. That is some ugly code, aint it. Plus, complicated paths for the player are dificult (like diagonal or curved paths). So, out with the old, in with the new. I tried transition.to( ), but that wouldn’t move the player at all. After hours and hours of painful manual debugging I narrowed the problem down to one (almost) line. It was the fact that I was putting transition.to( ) in a function.

Specifically, it was that I was adding an “enterFrame” listener. That listener just didn’t want to work. If I do timer.performWithDelay( ), or even Runtime:addEventListener(“touch”, move) it works fine. But when I try Runtime:addEventListener(“enterFrame”, move ) it breaks.

So this is what I’ve narrowed it down to. If this works, maybe I can have a look at that coordinates problem again.
[lua]local ped = display.newImage(“ped.png”, 90, 35 )

function move(event)
transition.to( ped, { time=1500, x=100, y=100 } )
end

Runtime:addEventListener(“enterFrame”, move)[/lua]
(And to answer your last question, yes, it did show me that the image was in fact displayed at 16, 16, even though I had set it to 0, 0 when I created it. And my reference point was at the center, I think it shows in the code I posted somewhere.)

[lua]ped:setReferencePoint(display.CenterReferencePoint)[/lua] [import]uid: 106739 topic_id: 18202 reply_id: 69680[/import]

Hey there,

Thanks for that - I appreciate it :slight_smile: (Both the kind words and the courtesy. Although I never mind people getting in touch it can be hard to keep up with them all sometimes and often you’ll get a faster reply here anyway ;))

I look at your code and I think that there is a better way of doing things - take a look at this example. It lets you draw a path BUT you can also predefine one. (Which I can offer more advice about if needed.)

http://developer.anscamobile.com/code/move-object-through-path

Let me know if this is useful for you - I know it’s a bit of a change from how you have been doing things but if you make the change I believe you’ll be happy with the end result.

Peach :slight_smile: [import]uid: 52491 topic_id: 18202 reply_id: 69754[/import]

Hello again Peach,

While what you sent me was immense help, and it is how I was thinking of eventually doing things, it still doesn’t want to cooperate.

I have wrote this, thus far, and while it is going in the first function normally (I did some manual debugging with print() ) the transition.to( ) seems to not want to work. I feel I am being incredibly thick, but I’d rather ask than stare at the screen a minute longer. I’ve done enough of that at it is.

[lua]local path = {
TopLeftX = 0,
TopLeftY = 0,
TopRightX = 100,
TopRightY = 0,
BottomRightX = 100,
BottomRightY = 100,
BottomLeftx = 0,
BottomLeftY = 100
}

local function movePedTopRight(event)
transition.to(square, {time = 500, x = path[TopRightX], y = path[TopRightY], onComplete = movePedBottomRight})
print(“one”)
end

local function movePedBottomRight(event)
transition.to(square, {
time = 500,
x = path[BottomRightX],
y = path[BottomRightY],
onComplete = movePedBottomLeft
})
print(“two”)
end

local function movePedBottomLeft(event)
transition.to(square, {
time = 500,
x = path[BottomLeftX],
y = path[BottomLeftY],
onComplete = movePedTopLeft
})
end

local function movePedTopLeft(event)
transition.to(square, {
time = 500,
x = path[TopLeftX],
y = path[TopLeftY],
onComplete = movePedTopRight
})
end

Runtime:addEventListener(“tap”, movePedTopRight)[/lua]

Thank you for helping out. I understand how full your schedule is, so I will try to only ask when completely bogged down (like now :P) [import]uid: 106739 topic_id: 18202 reply_id: 69886[/import]

Hey again,

You have wonderful manners :slight_smile:

Replace the code from your last post with this;
[lua]path = {}
path.TopLeftX = 0
path.TopLeftY = 0
path.TopRightX = 100
path.TopRightY = 0
path.BottomRightX = 100
path.BottomRightY = 100
path.BottomLeftx = 0
path.BottomLeftY = 100

local movePedBottomRight
local movePedBottomLeft
local movePedTopLeft

local function movePedTopRight(event)
transition.to(square, {time = 500, x = path.TopRightX, y = path.TopRightY, onComplete = movePedBottomRight})
print(“one”)
end

print (path.TopRightX)

movePedBottomRight = function(event)
transition.to(square, {
time = 500,
x = path.BottomRightX,
y = path.BottomRightY,
onComplete = movePedBottomLeft
})
print(“two”)
end

movePedBottomLeft = function(event)
transition.to(square, {
time = 500,
x = path.BottomLeftX,
y = path.BottomLeftY,
onComplete = movePedTopLeft
})
end

movePedTopLeft = function(event)
transition.to(square, {
time = 500,
x = path.TopLeftX,
y = path.TopLeftY,
onComplete = movePedTopRight
})
end

Runtime:addEventListener(“tap”, movePedTopRight)[/lua]

A few minor changes plus some forward declarations.

Let me know how that goes - hopefully it might alleviate some of your frustration.

Peach :slight_smile: [import]uid: 52491 topic_id: 18202 reply_id: 69974[/import]

Hello again,

IT IS ALIVE. Sorry, I’ll try to keep my B-Movies love out of this.

It merrily slides along the screen in a wonderful fashion. If only you could see me clapping at my own screen. It was very embarrassing indeed.

Thank you for all your help, sorry to keep you busy and I do hope you have a wonderful day! [import]uid: 106739 topic_id: 18202 reply_id: 70012[/import]

It’s OK, I like B-Movies too, depending on genre. (NOT drama. Horror, yes.)

No worries - I’m thrilled it made you clap, embarrassment inducing or not :wink:

You have a great day, too!

Peach :slight_smile: [import]uid: 52491 topic_id: 18202 reply_id: 70014[/import]