Recent

Author Topic: Controlling Multiple Objects  (Read 736 times)

S4MD!ng

  • New member
  • *
  • Posts: 10
  • ALevel Student (Not Great At Pascal)
Re: Controlling Multiple Objects
« Reply #15 on: February 11, 2019, 02:57:22 am »
OK, so i decided to go back to basics and try and make something that would do something visual, so that i know that it is doing something. So i changes ReduceLife() to also move the TDart up by 10. also, everytime that i press TButton1, it creates a new TDart, adds it to an array, and then calls, Reduce life for each item in the array.

It works to some extent, when i press the button, it generates a Tshape, and moves it up by 10. but for every subsequent button press, it moves the fist object up by 10, but none of the others created by the same button that worked for the first one!
Code: Pascal  [Select]
  1. {Nothing has changes elsewhere}
  2. procedure TDart.ReduceLife();
  3. begin
  4.   if FLife > 0 then
  5.     Dec(FLife);      //decreases FLife
  6.     Self.Top:= Self.Top-10;
  7. end;
  8.  
  9. procedure TForm1.Button1Click(Sender: TObject);  //Lets Me Create LOTS Of Shapes on the click of a button
  10. var
  11.   s1: TDart;
  12.   j:integer;
  13. begin
  14.   s1:= TDart.Create(Self);            //Self Refers to TForm1 (Creates the Shape)
  15.   s1.FSetGradient((Player.Top-s1.FY1)/(Player.Left-s1.FX1));         //Runs the Procedure Gradient Target Objects Coordinates
  16.   TDartList[i] := s1;
  17.   for j := 0 to High(TDartList) do
  18.   begin
  19.     TDartList[j].ReduceLife;
  20.   end;
  21.   i:=i+1;
  22.   SetLength(TDartList, (High(TDartList)+1))
  23. end;
  24.  
  25. begin
  26.   i := 0;
  27.   SetLength(TDartList, 1);
  28. end.
Sam

jamie

  • Hero Member
  • *****
  • Posts: 1195
Re: Controlling Multiple Objects
« Reply #16 on: February 11, 2019, 11:19:18 pm »
The array is changing so the HIGH intrinsic function may not be work.

 Try using Length(TDartList)-1 instead of High(TDartList)