Ultimate Amiga

Please login or register.

Login with username, password and session length
Advanced search  
Pages: [1]   Go Down

Author Topic: Animating Multiple Sprites/Bobs at once?!  (Read 6323 times)

0 Members and 1 Guest are viewing this topic.

roberthazelby

  • A600
  • *
  • Karma: 0
  • Offline Offline
  • Gender: Male
  • Posts: 19
Animating Multiple Sprites/Bobs at once?!
« on: December 05, 2011, 08:47:23 PM »

Evening all,

After months and months without using AMOS I came back to it over the weekend.

As I'm a complete beginner, I thought it would be fun to make a REALLY basic game which you control with the mouse.

So far I've
- limited the mouse cursor so it doesn't go off screen
- got the X and Y co-ordinates of the cursor displayed in the top left corner
- Have a different sound being played if you press left or mouse button

My main 'game' loop simply keeps displaying/updating the X/Y co-ordinates on screen, and waits for a mouse button to be pressed.

Now, I've loaded in a ABK file I created many moons ago, which contains a number of different frames of animation. So, once a mouse button is pressed the program hops to the relevant procedure (one for the left button and a separate one for the right) to make the bob appear at the location of the mouse and animate.

The above all works fine, but what I'd like to do is while the that animation is running at the location I clicked at, to then be able to click on another location and have another animation appear and run there. The idea is that the left mouse button will be a light fire (perhaps a handgun) and the right button a shotgun or rocket.

At present, my program has to finish running one animation before it'll let me run a new animation at the next location I click at. What this means is I can't mimic a rapid firing across the screen.

I'm a bit baffled as to how I'd go about animating two or more 'explosions' at the same time. Any suggestions?

Apologies for stumbling at a no doubt basic step.
Logged

BooBoo

  • Amiga Guru
  • A600
  • ****
  • Karma: 3
  • Offline Offline
  • Posts: 168
Re: Animating Multiple Sprites/Bobs at once?!
« Reply #1 on: December 06, 2011, 09:43:00 AM »

I take it your animation takes place in the main loop? take it out and Gosub it.

increase the bob no

Logged

roberthazelby

  • A600
  • *
  • Karma: 0
  • Offline Offline
  • Gender: Male
  • Posts: 19
Re: Animating Multiple Sprites/Bobs at once?!
« Reply #2 on: December 06, 2011, 11:57:09 AM »

I take it your animation takes place in the main loop? take it out and Gosub it.

increase the bob no

Hi!

No, the animations for the left and right mouse buttons take place outside the main loop.

I've tried using 'gosub' and 'proc' methods to hop out of the main loop to carry out the animations. Both give the same results - the code hops out of the main loop to perform the task, but will only animate another sprite once the first has completed it's run.

