如何防止Firemonkey复合组件中的子组件重复? [英] How can I prevent duplication of sub components in Firemonkey compound component?

查看:76
本文介绍了如何防止Firemonkey复合组件中的子组件重复?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个从TDummy派生的复合组件。组件来源为:

I am trying to write a compound component which is derived from TDummy. The component source is:

  TMyObjectType=(otCube,otSphere);
  TMyGameObject=class(TDummy)
  private
    FObj:TCustomMesh;
    FMyObjectType: TMyObjectType;
    procedure SetMyObjectType(const Value: TMyObjectType);
  public
    constructor Create(AOwner:TComponent);override;
    destructor Destroy;override;
    property MyObjectType:TMyObjectType read FMyObjectType write SetMyObjectType;
  end;

{ TMyGameObject }

constructor TMyGameObject.Create(AOwner: TComponent);
begin
  inherited;
  MyObjectType:=otCube;
end;

destructor TMyGameObject.Destroy;
begin
  FObj.Parent.RemoveObject(FObj);
  FreeAndNil(FObj);
  inherited;
end;

procedure TMyGameObject.SetMyObjectType(const Value: TMyObjectType);
begin
  FMyObjectType := Value;
  if(Assigned(FObj))then begin
    FObj.Parent.RemoveObject(FObj);
    FreeAndNil(FObj);
  end;
  case FMyObjectType of
    otCube: FObj:=TCube.Create(Self);
    otSphere: FObj:=TSphere.Create(Self);
  end;
  FObj.SetSubComponent(True);
  FObj.Parent:=Self;
end;

将MyObjectType更改为otSphere。

after I register the component and put one instance on a TViewport3D in the code of a Tbutton I try to change the MyObjectType to otSphere.

MyGameObject1.MyObjectType:=otSphere;

但似乎没有任何反应。所以我写了一段代码作为休假代码。

but it seems there is nothing happening. So I wrote a piece of code as fallow.

procedure MyParseObj(obj:TFmxObject;var s:string);
var
  i: Integer;
  a:string;
begin
  s:=s+obj.ClassName+'(';
  a:='';
  for i := 0 to obj.ChildrenCount-1 do begin
    s:=s+a;
    MyParseObj(obj.Children.Items[i],s);
    a:=',';
  end;
  s:=s+')'
end;

并在另一个按钮中调用它。

and call it in another button.

procedure TForm1.Button2Click(Sender: TObject);
var s:string;
begin
  s:='';
  MyParseObj(myGameObject1,s);
  ShowMessage(s);
end;

结果很奇怪。
,如果我按下button2的结果是:TMyGameObject(TCube(),TCube())

the result was strange. if I press the button2 result is: TMyGameObject(TCube(),TCube())

,当我按下button1之后,按下button2的结果是:TMyGameObject(TCube(),TSphere())

and when I press the button1 and after that press button2 result is: TMyGameObject(TCube(),TSphere())

为什么在我的对象中有两个TCustomMesh作为子对象? (TCube和TSphere是从TCustomMesh派生的)
我该如何解决?

why there is two TCustomMesh as child in my object? (TCube and TSphere are derived from TCustomMesh) how can I fix this?

我还有另一个测试。如果我没有在设计时创建对象,它将正常工作。如果我将TMyGameObject实例放在设计时间中,则会发生问题。

and there is another test that I performed. if I create the object not in design time it work properly. problem happens if I put an instance of TMyGameObject in design time.

推荐答案

保存表单时(从IDE中)所有控件及其所有子级均被保存。如果您的控件创建了自己的子代,则需要设置Stored = False来防止它们被IDE流化。

When you save a form (from the IDE) all controls and all their children are saved. If your control creates it's own children then you need to set Stored = False to prevent them being streamed by the IDE.

这篇关于如何防止Firemonkey复合组件中的子组件重复?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