Tween

Miscellaneous Forums/General Discussion/Tween

Okay, when you want to move an image in blitz, let's say you want to get it from 100,150 to 200,300. Normally, you might write something like this:

While Not KeyDown(1)

Cls

If imagex < 200 Then imagex = imagex + xspeed
If imagey < 300 Then imagey = imagey + yspeed

DrawImage image,imagex,imagey

Flip

Wend
Okay, simple enough, but a little tedious to retype every time you need it, especially if the starting & ending coordinates are both variables. Now, in Macromedia Flash, if you want to move an image, you can simply choose a starting & ending point, and the image will automatically "tween" between the two points at a set rate. Imagine if this could be refined to a Blitz function, which could be declared like this:

Tween(image,frame = 0,startx,starty,endx,endy,speed = 1)
Am I the only one that thinks that would be useful?

Okay, simple enough, but a little tedious to retype every time you need it

Retype? You need to use methods. Set the end location of the object (image), then have an update method and a draw method that does all of the work.

The functions you are talking about exist (after you write them).

http://www.syntaxbomb.com/forum/index.php/topic,351.0.html

You should manage to convert it into Types

Dabz

Wow, talk about unoptimized code...

Maybe you should think about removing the 3 trig calculations out of the equation, when all you want is a simple scaled delta.
You are going from the deltas, using a trig function (atan2) to get the angle, and then regetting the vector with 2 other trig (sin and cos) and scale them up. You can get away with a simple square root. I'd suggest to check on vectorial maths a bit ;)


Wow, talk about unoptimized code...



lol, I never mentioned it was optimized! ;)

It does what he asked so wheres the beef, it did what I wanted it to do when I sourced it from god knows where! :D

Though, I did notice you can talk the talk, so share your optimized example and we may all learn something! ;)

Dabz

I was kinda bored ...

Graphics 1024,768

;make anim image
Global Image = CreateImage( 32,32, 2)
SetBuffer ImageBuffer( Image, 0 )
Color 0,0,255 : Oval 0,0,32,32,1
SetBuffer ImageBuffer( Image, 1 )
Color 255,0,0 : Rect 0,0,32,32,1
SetBuffer BackBuffer()
MidHandle Image

Type TweenT
	Field Image, Frame
	Field CurrentX#
	Field CurrentY#
	Field EndX#
	Field EndY#
	Field Speed#
	Field Active
End Type

For l = 1 To 10
	TWEENcreate( Image , Rand(0,1), Rand( 20,800 ), Rand( 20,600 ) , Rand( 20,800), Rand(20,600), Rnd( .1, 5 ) )
Next

While Not KeyDown(1)

	Cls
	TWEENupdate()
	Flip
	
Wend

End

;==============================================================================
;==============================================================================
;==============================================================================

Function TWEENamend( t.tweenT, StartX#, StartY#, EndX#, EndY#, Speed# )

	t\EndX = EndX
	t\EndY = EndY
	t\Speed = Speed
	t\Active = True
	t\CurrentX = StartX
	t\CurrentY = StartY
	t\Active = True

End Function

;==============================================================================
;==============================================================================
;==============================================================================

Function TWEENcreate( Image, Frame , StartX#, StartY#, EndX#, EndY#, Speed# )

	t.tweenT = New tweenT
	t\Image = Image
	t\Frame = Frame
	t\CurrentX = StartX
	t\CurrentY = StartY
	t\EndX = EndX
	t\EndY = EndY
	t\Speed = Speed
	t\Active = True

End Function

;==============================================================================
;==============================================================================
;==============================================================================

Function TWEENupdate()

	For t.TweenT = Each TweenT
		
		If t\Active
		
			Dx# = t\EndX - t\CurrentX
			Dy# = t\EndY - t\CurrentY
			Distance# = Sqr( Dx * Dx + Dy * Dy )
			
			If Distance < t\Speed
				t\CurrentX = t\EndX
				t\CurrentY = t\EndY
				t\Active = False
				
				;pick another point
				TWEENamend( t , t\CurrentX, t\CurrentY, Rand(20,800), Rand(20,600), Rnd(.1, 5) )
				
			Else
			
				Nx# = Dx / Distance
				Ny# = Dy / Distance
				t\CurrentX = t\CurrentX + Nx * t\Speed
				t\CurrentY = t\CurrentY + Ny * t\Speed
			EndIf				
			
		EndIf

		;testing		
		Color 255,255,255
		Line t\CurrentX, t\CurrentY, t\EndX, t\EndY
		Oval t\EndX - 3, t\EndY - 3 , 7, 7, 0
		DrawImage t\Image, t\CurrentX, t\CurrentY, t\Frame
		
	Next
	
End Function		
						
;==============================================================================
;==============================================================================
;==============================================================================


Nice! :)

I'm not being funny Stevie, but is there any chance you could post that where my link points too?

Because it is, nice! :D

Dabz

Done. I always forget about Syntax Bomb tbh. I guess everyone on there comes here too?


Done. I always forget about Syntax Bomb tbh. I guess everyone on there comes here too?



I do, some others might pop over now and again, but with all respects, it has more of a community feel over there, which is good!

We have a good crowd over there, always willing to lend a hand with something. It would be nice to see more coders from here over there though, what with the amount of effort Qube has put in! :)

It has also drove me on more, because there is more support, even though the posting community is still low.

As an example, I am now within a week of releasing my first game commercially, not once in my 6-7 years here have I come close to that, fair do's, I've made complete games, but usually clones with bad graphics! :D

So its all gravy! :D

Anyway, thanks for posting that over there, I'm sure someone will find extremely useful... Your a good'un! ;)

Dabz

@Dabz, I think this is what he was talking about:
Function move()
		mag# = Sqr((pathy(current_path+1)-pathy(current_path))^2+(pathx(current_path+1)-pathx(current_path))^2)
		cx=cx/mag*3
		cy=cy/mag*3
		Color 100,200,100
		Rect cx-3,cy-3,6,6 
		If Abs(cx-pathx(current_path+1))<2 And Abs(cy-pathy(current_path+1))<2 Then				
			movement_started=0
			destX = Rand(10,1014)
			destY = Rand(10,758)
		End If
End Function 


it should get slightly better performance and function in precisely the same manner.