object movement

Monkey Targets Forums/Android/object movement

Jesse(Posted 2011) [#1]
anyone care to post how to move objects. I tried keydown: left,right,up,down but none work. I have a Droid phone but nothing in the key pad seem to work. I am wondering if I am going to have to write my own functions using TouchDown, TouchX and TouchY. Is there an alternative I can use?


AdamRedwoods(Posted 2011) [#2]
Since not all Android devices have built-in keyboards, I think it's recommended to create your own joypad in the display, and do your own touchcalls.


Jesse(Posted 2011) [#3]
Thanks Adam. Now I am going to have to figure out a good way to implement it.


AdamRedwoods(Posted 2011) [#4]
This is how I envision it, but any interpretation is welcome:
http://www.youtube.com/watch?v=MYehFrsiCcg


therevills(Posted 2011) [#5]
Have a look here:

http://www.monkeycoder.co.nz/Community/posts.php?topic=396


Jesse(Posted 2011) [#6]
Thanks guys. they are both good and very similar. I am going to duplicate the one Adam posted with graphic images using Samah's as the template.


Jesse(Posted 2011) [#7]
I made one that returns 1 and 0 for x and y good for horizontals, verticals, and diagonals. real basic not a proper module but it works:
Strict

Import Mojo

Class Stick
	Field offx:Float
	Field offy:Float
	Field x:Float
	Field y:Float
	Field vx:Float
	Field vy:Float
	Field length:Float
	Field active:Int
	Field oldActive:Int
	Field radius:Float
	Field deadZone:Float
	Field maxR:Float
	
	Function create:Stick(offx:Int,offy:Int,radius:Float,deadZone:Float,maxShift:Float)
		Local s := New Stick
		s.offx = offx
		s.offy = offy
		s.x = 0
		s.y = 0
		s.deadZone = deadZone
		s.maxR = maxShift
		s.active = False
		s.oldActive = False
		s.radius = radius
		Return s
	End Function

	Method update:Void()
		x=0
		y=0
		vx = 0
		vy = 0
		length = 0
		If MouseDown(0)
			vx = MouseX() - offx
			vy = MouseY() - offy
			length = Sqrt(vx*vx+vy*vy)
			If oldActive = True
				active = True
				If length < deadZone
				Elseif length > maxR
					Local dx:Float = vx/length
					Local dy:Float = vy/length
					length = maxR
					x = dx*length
					y = dy*length
				Else
					x = vx
					y = vy
				Endif	
			Elseif length < deadZone
				active = True
			Else
				active = False
			Endif
		Else
			active = False
		Endif
		oldActive = active
	End Method

	Method getdx:Int()
		If length > 0
			Local l1:Float = length/5*2
			If( x >  l1) Return  1
			If( x < -l1) Return -1
		Endif
		Return 0 
	End Method
	
	Method getdy:Int()
		If length > 0
			Local l1:Float = length/5*2
			If(y > l1) Return  1
			If(y < -l1) Return -1
		Endif
		Return 0
	End Method
	
 	Method display:Void()
		Local px:Float = offx + x
		Local py:Float = offy + y
		SetColor 0,255,0
		DrawOval px - radius,py-radius,radius*2,radius*2
	End Method
	
End Class

Class Controller
	Field x:Float
	Field y:Float
	
	Field vx:Float
	Field vy:Float
	
	Field radius:Float
	Field deadZone:Float
	Field maxR:Float
		
	Field stick:Stick
	

	Function create:Controller(x:Int,y:Int,radius:Int,maxr:Float,deadZone:Int)
		Local c := New Controller
		c.x = x
		c.y = y
		c.radius = radius
		c.stick = Stick.create(x,y,20,deadZone,maxr)
		Return c
	End Function
	
	Method update:Void()
		stick.update()
	End Method

	Method getdx:Int()
		Return stick.getdx() 
	End Method
	
	Method getdy:Int()
		Return stick.getdy()
	End Method
	

	Method display:Void()
		SetColor 200,0,0
		DrawOval x-(radius/2),y-(radius/2),radius,radius
		stick.display()
	End Method

End Class

Class Game Extends App
	Field controller:Controller

	Method OnCreate:Int()
		controller = Controller.create(100,100,64,32,20)
		SetUpdateRate(30)
		Return 0
	End Method
	
	Method OnUpdate:Int()
		controller.update()
		Return 0
	End Method
	
	Method OnRender:Int()
		Cls()
		controller.display()
		DrawText 	"x ="+controller.getdx(),10,10
		DrawText "y ="+controller.getdy(),10,30
		DrawText "angle ="+Int((ATan2(controller.stick.y,controller.stick.x)+360.0) Mod 360.0),10,50
	End Method

End Class

Function Main:Int()
	New Game
End Function