From memory (as my Amiga isn't in front of me), I move along the animation frames with a for next loop.
Logged

Hungry Horace

  • Amorphous Blue-Blob Man
  • Site Admin
  • A4000T
  • ******
  • Karma: 307
  • Offline Offline
  • Gender: Male
  • Posts: 3,364
  • Don't forget... Ameboid's need love too!
    • AUW
Re: Animating Multiple Sprites/Bobs at once?!
« Reply #3 on: December 06, 2011, 10:29:55 PM »

right.... i think i see the problem here. This may take me a little while to explain though.

If i've read this correctly, your main loop is something this follows....  (psuedo code only by the way!!)

Code: [Select]
LOOP START
 - POSITION Mouse Cursor
 - DRAW Mouse Cursor
 - READ Mouse Buttons
 // Is Left Mouse Button (LMB) pressed?
 - - IF YES then Explosion 1 Animation
 // Is Right Mouse Button (RMB) pressed?
 - - IF YES then Explosion 2 Animation
LOOP END

with two sub routines like...

Code: [Select]
- AT X,Y of Mouse Cursor...
- DRAW Explosion Animation x FRAME 1
- DRAW Explosion Animation x FRAME 2
- DRAW Explosion Animation x FRAME 3
- DRAW Explosion Animation x FRAME 4

actually... you may have "optimised" the above code with a FOR/NEXT loop, but the what happens is the same as i've written above (so later on i will use the "long" version as it explains what is happening better...)

ie. you have used:
Code: [Select]
- AT X,Y OF MOUSE
- FOR z = 1 to 4
- Draw Explosion Animation x FRAME z
- NEXT Z



So.... If we follow this through as an example of a program running, we have  a sequence that might occur like this....

Code: [Select]
LOOP CYCLE 1
 - POSITION Mouse Cursor
 - DRAW Mouse Cursor
 - READ Mouse Buttons
// Is LMB pressed?
- NO
// Is RMB pressed?
- NO
LOOP CYCLE 1 END


LOOP CYCLE 2
 - POSITION Mouse Cursor
 - DRAW Mouse Cursor
 - READ Mouse Buttons
// Is LMB pressed?
- YES
- - GOTO SUB-ROUTINE
- - - AT X,Y of Mouse Cursor...
- - - DRAW Explosion Animation 1 FRAME 1
- - - DRAW Explosion Animation 1 FRAME 2
- - - DRAW Explosion Animation 1 FRAME 3
- - - DRAW Explosion Animation 1 FRAME 4
- - RETURN FROM SUB-ROUTINE
// Is RMB pressed?
- NO
LOOP CYCLE 2 END

LOOP CYCLE 3
 - POSITION Mouse Cursor
 - DRAW Mouse Cursor
 - READ Mouse Buttons
// Is LMB pressed?
- NO
// Is RMB pressed?
- YES
- - GOTO SUB-ROUTINE
- - - AT X,Y of Mouse Cursor...
- - - DRAW Explosion Animation 2 FRAME 1
- - - DRAW Explosion Animation 2 FRAME 2
- - - DRAW Explosion Animation 2 FRAME 3
- - - DRAW Explosion Animation 2 FRAME 4
- - RETURN FROM SUB-ROUTINE
LOOP CYCLE 2 END

as you can see, it has to finish drawing all of the explosion 1 animation, before it can reach the next "check" to see if it does the explosion 2 animation, which it then performs as a whole new operation of all 4 frames.

The explosion 2 animation will not occur until another CYCLE 3, unless in CYCLE 2 we happen to be pressing *both* mouse buttons when the Buttons are read.

What you have occuring here is basically a "straight line" of code, which has to perform each task from start-to-finish before it can check whether or not another task has to happen.  What you need to have, is a main loop which continues positioning/checking even whilst an animation is running, and updating the explosion animation separately.

I will write some similar code in my next post to show how *I* would do this.

I'm sure there are many methods that would work, and the answer I will give is not neccessarily the "correct" one, but it will achieve the effect...  i'm sure you will develop your own coding style to achieve this, but hopefully it will give you an idea on where to go next :)
« Last Edit: December 06, 2011, 10:33:08 PM by Hungry Horace »
Logged
Quote from: KillerGorilla
because winuae is made of code and your amiga is made of stuff

Hungry Horace

  • Amorphous Blue-Blob Man
  • Site Admin
  • A4000T
  • ******
  • Karma: 307
  • Offline Offline
  • Gender: Male
  • Posts: 3,364
  • Don't forget... Ameboid's need love too!
    • AUW
Re: Animating Multiple Sprites/Bobs at once?!
« Reply #4 on: December 06, 2011, 11:20:32 PM »

and now.... my "proposed" solution code...

Please note, this can be severely optimised, by creating single sub-routines which perform shared tasks for animation 1 and 2, but I'm not going to go into that at this point, as I dont want to overly complicate things! As a result there is some "duplication" withing my code.

Basically, my code assumes that each explosion animation has three variables, it's Frame  Number (where if Frame = 0 then we say that the animation is "not playing") and also an X and Y coordinate each.

It also will not allow you to trigger an animation if it is already playing.


Code: [Select]
LOOP START
- POSITION Mouse Cursor
- DRAW Mouse Cursor
- READ Mouse Buttons

// At this point, we will check if we are needing to "create" an explosion animation

