Results 1 to 10 of 31

Thread: Dynamic in-game object creation and manipulation

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Does the order really matter for bullets and possibly glowing particles and the like? I find it redundant to, say if index 10 is removed from list of 100 bullets you would move all remaining 90 or so of them 1 backwards... It is simpler to move the last index in place of the removed one. 1 operation vs 90 operations. In order for this to work, you need to use a "downto" in the for loop, going from the last item to 0.

    Code:
    for b:=bullets-1 downto 0 do
      with bullet[b] do begin
        Move;
        dec(AliveTime);
        if AliveTime < 0 then begin
          dec(bullets);
          //bullet[b].Free; // Free it here if you must. Not needed if using records
          bullet[b]:=bullet[bullets]; // Replace current bullet with last one.
        end;
      end;
    New bullets will then always be added to the end.

  2. #2
    Hello User137!


    My main problem the appearance of enemy's bullets, which happening at wrong place. Its may has to do with moving and deleting of the bullets?
    I do it as follows:


    Code:
    if numofbullets>0 then
           begin
               i:=0;
               while i<numofbullets-1 do
               begin
                if bulletarray[i]<>nil then
                begin
                    if (bulletarray[i].xplace>ClientWidth) or (bulletarray[i].xplace<1) or (bulletarray[i].yplace>ClientHeight) or (bulletarray[i].yplace<1) or (collwithplayer(bulletarray[i].xplace,bulletarray[i].yplace)=1) then
                    begin
                        freeandnil(bulletarray[i]);
                        if i=numofbullets-1 then
                                dec(numofbullets)
                        else
                        begin
                                for j:=i+1 to numofbullets-1 do
                                    bulletarray[j-1]:=bulletarray[j];
                                dec(numofbullets);
                                dec(i);
                        end;
                    end else
                        begin
                         bulletarray[i].yplace:=bulletarray[i].yplace+4
                         MainWindow.Canvas.Draw(bulletarray[i].xplace,bulletarray[i].yplace,bulletarray[i].itsimage);
                        end;
                end;
                inc(i);
               end;
           end;

    Maybe it is wrong?

  3. #3
    You don't have a semicolon at the end of
    Code:
    bulletarray[i].yplace:=bulletarray[i].yplace+4
    And that's all i can see is wrong with the code. If you fire just 1 bullet, it starts from wrong place? Then it sounds like the mistake is the code that fires it.

  4. #4
    The player's bullets starts from right place, but the enemy's bullets sometimes comes from wrong places, as you can see in my program which can be download from 20th post.

  5. #5
    It's over 800 lines of code and not in english so it's difficult to read.

    Here is something odd:
    Code:
    function TFoablak.pontirany(x1,y1,x2,y2: integer): word;
    var szam: integer;
    begin
        szam:=round(arctan2(y2-y1,x2-x1));
        if szam<0 then
                szam:=round(szam+2*pi);
        pontirany:=round(360-(szam*180)/pi);
    end;
    You should set szam: single; instead of integer, and not round it directly from arctan2(). There is too much precision lost for angle (if you have radians as integer there are only about 6 different angles in the whole 360 range.).

    Also
    Code:
    if numofbullets>0 then  // < This line is not needed.
      begin
        i:=0;
        while i<numofbullets-1 do
    Lets see what happens if numofbullets = 0:
    Code:
    i:=0;
        while 0<-1 do
    0 is not < -1 so it never enters the while.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •