Timing changes query

BlitzMax Forums/BlitzMax Beginners Area/Timing changes query

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.

Graphics 800,600,32
'load an image to tile
image=LoadImage("flake.png")

While Not KeyHit(KEY_ESCAPE)

	TileImage image,x,y

	'move the tiled background
	x=x+1
	y=y+1

	Flip;Cls

Wend


Flake.png available at

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


ahh, thanks alot.

using for the image.

This works:
Graphics 800,600,32
'load an image to tile
image=LoadImage("flake.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




while this doesn't:
Graphics 800,600,32
'load an image to tile
Global image=LoadImage("flake.png")




While Not KeyHit(KEY_ESCAPE)

	function1()
	
	Flip;Cls

Wend

Function Function1()
TileImage image,x,y
duration=500 'millisecs = 1/2 sec
timer=MilliSecs()

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

End Function


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 ?

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]
Graphics 800,600,32
'load an image to tile
Global image=LoadImage("flake.png")
Global x, y
Global duration = 500 'millisecs = 1/2 sec
Global timer
Global dx = 1, dy = 1



While Not KeyHit(KEY_ESCAPE)

	function1()
	
	Flip;Cls

Wend

Function Function1()
TileImage image,x,y
'duration=500 'millisecs = 1/2 sec
'timer=MilliSecs()

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

End Function

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.

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.

well, you learn somthing new every day. thx Dreamora i wont have to create point varibles any more.