// Is LMB pressed AND Animation 1 Frame=0 ?
-- IF BOTH TRUE  THEN
- - Animation 1 Frame=1
- - Animation 1 Postion X = Mouse Cursor X
- - Animation 1 Postion Y = Mouse Cursor Y
-- ENDIF

// Is RMB pressed AND Animation 2 Count=0 ?
-- IF BOTH TRUE  THEN
- - Animation 2 Frame=1
- - Animation 2 Postion X = Mouse Cursor X
- - Animation 2 Postion Y = Mouse Cursor Y
-- ENDIF

// Update any running Animations

// Check if we need to "switch off" the animation, because it has drawn the last frame
-- IF Animation 1 Frame > 4
- - REMOVE Explosion Animation 1
- - Animation 1 Frame = 0
-- ENDIF

// Otherwise, draw the animation frame, and increase the counter by 1
-- IF Animation 1 Frame > 0
- - AT Animation 1 X, Animation 1 Y
- - DRAW Explosion Animation 1 Frame
- - Animation 1 Frame = Frame + 1
-- ENDIF

// repeat for animation 2
-- IF Animation 2 Frame > 4
- - REMOVE Explosion Animation 1
- - Animation 2 Frame = 0
-- ENDIF

-- IF Animation 2 Frame > 0
- - AT Animation 2 X, Animation 1 Y
- - DRAW Explosion Animation 2 Frame
- - Animation 2 Frame = Frame + 1
-- ENDIF

LOOP END


I really hope you are still with me on what this is doing!!

What I will do now, is a simialr example to previously, to show what this code would do if it was running.

I am going to assume that we click the LMB on cycle 2 and that we click the RMB half-way through the first explosion animation... which will give us two explosions at the same time!!

in the square brackets [ ] after each variable, i've put in the "value" of that variable at  that point of the code...

Code: [Select]
LOOP CYCLE 1
- POSITION Mouse Cursor
- DRAW Mouse Cursor
- READ Mouse Buttons

// Is LMB pressed AND Animation 1 Frame=0 ?
-- IF BOTH TRUE 
-- NO
// although Frame = 0, LMB is not Clicked

// Is RMB pressed AND Animation 1 Frame=0 ?
-- IF BOTH TRUE 
-- NO
// although Frame = 0, RMB is not Clicked


// Update Animations

-- IF Animation 1 Frame [0] > 4
-- NO
-- IF Animation 1 Frame [0]  > 0
-- NO

-- IF Animation 2 Frame [0]  > 4
-- NO
-- IF Animation 2 Frame [0]  > 0
-- NO

LOOP CYCLE 1 END



LOOP CYCLE 2
- POSITION Mouse Cursor
- DRAW Mouse Cursor
- READ Mouse Buttons

// Is LMB pressed AND Animation 1 Frame=0 ?
-- IF BOTH TRUE 
-- YES
// Frame = 0, LMB *is* Clicked
- - Animation 1 Frame=1
- - Animation 1 Postion X = Mouse Cursor X [20]
- - Animation 1 Postion Y = Mouse Cursor Y [35]

// Is RMB pressed AND Animation 1 Frame=0 ?
-- IF BOTH TRUE 
-- NO
// although Frame = 0, RMB is not Clicked


// Update Animations

-- IF Animation 1 Frame  [1]  > 4
-- NO
-- IF Animation 1 Frame [1] > 0
-- YES
// ** EXPLOSION 1 ANIMATION STARTS BEING DRAWN**
- - AT Animation 1 X [20], Animation 1 Y [35]
- - DRAW Explosion Animation 1 Frame [1]
- - Animation 1 Frame = Frame + 1 [2]

-- IF Animation 2 Frame [0] > 4
-- NO
-- IF Animation 2 Frame [0] > 0
-- NO

LOOP CYCLE 2 END



LOOP CYCLE 3
- POSITION Mouse Cursor
- DRAW Mouse Cursor
- READ Mouse Buttons

// Is LMB pressed AND Animation 1 Frame=0 [3] ?
-- IF BOTH TRUE 
-- NO
// Frame = 2, LMB status is unimportant as a result

