[noobie] Sprite stopping at walls a tile too late

I think I know the answer but I was hoping someone might be able to offer some insight as to a better way of producing this idea.  What I have is a Castle demo gameloop(), with a sprite that moves randomly.

[lua] – sprite movement / solid detection
local spritewall = obstacleUniv(mrsprite, mrsprite.level, mrsprite.locX, mrsprite.locY)

if not spritewall then
– mrsprite:play()
npcPath = getNPCRandomPath() // up / down / left / right, randomly

if( not mrsprite.isMoving ) then
    mte.moveSpriteTo( { sprite = mrsprite, locX = mrsprite.locX + npcPath[‘cords’][1], locY = mrpoo.locY + npcPath[‘cords’][2], time = spritemoveTime, easing = “linear” } )
    mrsprite:setSequence(npcPath[‘direction’])
    mrsprite:play()
end

– mte.moveSprite(mrsprite, aiPath1[aicounter][1], aiPath1[aicounter][2])
– npcAIPath1(mrsprite)
else
    – print(‘pause the sprite’)
    – print(mrsprite.sequence)
    – mrsprite:setSequence(‘left’)
    – mte.cancelSpriteMove(mrsprite) – maybe… but no
    mrsprite:pause() – you’ve gone too far mrsprite!!!
    – mrsprite:pause()
end[/lua]

I suppose I just need to check the tile property one tile ahead of the direction of the sprite movement in order to stop it before it walks too far.  Is there a cleaner way to handle this logic?  I understand this might be broad question but mainly seeking advice.

Thanks for any help / advice!

Generally what you would want to do is figure out where the sprite is going to go and then check that location for an obstacle. If an obstacle is found, find another random direction and check for an obstacle at that location. Rinse and repeat until an obstacle-free destination is found. The important detail is that you must check the destination for an obstacle, not the sprite’s current location.

Thanks Dyson!  Ultimately, I was able to do this.  Surprisingly easier than expected.  Occasionally the animation sequence stops when my sprite finds a solid but for the most part, works consistently.

The one trick I was hoping to solve without using a global is to move my sprite one block, pause for a random amount of time between 2000-6000ms, then move a random direction again.  Setting a global to true, then calling a performwithdelay after the sprite move, to set the global to false, then checking for it in the gameloop, seems to have inched me closer to success than anything else I’ve attempted.  From here, I’m going to experiment with basic dialog and menus now that my NPC’s have a better attention span.

I realize that my path of learning to code an RPG is going to be experimental rather than following best practice as there’s not a great deal of reference material that I’ve been able to find just yet.  Doesn’t seem as impossible as I first thought.

Thanks again for responding :wink:

The idea of the global variable isn’t inherently bad, it just comes with it’s own problems involving garbage collection and the risk of overwriting Corona’s internal libraries. For example, creating a global variable named debug will break the SDK’s error reporting in the terminal. This link explains in more detail why globals are bad and a simple method for avoiding them: http://www.coronalabs.com/blog/2013/05/28/tutorial-goodbye-globals/

Using a global flag for triggering NPC movement will cause every NPC to wait the same interval as each other and move as one. You could create multiple global flags, but then the question arises of how you will assign them to NPC’s, and how you will apply a timer to each one individually.

Something you might find helpful is that all Corona display objects are also tables, and you can add any additional parameters to those tables you might need. So, for example, you could give every sprite a timer parameter and set it to a random number. Then, in your gameloop, you could iterate through your sprites and subtract each sprite’s timer by 1. If the sprite’s timer == 0, move the sprite and set it’s timer to a new random number. Now your NPC’s will all move and wait independently of each other.

Generally what you would want to do is figure out where the sprite is going to go and then check that location for an obstacle. If an obstacle is found, find another random direction and check for an obstacle at that location. Rinse and repeat until an obstacle-free destination is found. The important detail is that you must check the destination for an obstacle, not the sprite’s current location.

Thanks Dyson!  Ultimately, I was able to do this.  Surprisingly easier than expected.  Occasionally the animation sequence stops when my sprite finds a solid but for the most part, works consistently.

The one trick I was hoping to solve without using a global is to move my sprite one block, pause for a random amount of time between 2000-6000ms, then move a random direction again.  Setting a global to true, then calling a performwithdelay after the sprite move, to set the global to false, then checking for it in the gameloop, seems to have inched me closer to success than anything else I’ve attempted.  From here, I’m going to experiment with basic dialog and menus now that my NPC’s have a better attention span.

I realize that my path of learning to code an RPG is going to be experimental rather than following best practice as there’s not a great deal of reference material that I’ve been able to find just yet.  Doesn’t seem as impossible as I first thought.

Thanks again for responding :wink:

The idea of the global variable isn’t inherently bad, it just comes with it’s own problems involving garbage collection and the risk of overwriting Corona’s internal libraries. For example, creating a global variable named debug will break the SDK’s error reporting in the terminal. This link explains in more detail why globals are bad and a simple method for avoiding them: http://www.coronalabs.com/blog/2013/05/28/tutorial-goodbye-globals/

Using a global flag for triggering NPC movement will cause every NPC to wait the same interval as each other and move as one. You could create multiple global flags, but then the question arises of how you will assign them to NPC’s, and how you will apply a timer to each one individually.

Something you might find helpful is that all Corona display objects are also tables, and you can add any additional parameters to those tables you might need. So, for example, you could give every sprite a timer parameter and set it to a random number. Then, in your gameloop, you could iterate through your sprites and subtract each sprite’s timer by 1. If the sprite’s timer == 0, move the sprite and set it’s timer to a new random number. Now your NPC’s will all move and wait independently of each other.