Grid Movement Help
-
Designing a game that moves a character on a grid.
Any help would be appreciated.I built a mock up of the game, then blanked it, made a debug and created the stationary character. The animation sequence runs constantly, testing...
I have the dimensions of movement, which i believe are 20 or 40 pixels.
In other coding environments I've had access to frames and could easily mitigate this issue if they were available but, all I seem to be getting is it jumps to the next part of the grid.
ive tried making it with a while loop and several other ways but nothing seems to work.
The animation is 16 frames total ( 0 -15 ) and I have no idea how to code a character moving on a specific grid...I could keep trying and maybe figure it out but Im at a loss.
If you NEED my code for movement I could share it, but the
TLDR of it is...I need a character to animate across a grid with the press of a D-Pad button Press.
(im using press.r as the pressed boolean)
-
If sprite.x_speed == 0 and c.right and !oldc.right then
SetSpriteanimation(sprite,0,5,8)
SetSpritespeed(sprite,20,0)
Endif
If sprite.x_speed >1 then
If Getspriteanimframe(sprite)>=5 then
SetSpritespeed(sprite,0,0)
Endif
EndifMany ways to do it but this is the first that comes to mind, you would need to play with the animation speed/sprite speed values.
-
I’d write a ‘move_to(sprite,endpoint,timetotake)’ function that does the job.
It’d be like
If sprite.x != Endpoint.x and sprite.y != endpoint.y then
Sprite.xspeed = (endpoint.x - sprite.x)/timetotake
Sprite.yspeed = (endpoint.y - sprite.y)/ timetotake
Else
Setspritespeed(sprite,{0,0})
EndifSomething like that anyway
-
@waldron
This helped alot, had to really look into the sprite systems to see how things are worded...
Now i have another issue and will post my complete fix, if and when, it gets done.if playerSpr.anim_frame >= number then
gives me an issue...Stopping the animation, even if i stop it on the animation frame always wants to 0 out my animations frame, even if it says its at 8...
so my 2 states of motion would be 0 and 8 and ive tried for a few hours but...
it does not want to run the animation from the stop point. because after it sets the animation frame speed to 0 it also makes the file playerSpr.anim_frame equal to 0...annnnd, i dont know what is going on...
Thankful to have programmed for awhile to make debugs but...
this isnt making any sense, thanks for the help so far, at least he can move...
appreciate -
@_λιεχ_ Ah forgot to add in the new animation state to idle once there's no movement so just before the setspritespeed to 0,0 add in the new idle animation.
if your move animation is 0,8 then the end frame would be 8 then reset to 0,0. hope this helps i'm more visual and hands on with programming .
also for the depressed button oldc to work your loop should have this:loop
c=controlsupdate()
oldc=c
repeatunless your using a different method.
good luck :) -
@waldron
Ive tried alot.
ALOT... here is my code right now WHICH should work,
Only works one the first try then he just runs and runs and runs and...
It should make sense the way I've written it before I took it this far.if !move.a then
if playerSpr.x_speed == 0 and !c.right and pressed.r then
move.x = playerSpr.x + 40
setSpriteAnimFrame ( playerSpr, 8 )
setSpriteAnimation (playerSpr, 8, 15, 16 )
setSpriteSpeed ( playerSpr, 77.5, 0 )
endIfif playerSpr.anim_frame >= 15 then (i did 15 because the == doesnt work at all )
setSpriteAnimation ( playerSpr, 0, 15, 0)
setSpriteAnimFrame (playerSpr,0)
setSpriteSpeed (playerSpr, 0, 0)
pressed.r = false
playerSpr.x = move.x
move.a = true
endIf
endIfif move.a then
if playerSpr.x_speed == 0 and !c.right and pressed.r then
move.x = playerSpr.x + 40
setSpriteAnimFrame ( playerSpr, 0 )
setSpriteAnimation (playerSpr, 0, 15, 16 )
setSpriteSpeed ( playerSpr, 77.5, 0 )
endIfif playerSpr.anim_frame >= 8 then
setSpriteAnimation ( playerSpr, 8, 0, 0) <- i know 8,0,0 works...
setSpriteAnimFrame (playerSpr,8)
setSpriteSpeed (playerSpr, 0, 0)
pressed.r = false
playerSpr.x = move.x
move.a = false
endIf
endIfTHIS confuses me so much...
the playerSpr.anim_frame will equal 7.99999999 even with rounding if I do other things.
this way gets the player animation to stop on frame 8, but when you start the animation up it counts up from 0... but it clearly is frames 8-0...BUG? im so annoyed at something that is not even that hard for my own brain to compute.
but with this setup, he will walk the first grid perfect, start walking the other grid and just keep replaying the 8-0 animation frames as 0-8.... FACEPALM
-
@waldron
as a side note, this is all in a FUNCTION aswell so...
idk if that has anything to do with it... -
@_λιεχ_ I think this might be your problem. The animation frame returns a count of which frame in the cycle it is on starting from zero rather than the tile number. So if your animation runs from tile 3 to 6 it will return values 0 to 3. 0 representing tile 3, 1 representing tile 4 etc. @waldron will know, his sprite work is miles ahead of mine.
-
i just made 2 sprites and now all my code works 100%....
Im not going to try and imagine why a stop watch would reset its internal timer to add to itself again when it already has a number... it can add onto. If thats how the engine works...Id rather just make 2 different character models and write the code out more i guess...
but that means i need 2 models for turning, 2 models for climbing, 2 models for jumping...I just wanted to add depth into an 8bit character...
now i question how ill add the function to hold down the c.right to make him run till i let go...
I guess the computation is fast enough you wont see the img swap but... ill hate my life a little... -
@_λιεχ_ ideally animation states is what your after i didn't want to confuse you, the Gothic Vania sample program provides a state machine which triggers animations when called.
as Richard was saying you only count the frames in the animation so setspriteanimation(sprite,4,8,10) the end frame would be 4.
i added the sprite speed part so the user can't interrupt movement between moves.
i'd be happy to share a program later if your still having trouble to help you get over the first few hills.