Page 1 of 2 12 LastLast
Results 1 to 10 of 17

Thread: Problem with sprite engine. Not working right

  1. #1

    Problem with sprite engine. Not working right

    Code:
    if (R5Direction='RIGHT')then
    begin
    X:=x+Speed1*dx;
    end;
    
    if (R5Direction='LEFT')then
    begin
    X:=X-Speed1*dx;
    end;
    
    
             if (X>=GameAreaX2-Image.width)then //hits right wall
             begin
             R5Direction:='LEFT'; //sets all 8 sprites to go left
             end;
    
             if &#40;X<=GameAreaX1&#41;then // hits left wall
             begin
             R5Direction&#58;='RIGHT';
             end;
    oK what we have here is a space invader type game. The 8 sprites bounce from wall to wall like the classic game does. If it hits the left wall, the global variable is set to RIGHT and vice versa for the other wall.

    Problem. When first sprite hits left wall, instead of just turning right at same time as the other sprites, it jerks and then goes out of sync with the other sprites. After a while the gap between sprite 1 and sprite 2 becomes huge. The faster the sprite moves the larger the gap becomes each time the wall is hit.

    The code dictates that all sprites should move left and right at the same time, but srpite 1 is being treated differently to sprite 2-8

    Sprite 8 hits the Right wall but everything stays in sync. It only happens with sprite 1 on the left wall if the code is exactly as above.

  2. #2
    Legendary Member cairnswm's Avatar
    Join Date
    Nov 2002
    Location
    Randburg, South Africa
    Posts
    1,537

    Problem with sprite engine. Not working right

    What seems to be happening is that the first sprite moves right and then encounters the wall. After sprite 1 has touched the wall all the other sprites move left (but sprite 1 has already moved right).

    So you need to change the logic so that the global var can only be changed at the end of a move step and not in the middle of it. The easist way to do this is with a temp var that gets changed as above and then at the end of the frame set the global var to the value of the temp var.
    William Cairns
    My Games: http://www.cairnsgames.co.za (Currently very inactive)
    MyOnline Games: http://TheGameDeveloper.co.za (Currently very inactive)

  3. #3

    Problem with sprite engine. Not working right

    no that is the thing, sprite 1 doesn't move right (It moves left and encounters left wall), it actually jerks and ends up behind the other sprites, meaning it has moved left or simply stalled. Also, since I have put the movement portion of the code above the code that changes the global variable, shouldnt they all move together on the next tick?

  4. #4

    Problem with sprite engine. Not working right

    I guess you process your sprites in a for loop...

    Code:
    for spritecount &#58;= 1 to 8 do
    begin
      sprite.process;
    end;
    in SpriteProcess I guess you do the code you posted...

    Then I ask you:

    What happens if sprite 1 hits the left wall?

    1. it moves left
    2. it sets direction to right

    Now, sprite 1 is the first one in your processing, it hits the wall and sets direction for all sprites to right... but the sprite itself already wandered to the left... the following 7 in this loop will wander right!

    So thats your problem I guess

    This happens only with the first sprite (and not with the 8th), because the 8th does not affect the other sprites in this loop turn. The first does!

    You have to check the wallhit BEFORE you set the new position... and then decide what to do. This way also the first sprite will be affected by the direction change....

    Got the point?
    <a href="http://www.greatgamesexperiment.com/game/Valgard/?utm_source=gge&amp;utm_medium=badge_game"><img border="0" alt="GGE" title="GGE" src="http://static.greatgamesexperiment.com/badge/game/valgard/gge400x56.png"></a>

  5. #5

    Problem with sprite engine. Not working right

    sprite 1 can't hit the right wall, unless all 7 other sprites are dead. As in space invaders, they are ina line. I will supply a demo and see what you make of it

  6. #6

    Problem with sprite engine. Not working right

    Updated my post and corrected the directions... the idea is the same
    <a href="http://www.greatgamesexperiment.com/game/Valgard/?utm_source=gge&amp;utm_medium=badge_game"><img border="0" alt="GGE" title="GGE" src="http://static.greatgamesexperiment.com/badge/game/valgard/gge400x56.png"></a>

  7. #7

    Problem with sprite engine. Not working right

    hmm but shouldn't sprite 1 hit left wall. set direction to right. then all other sprites move at same time as sprite 1 on the next loop, the direction is right and they all move at same time? The distance should not change

    I will post the demo, if you can sort it somehow then, I will conceed this is my error ops:

  8. #8

    Problem with sprite engine. Not working right


  9. #9

    Problem with sprite engine. Not working right

    Sorry, I had feed my son.

    Try this code. Cannot test it, because I don't have DelhiX installed. But it should work this way. It's exactly the problem I mentioned... (and William too )


    Code:
    procedure TRow5Sprite.DoMove&#40;MoveCount&#58; Integer&#41;;
    begin
      inherited DoMove&#40;MoveCount&#41;;
      collision;
    
      if &#40;R5Direction='RIGHT'&#41;then
      begin
        if &#40;X>GameAreaX2-Image.width&#41;then
        begin
          r5direction&#58;='LEFT';
          X&#58;=X-Speed1*dx;
          exit;
        end
        else  
          X&#58;=x+Speed1*dx;
      end;
    
      if &#40;R5Direction='LEFT'&#41;then
      begin
        if &#40;X<GameAreaX1&#41;then
        begin
          r5direction&#58;='RIGHT';
          X&#58;=X+Speed1*dx;
        end
        else  
          X&#58;=X-Speed1*dx;
      end;
    end;
    Basically, FIRST check the collision, then move...
    <a href="http://www.greatgamesexperiment.com/game/Valgard/?utm_source=gge&amp;utm_medium=badge_game"><img border="0" alt="GGE" title="GGE" src="http://static.greatgamesexperiment.com/badge/game/valgard/gge400x56.png"></a>

  10. #10

    Problem with sprite engine. Not working right

    nice try but now sprite 8 is moving into sprite 7 instead lol

Page 1 of 2 12 LastLast

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
  •