// Is RMB pressed AND Animation 1 Frame=0 [0] ?
-- IF BOTH TRUE 
-- YES
// although Frame = 0, RMB is not Clicked



// Update Animations

-- IF Animation 1 Frame  [2]  > 4
-- NO
-- IF Animation 1 Frame [2] > 0
-- YES
- - AT Animation 1 X [20], Animation 1 Y [35]
- - DRAW Explosion Animation 1 Frame [2]
- - Animation 1 Frame = Frame + 1 [3]

-- IF Animation 2 Frame [0] > 4
-- NO
-- IF Animation 2 Frame [0] > 0
-- NO

LOOP CYCLE 3 END



LOOP CYCLE 4
- POSITION Mouse Cursor
- DRAW Mouse Cursor
- READ Mouse Buttons

// Is LMB pressed AND Animation 1 Frame=0 [3] ?
-- IF BOTH TRUE 
-- NO
// Frame = 3, LMB status is unimportant as a result

// Is RMB pressed AND Animation 1 Frame=0 [0] ?
-- IF BOTH TRUE 
-- YES
// Frame = 0, RMB *is* Clicked
- - Animation 2 Frame=1
- - Animation 2 Postion X = Mouse Cursor X [156]
- - Animation 2 Postion Y = Mouse Cursor Y [67]


// Update Animations

-- IF Animation 1 Frame  [3]  > 4
-- NO
-- IF Animation 1 Frame [3] > 0
-- YES
- - AT Animation 1 X [20], Animation 1 Y [35]
- - DRAW Explosion Animation 1 Frame [3]
- - Animation 1 Frame = Frame + 1 [4]

-- IF Animation 2 Frame [1] > 4
-- NO
-- IF Animation 2 Frame [1] > 0
-- YES
// ** EXPLOSION 2 ANIMATION STARTS BEING DRAWN**
- - AT Animation 2 X [156], Animation 2 Y [67]
- - DRAW Explosion Animation 2 Frame [1]
- - Animation 2 Frame = Frame + 1 [2]


LOOP CYCLE 4 END




LOOP CYCLE 5
- POSITION Mouse Cursor
- DRAW Mouse Cursor
- READ Mouse Buttons

// Is LMB pressed AND Animation 1 Frame=0 [4] ?
-- IF BOTH TRUE 
-- NO
// Frame = 4, LMB status is unimportant as a result

// Is RMB pressed AND Animation 1 Frame=0 [2] ?
-- IF BOTH TRUE 
-- NO
// Frame = 4, RMB status is unimportant as a result


// Update Animations

-- IF Animation 1 Frame  [4]  > 4
-- NO
-- IF Animation 1 Frame [4] > 0
-- YES
- - AT Animation 1 X [20], Animation 1 Y [35]
- - DRAW Explosion Animation 1 Frame [4]
- - Animation 1 Frame = Frame + 1 [5]

-- IF Animation 2 Frame [2] > 4
-- NO
-- IF Animation 2 Frame [2] > 0
-- YES
- - AT Animation 2 X [156], Animation 2 Y [67]
- - DRAW Explosion Animation 2 Frame [2]
- - Animation 2 Frame = Frame + 1 [3]

LOOP CYCLE 5 END




LOOP CYCLE 6
- POSITION Mouse Cursor
- DRAW Mouse Cursor
- READ Mouse Buttons

// Is LMB pressed AND Animation 1 Frame=0 [5] ?
-- NO
// Frame = 4, LMB status is unimportant as a result

// Is RMB pressed AND Animation 1 Frame=0 [3] ?
-- NO
// Frame = 4, RMB status is unimportant as a result


// Update Animations

-- IF Animation 1 Frame  [5]  > 4
-- YES
// Switch-off Animation 1, because we have used all 4 frames.
- - REMOVE Explosion Animation 1
- - Animation 1 Frame = 0

-- IF Animation 1 Frame [4] > 0
-- NO


-- IF Animation 2 Frame [3] > 4
-- NO
-- IF Animation 2 Frame [3] > 0
-- YES
- - AT Animation 2 X [156], Animation 2 Y [67]
- - DRAW Explosion Animation 2 Frame [3]
- - Animation 2 Frame = Frame + 1 [4]

