Difference between revisions of "Super"
Jump to navigation
Jump to search
m (Fix yet another typo) |
|||
(3 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
− | <c>super</c> is a [[:Category:Keywords|keyword]] used to access base class methods from within a subclass method. | + | <c>super</c> is a [[:Category:Keywords|keyword]] used to access base class methods from within a subclass method. It is part of MiniScript's support for [[object-oriented programming]]. |
+ | |||
+ | |||
+ | <b>Example code:</b> | ||
+ | |||
+ | <ms> | ||
+ | //PLANT base class | ||
+ | PLANT = new Sprite | ||
+ | PLANT.image = Image.create(25, 25, color.lime) | ||
+ | PLANT.maxGrowth = 20 | ||
+ | PLANT.scale = 0 | ||
+ | PLANT.grow = function | ||
+ | self.scale = self.scale + .1 | ||
+ | if self.scale > self.maxGrowth then | ||
+ | self.scale = self.maxGrowth | ||
+ | return | ||
+ | end if | ||
+ | end function | ||
+ | |||
+ | //tree subclass | ||
+ | tree = new PLANT | ||
+ | tree.x = 480 | ||
+ | tree.y = 320 | ||
+ | |||
+ | tree.grow = function //trees function that would override grow within the PLANT base class | ||
+ | super.grow //reaches out to the PLANT base class and calls it | ||
+ | print "I grew!" //end of the function prints after super.grow is called. | ||
+ | end function | ||
+ | |||
+ | //pushes tree to default sprite display | ||
+ | display(4).sprites.push tree | ||
+ | |||
+ | //loop for tree.grow | ||
+ | while not tree.scale == tree.maxGrowth | ||
+ | tree.grow | ||
+ | wait .5 | ||
+ | end while | ||
+ | </ms> | ||
[[Category:Language]] | [[Category:Language]] |
Latest revision as of 22:11, 12 March 2022
super
is a keyword used to access base class methods from within a subclass method. It is part of MiniScript's support for object-oriented programming.
Example code:
//PLANT base class
PLANT = new Sprite
PLANT.image = Image.create(25, 25, color.lime)
PLANT.maxGrowth = 20
PLANT.scale = 0
PLANT.grow = function
self.scale = self.scale + .1
if self.scale > self.maxGrowth then
self.scale = self.maxGrowth
return
end if
end function
//tree subclass
tree = new PLANT
tree.x = 480
tree.y = 320
tree.grow = function //trees function that would override grow within the PLANT base class
super.grow //reaches out to the PLANT base class and calls it
print "I grew!" //end of the function prints after super.grow is called.
end function
//pushes tree to default sprite display
display(4).sprites.push tree
//loop for tree.grow
while not tree.scale == tree.maxGrowth
tree.grow
wait .5
end while
This article is a stub. You can help the MiniScript Wiki by expanding it.