When executing a superclass constructor, my code executes a subclass constructor. To avoid an infinite loop, I don't want the subclass constructor to execute the superclass constructor in this case. How do I avoid this? In particular, running the player class constructor prompts the user for selecting a file which determines which subclass to load the file as: Pfighter or Pwizard. The subclass, Pfighter, is a subclass of the player and character superclasses. This causes the player constructor to execute which causes an infinite loop due to the fact that the subclass can't pass information about the previous iteration to the superclass during instantiation. Again, not desirable. Can I retype the object to the lower class without rerunning the superclass constructor?
classdef character < handle
properties
Name
Class
Level
Team
Marker
HP
MP
MPcost
Speed
Gun
Range
Damage
Spellbook
card
HPmax
status
ID
position
positionold
legend
scatter
outofbounds = false
levelup = false
AIplayer
end
methods
function obj = character[varargin]
if nargin == 0
g = game['settings'];
n = 1;
end
obj.status = status[];
end
function obj = getcard[obj]
obj.card = card[obj.AIplayer];
fieldnames = fields[obj.card];
for j=1:size[fieldnames,1]
obj.[fieldnames{j}] = obj.card.[fieldnames{j}];
end
if obj.AIplayer
if strcmp[obj.Class,'Fighter']
obj = Cfighter[obj];
else
obj = Cwizard[obj];
end
else
if strcmp[obj.Class,'Wizard']
obj = Pwizard[obj];
else
obj = Pfighter[obj];
end
end
s = settings;
switch obj.Team
case 'red'
obj.position = [-s.gridsize,-s.gridsize];
case 'green'
obj.position = [s.gridsize,s.gridsize];
case 'blue'
obj.position = [-s.gridsize,s.gridsize];
case 'yellow'
obj.position = [s.gridsize,s.gridsize];
end
if obj.AIplayer
obj.position = obj.position + [3*rand[],3*rand[]];
end
obj.positionold = obj.position;
end
end
end
classdef player < character
properties
levels
XP
end
methods
function obj = player[obj]
obj.AIplayer = false;
g = game['settings'];
levelsfile = file[[g.settings.LevelsPath g.settings.Levelfile],'int'];
obj.levels = levelsfile.table;
if ~exist[obj.card]
obj.getcard;
end
obj.getXP;
end
function player = getXP[player]
if exist['XP.mat','file']
load['XP.mat']
else disp['No experience mat file found.'];
return
end
XPexist = false;
for j = 1:size[experience,1]
if strcmp[experience{j,1},player.Name]
player.XP = experience{j,2};
XPexist = true;
end
if j==size[experience,1] && ~XPexist
player.XP = 0;
end
end
end
end
end
classdef Pfighter < player & fighter
properties
end
methods
function obj = Pfighter[obj]
obj.Class = 'Pfighter';
end
end
end