LOOP CYCLE 6 END


by following that all through (!!!) we can see that Animation 1 starts in Loop Cycle 1, and continues drawing one further animation frame each Loop Cycle, meaning that it is finally "switched off" only in Loop Cycle 6.

By keeping the animation updates and the mouse/event checking separate, this allows us to trigger another Animation 2 in Cycle 4, whilst Animation 1 is still happening.... I didnt follow the code through any futher than the 6 loops, but in Loop 7 we would have been able to have triggered Animation 1 again whilst Animation 2 was still running, as our frame counter had been reset back to zero.


I really hope this helps explain a systematic way of achieving a game-loop that allows true multiple events to occur for you!!

Please feel free to ask questions, as it is getting quite ate now, so i've probably written something wrong in there somewhere!
« Last Edit: December 07, 2011, 07:52:49 AM by Hungry Horace »
Logged
Quote from: KillerGorilla
because winuae is made of code and your amiga is made of stuff

roberthazelby

  • A600
  • *
  • Karma: 0
  • Offline Offline
  • Gender: Male
  • Posts: 19
Re: Animating Multiple Sprites/Bobs at once?!
« Reply #5 on: December 07, 2011, 12:37:14 PM »

Thank you so much for the considerable time and effort you've gone to to explain this in layman's terms.

As soon as I saw your solution I was hit by an overwhelming feeling of "Of course! Why didn't I see that?!".

I'll see if I can get some time over the weekend to code-up your solution and give it a go. I'll report back if/when I get it up and running.

You've made my day. Thank you!
Logged

Hungry Horace

  • Amorphous Blue-Blob Man
  • Site Admin
  • A4000T
  • ******
  • Karma: 307
  • Offline Offline
  • Gender: Male
  • Posts: 3,364
  • Don't forget... Ameboid's need love too!
    • AUW
Re: Animating Multiple Sprites/Bobs at once?!
« Reply #6 on: December 07, 2011, 04:49:21 PM »

Hi Rob,

I spent some time in my lunch break putting my psuedo code into action!

have a look at the attached files. I had to build my own explosion graphics, and there is a bit of code in there to "generate" my AMOS bank (your .abk file) "on the fly" ... this is because I dont like the abk format much as I prefer to be able to simply adjust a sprite table in an IFF file, and know that my code will take in any changes on the next run!  You'll need to press a keyboard key to skip the graphics-table screen.

You may also need to adjust the "wait" times, because i ran it on a very fast UAE setup and it was flying by too quickly to be able to see the multiple explosions in action at any point!

Anyway, you should be able to see it all working as intended. Maybe later i will have a try at sliming down the code "for fun" so there is more shared code :)

Enjoy!
« Last Edit: December 07, 2011, 04:51:31 PM by Hungry Horace »
Logged
Quote from: KillerGorilla
because winuae is made of code and your amiga is made of stuff

Hungry Horace

  • Amorphous Blue-Blob Man
  • Site Admin
  • A4000T
  • ******
  • Karma: 307
  • Offline Offline
  • Gender: Male
  • Posts: 3,364
  • Don't forget... Ameboid's need love too!
    • AUW
Re: Animating Multiple Sprites/Bobs at once?!
« Reply #7 on: December 07, 2011, 08:41:25 PM »

i've made a further couple of examples in the attached ZIP :)

v 001 - simply the original example of the above code in action, nothing too fancy, but does the job

v 002 - cleans up the code a little bit, so that some GOSUBs are used out of our main game loop and reduce the amount of duplicate code throughout... I chucked a few of the very-similar-variables into unnessesarily large Arrays, for future expansion :)

v 003 - I played around with timing elements a bit, to show how you can separate your animation frame-rate from your overall game-loop speed - hopefully allowing you to play around with frame-speeds and add more game-code without worrying about needing to adjust the wait lengths.

I was tempted to have a look into making a version 004 which allows multiple explosions from the LMB in order to create your original idea of "a rapid firing across the screen." (i.e. as if a machine gun was firing out of the LMB) ... perhaps with a limit of 5-6 explosion "1"s on screen at any point.... whether I'll have time for that now though, i'm not so sure.

