-
Notifications
You must be signed in to change notification settings - Fork 8
/
Beginners - Class inside Class.monkey2
72 lines (62 loc) · 1.59 KB
/
Beginners - Class inside Class.monkey2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
' Class inside Class Example
#Import "<std>"
#Import "<mojo>"
Using std..
Using mojo..
Class master
' The s variable is used to create the slave with
Field s:slave
' This is a class inside a class
Class slave
' Fields
Field x:Int,y:Int
' New method
Method New(x:Int,y:Int)
' Assign self x and y with new x'es and y'es
Self.x = x
Self.y = y
End Method
' Draw method which draws on canvas
Method draw(canvas:Canvas)
canvas.DrawCircle(x,y,100)
End Method
End Class
' new method for main class
Method New(x:Int,y:Int)
' Assign into s from class inside the main
' class.
s = New slave(x,y)
End Method
' update class in main class which passes
' the canvas(draw)
Method update(canvas:Canvas)
s.draw(canvas)
End Method
End Class
' make a global variable which we put the master
' class into. We create it here also.
Global mymaster:master = New master(200,200)
Class MyWindow Extends Window
Method New()
End Method
Method OnRender( canvas:Canvas ) Override
App.RequestRender() ' Activate this method
' Here we call the mymaster update method
' canvas which we draw on is passed into the call
mymaster.update(canvas)
' We can modify variables from classes in classes
' also.
' mymaster is the variable we created the master class
' with.
' s is the variable inside the mymaster class we created
' the slave class with.
mymaster.s.x = Rnd(Width)
' if key escape then quit
If Keyboard.KeyReleased(Key.Escape) Then App.Terminate()
End Method
End Class
Function Main()
New AppInstance
New MyWindow
App.Run()
End Function