About Monkey 2 › Forums › Monkey 2 Programming Help › Referencing New (Constructor)
This topic contains 4 replies, has 3 voices, and was last updated by 
 Matthew Smith
 2 years, 9 months ago.
- 
		AuthorPosts
 - 
		
			
				
June 25, 2016 at 5:17 am #1215
I’m wanting to essentially overload New but rather than having to duplicate code, I’d like to pass onto another New like such:
Monkey1234567891011121314151617181920212223242526272829303132333435363738Class SpriteField Blend:=BlendMode.AlphaField Color:= New Color( 1,1,1,1 )Field Images:Image[]Field Enabled:=TrueField Frame:=0.0Field Rotation:=0.0Field Scale:=New Vec2f( 2,2 )Field Position:=New Vec2fMethod New(image:Image,x:Int,y:Int)Self.New(New Image[](image),x,y)EndMethod New(images:Image[],x:Int,y:Int)' SetSelf.Images=imagesSelf.Position.X=xSelf.Position.Y=y' Set image propertiesFor Local i:=0 Until Self.Images.LengthSelf.Images[i].Handle=New Vec2f( .5,.5 )Next' StoreSprites.Push(Self)EndMethod Update()EndMethod Render(canvas:Canvas)canvas.BlendMode=Self.Blendcanvas.Color=Self.Colorcanvas.DrawImage( Self.Images[Self.Frame],Self.Position,-Self.Rotation,Self.Scale )EndEndAs you can see the first New is calling the second New. Currently I’m getting the following:
Monkey123456789MX2CC V0.010***** Building app 'D:/Dev/Monkey/Game/Game.monkey2' *****Parsing...Semanting...Translating...Caught signal:Memory access violationDone.Been quite a while since I did any Blitz/Monkey coding so a bit rough in remembering this stuff! Too much C# nowadays. Thanks Playniax for the example.
June 25, 2016 at 6:39 am #1217In Java you can do something like this(pun!) with ctor chaining:
[/crayon]Monkey123456789[crayon-5cba14959a447619835852 inline="true" ]class Sprite {public Sprite(float x, float y){this(x, y, 3);}public Sprite(float x, float y, int lives){}}With Monkey I think you need to create a common method or function:
[/crayon]Monkey1234567891011121314151617[crayon-5cba14959a44c811032891 inline="true" ]Class Player Extends SpriteField lives:IntMethod New(x:Float, y:Float)Init(x, y, 3)EndMethod New(x:Float, y:Float, lives:Int)Init(x, y, lives)EndMethod Init(x:Float, y:Float, lives:Int)Self.x = xSelf.y = ySelf.lives = livesEndEndIt would be nice to have ctor chaining.
June 26, 2016 at 3:33 am #1233Thanks for your response – yeah would be a nice to have! But no probs.
C# has a similar option using both this (Self) and base (Super):
Monkey12345678910class Sprite {public Sprite(float x, float y) : this(x, y, 3){}public Sprite(float x, float y, int lives){}}June 26, 2016 at 3:48 am #1235Unfortunately, you’ll currently have to use the ‘Init’ approach for this particular code.
The problem is that you can’t currently have ‘complex’ parameter expressions when you chain new’s together (ie: the ‘New Image[]( image )’ ) – mainly because you can’t in C++, and mx2 New currently maps directly to c++ ctors. Which seemed like a good idea at the time but has proven to have some serious limitations.
I do plan to eventually fix this though, hopefully soon!
June 26, 2016 at 7:20 am #1239Ok – thanks Mark!
 - 
		AuthorPosts
 
You must be logged in to reply to this topic.