我在Matlab中使用OOP来制作由矩形组成的播放器。我想按下一个键时矩形移动,所以我做了一个处理播放器的课程。我可以绘制矩形,但我只能使用一种方法无法移动。相反,我收到错误“Undefined function or variable'PlayerShaft'。”我想要传递或保存矩形错误。我认为它的句柄会被类本地保存,以便我可以访问它并改变它,但似乎并非如此。我已经查看了Matlab如何处理类,但我似乎无法找到有关此特定问题的任何信息。我究竟做错了什么?这是迄今为止的代码:
classdef Player
properties
axesWidth = 100;
axesHeight = 100;
ShaftPosX = 50;
ShaftPosY = 50;
ShaftWidth = 10;
ShaftHeigh = 20;
BallOffset = 5;
end
methods
function obj = Player()
PlayerShaft = rectangle('Position',[obj.ShaftPosX obj.ShaftPosY obj.ShaftWidth obj.ShaftHeigh],...
'Curvature',1,...
'FaceColor','r',...
'Visible','on');
disp('It is on');
disp(obj.ShaftPosX)
end
function DrawPlayer(obj)
for i=1:20
obj.ShaftPosX = obj.ShaftPosX +1;
set(PlayerShaft,'Position',[obj.ShaftPosX obj.ShaftPosY obj.ShaftWidth obj.ShaftHeigh]);
%PlayerShaft.Position = [obj.ShaftPosX obj.ShaftPosY obj.ShaftWidth obj.ShaftHeigh];
pause(0.1);
disp(obj.ShaftPosX)
end
end
end
end