Scrolling of Picture Larger Than Visible Area in Monkey2

I finally today made my first Monkey2 app. I think it was someday in September I finally noticed that Monkey X has evolved into hugely more advanced programming language: Monkey2.

As I wrote in previous post I have an unfinished game project in Monkey X Pro. I think think I’ll finish it someday in Monkey2… I have one other little project in my mind to be done in Monkey2.

This is why I decided to try to make my first Monkey2 program today. In my older post I wrote a  short tutorial on scrolling a picture larger than visible area in Monkey X Pro keeping Android target in mind. In this post the same thing is written in Monkey2 to desktop.

The picture is scrolled with mouse, to change the code to touch screen, just change the mouse related code to touch related code.

Source code below:

' Scrolling of pre-loaded picture that is larger than the visible area in Monkey2
'

Namespace myapp

#Import "bg1280x960.png"
#Import "<std>"
#Import "<mojo>"

Using std..
Using mojo..

Class MyWindow Extends Window

	Field gfxBG:Image
	Field scrollX:Float
	Field scrollY:Float
	Field touchX:Float
	Field touchY:Float
	Field touchXD:Float
	Field touchYD:Float
	Field prevTX:Float
	Field prevTY:Float
	Field scroll:Bool
	
	Method New( title:String="Scrolling...",width:Int=800,height:Int=600,flags:WindowFlags=Null )
		Super.New(title,width,height,flags )
		gfxBG = Image.Load("asset::bg1280x960.png")
	End
	
	Method OnRender( canvas:Canvas ) Override
	
		App.RequestRender()

		If scrollX > 0 Then scrollX = 0
		If scrollX < -(1280 - 800) + 1 Then scrollX = -(1280 - 800) + 1
		If scrollY > 0 Then scrollY = 0
		If scrollY < -(960 - 600) + 1 Then scrollY = -(960 - 600) + 1
		
		canvas.DrawImage(gfxBG,scrollX, scrollY)
		
	End
	
	Method OnMouseEvent( event:MouseEvent ) Override
	
		touchX = Mouse.X
		touchY = Mouse.Y
					
		If Mouse.ButtonDown(MouseButton.Left) Then
			
			If scroll = True Then

				' touchXD & touchYD variables prevent the picture
				' to "jump", if scrolling is stopped and user then
				' touches somewhere on the screen				

				touchXD = touchX
				touchYD = touchY
				
			Else
				' if not scrolling, values below are set to zero,
				' because when the scrolling starts again, otherwise
				' scrollX & scrollY could change too much because of
				' values of previous scrolling
								
				touchXD = 0
				touchYD = 0
				prevTX = 0
				prevTY = 0
			
			Endif
			
			scroll = True
				
			scrollX = scrollX - (touchXD - prevTX)
			scrollY = scrollY - (touchYD - prevTY)
			
			prevTX = touchX
			prevTY = touchY

		Else
			scroll = False			
		Endif

	End
		
End

Function Main()

	New AppInstance	
	New MyWindow
	
	App.Run()
End

Video below demos the source (the text in the video is in video only, though):

Below is a video from the mentioned older blog post written in Monkey X Pro to Android:

Feel free to use my code as you wish.

 

 

%d bloggers like this: