I’m making a game where I have must have some objects like weapons and characters, which I in the end would like to get visual as a sprite or movieclip
Should I write the logic for the objects in several classes and then make a reference to the object in a sprite class like the following :
public class WeaponModel
{
public damage:Int = 8;
public function Weapon(){
//Do the constructor code
}
public function get damage():int{
return this.damage;
}
}
public class WeaponSprite extends Sprite
{
var weaponModel:WeaponModel;
public function WeaponSprite(weaponModel:WeaponModel){
this.weaponModel = weaponModel;
}
public function get weaponModel():WeaponModel{
return this._weaponModel;
}
}
And then when I need to do damage just call the WeaponSprite’s method weaponModel() when I need to acces the methods or should I just merge the two together into one class.?
I would avoid passing a reference to the model to a view. A more decoupled approach would be to pass a model id to the view. The provide access to the model id through a getter. In general you want to keep knowledge of models out of views and knowledge of views out of models.
Below is an example of how a controller class might manage the relationship between views and models.
Another thing to consider is how large of an application you are building and how many people will be working on it. When building a quick prototype, you may not be concerned with following strict OOP patterns. However, the larger and more complex a project gets, the more likely it is that a framework or specific design patterns will be of value to you.
If you are interested in learning a framework that facilitates consistent and flexible designs, consider looking at Robot Legs.