Page 2 of 2 FirstFirst 12
Results 11 to 20 of 31

Thread: Dynamic in-game object creation and manipulation

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Quote Originally Posted by phibermon View Post
    please post your code for how you setup the array of bullets
    I'm using fixed length arrays:
    Code:
    bullets: array [0..100] of TBullet;
    Then in a Timer event I create, move and destroy the bullets:
    Code:
    if (plshoot=true) and (plcanshoot=true) then
      begin
             abullet:=TBullet.create(Player.Left,Player.Top,bulletpic);
             bullets[numofbullets]:=abullet;
             inc(numofbullets);
      end;
    
    
      if numofbullets>=1 then
      begin
           for i:=0 to numofbullets-1 do
           begin
            if bullets[i]<>nil then
            begin
                bullets[i].yplace:=bullets[i].yplace-4;
                if bullets[i].yplace<10 then
                begin
                     freeandnil(bullets[i]);
                     if i=numofbullets-1 then
                        dec(numofbullets)
                     else
                     begin
                          for j:=i+1 to numofbullets-1 do
                              bullets[j-1]:=bullets[j];
                          dec(numofbullets);
                     end;
                end;
            end;
           end;
      end;
    (I have already corrected the first variable of the loops from 1 to 0, as you wrote.)
    When I press the fire button, the bullets are only flash at the top of the player. :-/

  2. #2
    Is your numofbullets global variable or is it declared inside Timers OnEvent method.
    If it is declared in an OnTimer event method that means that it is cleared after ending of each Timer cycle and then recreated in another cycle with default value which is 0.

  3. #3
    The numofbullets is a global variable; only the i and j variables are declared in the timer.

  4. #4
    Hello and happy new year, everybody!


    At last, my first game with Lazarus is done: a small space shooter, with some enemies, one boss and power-ups.
    It's name is "Space Defender" and you can download from here:
    http://www.programozzunk.ucoz.hu/laz...k/spacedef.zip
    Unfortunately it has some bugs:
    - the appearance of bullets of enemies sometimes happens in wrong place,
    - the sprites of the spacehips are flashing,
    - sometimes the game throw you out, when the player is rebirth at the boss and you press buttons.
    In spite of all this, I hope that you will enjoy this game, but if you will find the solutions for these bugs, please tell me.

  5. #5
    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.

  6. #6
    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?

  7. #7
    Maybe I find what is wrong in my code: I have to put the drawings in the Timer event, not in Formpaint. Ehhh... I need more experience with Lazarus.
    Last edited by Tomi; 21-12-2015 at 01:29 PM.

Page 2 of 2 FirstFirst 12

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
  •