Timing changes query

BlitzMax Forums/BlitzMax Beginners Area/Timing changes query

DannyD(Posted 2005) [#1]
Hi,
Beginner question again :) This code moves a image on the screen in x+1, y+1 direction. I then want to alter the direction after a few seconds without any user input.After another 5 seconds I want to move it another direction. How do I go about scripting it like that ? Any suggestions ? Thanks in advance.



Flake.png available at


degac(Posted 2005) [#2]
Graphics 800,600',32
'load an image to tile
image=LoadImage("test2.png")

duration=500 'millisecs = 1/2 sec

dx=1
dy=1

timer=MilliSecs()

While Not KeyHit(KEY_ESCAPE)

	TileImage image,x,y

	'move the tiled background
	x=x+dx
	y=y+dy
	
	If MilliSecs()>timer+duration
	dx=-dx
	dy=-dy
	timer=MilliSecs()
	End If
	
	Flip;Cls

Wend



DannyD(Posted 2005) [#3]
ahh, thanks alot.


DannyD(Posted 2005) [#4]
using for the image.

This works:



while this doesn't:


I don't know why ? Am I right in asserting flip and cls should only be called once within a program? Any ideas or tips ?


Diablo(Posted 2005) [#5]
this is a scope problem. all the varibles within the function are local and refreshed everythime the function is called (or set back to 0) which means your basicaly doing 0 + 0 & 0 - 0.

make x & y global and it should fix it.

[edit] in fact it does fix it :)

[edit 2]
you need to globalize and init the other varibles too, by bet :D

[extra note]why your first code works: the varibles are in the program scope. why your second code dont work: the varibles are in the function scope and as a result go out of scope when the function is exited and the varibles are re init when you cal the function again.


Dreamora(Posted 2005) [#6]
Not needed.
If you define a variable as global within a function, it won't lose its value when the function ends, so they still have their old value, the next time the function is called.


Diablo(Posted 2005) [#7]
well, you learn somthing new every day. thx Dreamora i wont have to create point varibles any more.