object moves every x millisecs

BlitzMax Forums/BlitzMax Beginners Area/object moves every x millisecs

B(Posted 2009) [#1]
i would like to know how to make an object move every x number of milliseconds.

I have this game where you can adventure around, and as soon as you get in range of the enemies they chase after you, however the enemies get to you the instant you get within range.

so i need to have them move only x number of milliseconds.

ive tried converting some code that someone shared with me to have a key pressed every x milliseconds, however I was unable to change it to my purpose.

Thanks!

B


BladeRunner(Posted 2009) [#2]
type test
   field lastmoved:int
   
   method move()
      if millisecs() > lastmoved then
           lastmoved = millisecs()+500' 2 times a second.
           'your moving stuff here
           print "hello moving world" 'for testing purposes only
      endif
   end method
end type

local t:test = new test
while not keyhit(key_escape)
   t.move()
wend



Jesse(Posted 2009) [#3]
you are making your game programming too complicated. The way you are doing it will require to make a trigger for every object you want to move.
Try moving everything a fraction of a pixel.

lets say you have an object with x and y both have to be floats).
first you know that flip() flips sixty times a second so if you want it to move 2 pixels per second you do:
dx:float = 2.0/60.0
dy:float = 2.0/60.0
so if you want to move your character to the right you do this:
x:float = x+dx
for down:
y:float = y+dy
and to draw it all you have to do is:
drawimage image,x,y or drawtext text$,x,y
that should work for any character you want to move even your player.


Pete Carter(Posted 2009) [#4]
Doesnt blitzmax default the refresh to your desktops refresh rate unless you set it?


Jesse(Posted 2009) [#5]
@Peter Carter:
if look up the graphics command,it shows this:
Function Graphics:TGraphics( width,height,depth=0,hertz=60,flags=0 )
if you don't specify the hertz it will default to 60. and is what you use when you do a flip() or flip(-1).


gameproducer(Posted 2009) [#6]
This thread might be helpful:
http://www.blitzmax.com/Community/posts.php?topic=83511


Jesse(Posted 2009) [#7]
I didn't want to mention it yet as I thought it might be a bit too much for him.


B(Posted 2009) [#8]
Ya all that stuff is pretty complicated to me.

I can sorta understand what they are talking about, but putting those ideas down to code is a whole other ballgame for me.

I put bladerunners code in my game and it seems to work fine. Jesse's idea worked but it didnt quite capture the feel of the game that I wanted.

I was wondering could I use Jesse's idea, and then just change how often I update the graphics to make it seems like its moving on a 10x10 grid? like squares?


Jesse(Posted 2009) [#9]
then in that case do this
if you did it like I said, then
what ever result you get out of x and y do this:

px:float = floor(x/10.0)*10.0 'this will make the movement only every ten pixels
py:float = floor(y/10.0)*10.0

drawtext text$,px,py
drawimage image,px,py


B(Posted 2009) [#10]
ok cool!

it took me a while to get it working in my code, but now I understand it!
that floor command is cool!

thanks alot Jesse!

ill use this code as it gives it the feel of of moving every 10 pixels but doesnt actually do that. it also cuts way back on code and I think it's better than creating a flag every single time.

does this way not slow down the program as much as making flags?

what exactly are the pros and cons of this way over Bladerunner's way?


Jesse(Posted 2009) [#11]

does this way not slow down the program as much as making flags?


True, less code to process.

what exactly are the pros and cons of this way over Bladerunner's way?


blade runner way has its use. Such as writting text while your program is running. I don't think he generally codes that way either(I coud be wrong). We are all just trying to work with you to get your code working.

I personaly don't think anybody uses his way to move objects or process key presses for object movement but I can only speak for myself.


BladeRunner(Posted 2009) [#12]
Actually I would use such a code for moving purposes of enemies.
As they are in a seperate Class they can just be updated in a single loop and all movement woud be done for each instance. I don'tsee where this way is to complicated?
If all Instances of an class had to be updated/moved/whatever at once, i would add a class-global timer for this use.
This way there can be individual movement and also grouped movement with jus one line of code called.
The overhead of this operations shouldn't be a problem at all.

Maybe my code was misinterpreted because it was a very simplified example?