Class:function inside method
Monkey Forums/Monkey Programming/Class:function inside method
| ||
Hello everybody. It seems i haven't understood functions properly. I have this chunk of code: [code] Import mojo Class ship Field posx Field posy Field speed '------------------------------- Method init() posx=100 posy=100 speed=5 End '------------------------------- Method update() posx=posx posy=updown(posy,speed) End '------------------------------- Method render() DrawCircle (posx,posy,40) End '------------------------------- End Global player := New ship Function createplayer() player.init() End Function playerupdate() player.update() End Function playerrender() player.render() End Function updown(y,s) y=y + s 'start the movement If y >= 460 'check against bottom border y=y 'stop movement s *= -1 'invert the movement by inverting the speed Endif If y < = 20 'check against top border y = y 'stop movement s *= -1 'invert the speed Endif End Class game Extends App Method OnCreate() createplayer() SetUpdateRate(60) End Method OnUpdate() playerupdate() End Method OnRender() Cls playerrender() End End Function Main() New game End [\code] What i want to do is having an external function to calculate movements instead of declaring every movement into a class method, to make the code tidier. Is it even possible? Is it the better way to do it? I am pretty sure i am missing something simple here, but since i am learning the language i just can't get over it. Thank you. |
| ||
Import mojo Class ship Field posx Field posy Field speed '------------------------------- Method init() posx=100 posy=100 speed=5 End '------------------------------- Method update() posx=posx posy=updown(posy,speed) End '------------------------------- Method render() DrawCircle (posx,posy,40) End '------------------------------- End Global player := New ship Function createplayer() player.init() End Function playerupdate() player.update() End Function playerrender() player.render() End Function updown(y,s) y=y + s 'start the movement If y >= 460 'check against bottom border y=y 'stop movement s *= -1 'invert the movement by inverting the speed Endif If y < = 20 'check against top border y = y 'stop movement s *= -1 'invert the speed Endif End Class game Extends App Method OnCreate() createplayer() SetUpdateRate(60) End Method OnUpdate() playerupdate() End Method OnRender() Cls playerrender() End End Function Main() New game End Sorry i used the wrong forum code.... |
| ||
You can do this if you want to pass your variable player into each of those functions, such as:Function createplayer(player:Ship) player.Init() End Then inside Oncreate you'd call: createplayer(player) But you don't have to do this and I'm not sure what purpose it serves unless you are making a function that will manage all your method calls for player based on some sort of criteria. Just call the methods directly in OnCreate(), OnUPdate(), and OnRender(), such as: Method OnCreate() player.Init() SetUpdateRate(60) End |
| ||
I would first of all recomend that you actually use methods within the class to do this , not only is it far more modular and cleaner but its a lot less hassle. At the moment your Play is global so there is nothing stopping you from simply setting its x,y position inside an external function. Player.posx = 10 I think what you should do isntead however is something like this. [monkeycode] Import mojo Class ship Field posx Field posy Field speed Method movedown() self.posy+=self.speed end Method boundCheck() 'in here you would do that boundery check, 'if posy > 420 then set self.speed = -self.speed End '------------------------------- End Global player := New ship Class game Extends App Method OnUpdate() playerupdate() if KeyDown(KEY_A) then Player.movedown() End Method OnRender() Cls playerrender() End End Function Main() New game End [/monkeycode] thats not a complete source but should show what I mean. |
| ||
What i was trying to do is making a function that will execute some code and return a result that is going to be processed inside a method, so i don't have to type the same code to have similar classes doing the same thing (hope it make sense). Example: class player class enemy class bullet class dog All those classes sooner or later will need a boundary check, an up&down, a jump etc etc. So my idea was to have all those classes with the same methods for updating, rendering etc, and inside those methods have a function that does the calculations for updating etc. All of this is purely for learning the language and it's possibilities (i was also thinking about inheritance of classes...but it's probably way too early for me!). |
| ||
What you want to do is use Extend to add that code to those classes. for example. sec.. getting daughter from school brb |
| ||
Create a generic GameObject class that stores the common information then use Extends to make each different type.Class GameObject Field xPos:Float Field yPos:Float Method New(xPos:Float, yPos:Float) Self.xPos = xPos Self.yPos = yPos End End Class Player Extends GameObject Field width:Float Field height:Float Method New(xPos:Float, yPos:Float, width:Float, height:Float) Super.New(xPos, yPos) Self.width = width Self.height = height End End Then of course any other methods can call the GameObject methods with Super, and have their own parameters, and do their own things, while also storing the base information in the class it extends. |
| ||
[monkeycode] Class world Field leftwall:Int Field rightwall:Int Method boundcheck() 'bound checking code is done once. End Method End Class Class dog Extends world Field x:Int Field y:Int Method update() Self.boundcheck() End Method draw() End End Class Function Main:Int() Return 0 End [/monkeycode] Sorry thats kinda rushed, but you get the idea, you would write your boundary checking code in the world class, and then as each new game object extends that world class it would get that boundry checking method. You would only need to write your method once, and then each new class you add will use it. You could for example put field x,y inside world, as a world has a position, and then each new class/object or player that extends world would then inherit those fields.. hope this helps last thing I wana do is confuse you. |
| ||
Thanks, it make sense. I was wondering if extending the main class was the right way to do it. It's just that for some strange reason i am still tied to a heavy use of functions... ok time to study inheritance. Thank you all. |
| ||
Glad I am able to help out, its normally me asking for help so its nice to pay it forward. |
| ||
In fact, this is a pretty standard example of the sort of thing inheritance is good for. |