Hope all this helps, and happy coding :)
« Last Edit: December 07, 2011, 08:42:57 PM by Hungry Horace »
Logged
Quote from: KillerGorilla
because winuae is made of code and your amiga is made of stuff

Hungry Horace

  • Amorphous Blue-Blob Man
  • Site Admin
  • A4000T
  • ******
  • Karma: 307
  • Offline Offline
  • Gender: Male
  • Posts: 3,364
  • Don't forget... Ameboid's need love too!
    • AUW
Re: Animating Multiple Sprites/Bobs at once?!
« Reply #8 on: December 13, 2011, 11:39:53 AM »

thanks for giving me the AMOS bug again Rob... hehe.

I am constantly thinking about coding again!
Logged
Quote from: KillerGorilla
because winuae is made of code and your amiga is made of stuff

roberthazelby

  • A600
  • *
  • Karma: 0
  • Offline Offline
  • Gender: Male
  • Posts: 19
Re: Animating Multiple Sprites/Bobs at once?!
« Reply #9 on: December 31, 2011, 10:33:02 AM »

I've started looking at the versions 1 to 3 of the code samples you kindly put together for me.

Versions 1 and 2 don't seem to do much on my Amiga. The animation cells get displayed as a background. Most odd.

Version 3 works perfectly, and I'm in the process of reading and re-reading through the code to try and work out what's going on.

I've updated version 3 so that the left mouse button now produces multiple explosions at the same time, which looks great.

The one thing that I'm completely stumped by, and I'm sure I'm being daft here, is that I can't see where the animation frames are being loaded in. There's no line of code that points to the file.
Logged

Hungry Horace

  • Amorphous Blue-Blob Man
  • Site Admin
  • A4000T
  • ******
  • Karma: 307
  • Offline Offline
  • Gender: Male
  • Posts: 3,364
  • Don't forget... Ameboid's need love too!
    • AUW
Re: Animating Multiple Sprites/Bobs at once?!
« Reply #10 on: January 04, 2012, 07:58:01 PM »

Versions 1 and 2 don't seem to do much on my Amiga. The animation cells get displayed as a background. Most odd.

you did press a keyboard key at this point right? There is a "wait key" after the image is loaded, just to show you where the animation frams are coming from.

If you press a button here, it will then go into the main game loop.


Quote
Version 3 works perfectly, and I'm in the process of reading and re-reading through the code to try and work out what's going on.

I've updated version 3 so that the left mouse button now produces multiple explosions at the same time, which looks great.

The one thing that I'm completely stumped by, and I'm sure I'm being daft here, is that I can't see where the animation frames are being loaded in. There's no line of code that points to the file.

Scroll to the bottom of the code an you will see a procedure called "_GETBOBS"  in version 001 and 002 this procedure is "open" for you to look at. On Version 003 you will need to open it either via the AMOS menu, or by pressing F9 on the procedure name... this will allow you to see the code that loads the picture, grabs all of the images, and puts them into an AMOS bank "on the fly"


Hope that helps!
Logged
Quote from: KillerGorilla
because winuae is made of code and your amiga is made of stuff

roberthazelby

  • A600
  • *
  • Karma: 0
  • Offline Offline
  • Gender: Male
  • Posts: 19
Re: Animating Multiple Sprites/Bobs at once?!
« Reply #11 on: January 07, 2012, 10:57:56 AM »

Thanks for your help, HH. I should have updated your thread before you replied, as I went back to programs 1 and 2, rummaged around and saw how they worked.

The big thank you is for pointing out F9. I kept looking at the 'hidden' procedure thinking "Why the heck is that there? It doesn't do anything", and then wondering how on earth you were pulling the graphics in.

You've been a big help. I'll try and get some bits done over the weekend. I'm really enjoying this and appreciate the efforts you've gone to in helping me get started.

I think I owe you a beer or three!
Logged
Pages: [1]   Go Up
 

TinyPortal 2.2.2 © 2005-2022