Need MoveImageTo function

Monkey Forums/Monkey Programming/Need MoveImageTo function

Tri|Ga|De(Posted 2011) [#1]
Does anyone have a function to move an image?

Like

MoveImageTo(theImage, xpos, ypos)

It must me animated, see it move to new point!

I know I could make it myself, but if some already has it then......


Yahfree(Posted 2011) [#2]
You mean basic tweening? That should be easy. Here's some code:

Class Moveable
	Const STOPWITHIN:Float = 5'Must be higher if higher speed
	Field x:Float,y:Float
	Field dx:Float,dy:Float
	Field mx:Float,my:Float,mc:Float
	Field moving:Int
	Field speed:Float = 1
	Method Move(nx:Float,ny:Float)
		dx = nx
		dy = ny
		moving=True
		mx = Abs(dx-x)
		my = Abs(dy-y)
		mc = Sqrt((mx*mx)+(my*my))
	End Method
	Method Update()
		If moving
			x+=(mx/mc)*speed
			y+=(my/mc)*speed
			If (Abs(dx-x) < STOPWITHIN) And (Abs(dy-y) < STOPWITHIN)
				x = dx
				y = dy
				moving = False
			End If
		End If
	End Method
End


Just extend whatever object you want to move with this class, and call "Super.Update()" in that objects update class. The call the method Move() when you want to move it.

You can play with the speed variable too. But make the STOPWITHIN variable match it.. because if you have something going 20px/frame, it may skip over the STOPWITHIN zone, and just keep going


Yahfree(Posted 2011) [#3]
Here's a Moveable image class:

Class MImage Extends Moveable
	Field img:Image
	Method New(I:Image,_x:Float,_y:Float)
		img = I
		x= _x
		y = _y
	End Method
	Method Draw()
		DrawImage img,x,y
	End Method
End Class


You'd do something like this:
Local Logo:MImage = New MImage(image,x,y)



Hima(Posted 2011) [#4]
There's a tween example in Monkey folder. It come with many ease functions too so you might wanna take a look into that :)


Tri|Ga|De(Posted 2011) [#5]
Yahfree:

I'm really tried to implement your code but somehow I can not get it to work.

Do you have an example with fx. a DrawRect that get moved?

I'm a bit rusty, have not programmed in BMax for many years.
Just C# and Obj-C


degac(Posted 2011) [#6]
from the sample BASIC GAME