Animation speed & Game pathfinding

Hi,

I’m make some progress on my game… at least I think I am…

Anyway how do I slow the “sprites” animation down, not the movement but the animation ?
I’m using a timer, period set to 10 and invalidates the game canvas. Everything is fine except the characters animate too fast.
I’ve tried adjusting the timer period to a higher number but that effects the movement speed.

Also I don’t understand how to get the enemies to chase after the hero. I’ve searched about pathfinding but thats a bit above me.
Right now I have enemies that wander around aimlessly , basically dumb enemies . I don’t need super AI or anything fancy just something that is good enough,
if you will.

Thanks.

Mac OS 10.12.6

One question is simple to answer: instead of moving 10 pixels let the sprites move only 1 pixel.

The other question is much harder. How do you expect to develop your game if you don’t understand the basics? Pathfinding algorithms aren’t that complicated. So:

a) you get a subscription to xdev and read the back issues. I remember some articles about path finding.
b) you go to rblibrary.com and check out the titles. The one with the canvas should be very helpful.
c) at Amazon you check out titles on “game development”. Those are usually in another language. Stuff on Java or Javascript is relatively easy to rewrite in Xojo.

Its not the x,y movement but the frame animation that I need to slow down.

Well the basics can be somewhat subjective. Not all game use pathfinding . I could just use all dumb enemies , though would like
to have a few that are “smart”.

Thanks.

you do not want to slow down the FPS… trust me.
what you DO want to do is move the objects at a given speed…
X pixels per second as an example

One way is a timer with a period of 1/x and each time it fires move a delta of 1 pixel… you may have to experiment a bit

You want to use a timer as you app may run on various machines with different processing speeds, and you don’t want your objects moving faster just because its on a faster machine…

Its not the objects movement, they are moving just fine, its the frame animation that too fast.

For the characters I have 3 frames of animation for each direction. Each image with arms and legs in different positions.
Its the changing of the images thats too fast.

Perhaps I wasn’t clear or I’m not understanding.

Thanks.

frame animation can NEVER be “TOO FAST”… you want it as fast as possible

So what indicates to you the FPS is too fast?

Its not over all fps, its when it goes through the 3 frames of animation the arms and legs cycle too fast. Its like a blur.
Also when an enemy dies it cycles through a poof of smoke animation but it goes by so fast you barely notice it.

EXACTLY why a timer should be used… not to control the frame speed, but to control the animation itself
but you do what you please… I’ve attempted to provide my insight, but you seem to have other ideas.

Good Luck… I’m out

Well I am using a timer. I though it was controlling the animation…

Your insight is helpful.

Perhaps I wasn’t being clear or I misunderstood something…

Thanks.

I tried to post this earlier and it double posted so I deleted one, now it’s gone.

Anyways, a common method is to calculate a time delta between frames and use that to calculate your velocities, animation frames, and anything else that needs to animate with reference to time. Even though you use a timer to do something, it doesn’t actually guarantee that your timer will make things happen when you expect. By determining how much time has passed between frames you can do things based on the actual time passed and it’s much more accurate. HTH

Hi Rock,

It might be a good idea to do some reading up on stop motion animation/claymation, because the theories there will still hold true regardless of whether you are changing frames in a Xojo canvas or moving a physical object.

My 10yr old son and I mucked around with claymation some time back and the hardest thing was trying to make a figure look like it was walking. Look up “ease-in” and “ease-out” and you’ll find that 3 frames will not be enough for a step.

I’d recommend that you download a free animation app. Load up your frames, then have a good look at what’s happening, add some new frames or repeat some. Then look at how you could apply this via a timer in Xojo.

Hope this is of some use.

Okay, so you need to stop thinking in frames and start thinking in time. How long in milliseconds/ticks do I want this image to be shown for? How many seconds does it take for this sprite to move from this point to this point?

It’s up to you how simply or complex your solution needs to be, but the basic principle is that the timer should ideally only invalidate or refresh the canvas. Obviously you can use the same timer to update each elements graphic and location.

It can be hard at first to get your head around it, but as someone who wrote games in Xojo (over a decade ago) it makes it easier in the long run.

Thanks for the feedback.

I think thats what I am missing, “a time delta between frames” / '“milliseconds/ticks”.

Just for the record I have a time called AnimeTimer, which all it does is call GameCanvas.Invalidate.

3 frames of animation might not be the best but I do believe Mario in Super Mario Bros (NES) only had 3 frames of walk animation and I think BurgerTime only has 3 frames as well. When I get further along I’ll look at adding more frames… though I’m not that great at making sprites.

I did a web search for “calculate a time delta between frames” and it brought up a post from XOJO forums and someone had posted the following

sub MyRefresh()

static previousTicks as single
if ticks > previousTicks then
 Canvas1.Refresh
previousTicks = Ticks
else
 Canvas1.invalidate // In case it is the last drawing
next

end sub

Would this be what I need or something similar ?

Thanks for all the help !

So off the top of my head… A simple version would be.

Dim currentTime as double = ticks - lastCycleTime
if currentTime < 20 then
    // --- Display frame1
elseif currentTime < 40 then
    // --- Display frame 2
elseif currentTime < 60 then
    // --- Display frame 3
else
    lastCycleTime = ticks // --- begin the loop again.
end if

gameCanvas.invalidate( me.left, me.top, me.width, me.height, false )

Oh and never invalidate the whole canvas (unless you really need it all changed), otherwise simply invalidate the smallest possible rect, in this case the rect for the sprite. Otherwise you’re wasting time flushing the rest of the canvas to screen buffer.

Here is a “GameKit” I started several years ago based on Apple’s SpriteKit. The Scene is the parent and the sprites are child nodes which can be nested as needed. You attach actions to the nodes to handle animations, movement, rotation, scaling, etc. The actions can be repeated, grouped and sequenced. The scene has a timer which handles the game loop and all updating.

The above project contains a simple tank game with preferences to see how things tie together. It may be worth taking a look at to get some ideas.

The example game controls are.

Tank movement/rotation WASD
Turret rotation arrow keys
Shoot Spacebar

Here is another project using code from a Maze book I was porting code from. It has code for generating mazes, pathfinding, etc.

There is a game included with various mobs and power ups. The goal is to get through the maze to the exit. The maze will be created at whatever size you make the window. There are preferences to play with the maze generation and mob spawning. You can use fog of war or not. The mobs have different sensing methods (line of sight, sound, feel) range and speed. There are powerups to stun and teleport the mobs as well as a reveal map area.

Controls

Left click where you want to move. Pathfinding will be done in shortest route. Only visible areas can be clicked if using fog of war.
Click power up (lower left of screen) to pick up and right click to place on any visible portion of the maze.
Walk over boxes to collect powerup.
Spacebar to start/pause game.
All the mobs in the maze and their current state (awake, asleep/stunned) are displayed on the bottom of the screen.

Both projects are in unfinished but playable states. They were for my own fun and learning. Please use, abuse, copy, throw out/up to your hearts content.

I wish there had been more example code in Xojo when I started. Porting form other platforms is not that hard though and a great way to learn.

Cool thanks!

I’ll take a look at those when I get a chance.

The characters animations are working as I want. Now I’m working on the pathfinding. I added code from the 1510 example. I’m trying to figure how to have the enemy to follow the path found. In the 1510 example its just drawing a line from start to goal.

Anyone use the 1510 example know how to do this?

Thanks.