|
Post by alchemist on Sept 13, 2017 19:55:08 GMT
Hi everyone How can i put a bouncing enemy sprite in a random starting position? Thanks
|
|
|
Post by alessandro on Sept 14, 2017 14:39:40 GMT
Ciao Francesco. Il problema delle posizioni casuali degli sprite in AGD è che devi stare attento a che non vengano generati nelle stesse posizioni degli elementi della scena, specie se occupati da blocchi solidi.
Hai bisogno di stabilire delle coordinate di partenza e questo lo puoi fare posizionando all'inizio uno sprite "vuoto" (cioè la cui immagine non sia disegnata), che poi generi a sua volta un altro sprite, impostato come nemico rimbalzante, spostandosi lungo coordinate casuali entro una certa zona, per poi sparire.
Per esempio, poniamo che in ogni schermo le coordinate X da 50 a 150 e Y da 100 a 200 siano sempre libere, che l'evento sprite 1 sia associato allo sprite "vuoto" e l'evento 2 allo sprite "nemico rimbalzante". Poniamo anche l'immagine sprite 4 come quella del nemico.
Nell'evento sprite 1:
GETRANDOM 151 ; genera un numero casuale tra 0 e 150 LET X RND ; lo sprite ne assume il valore per X ADD 50 TO X ; aumenta di 50 per trovarci nella zona consentita GETRANDOM 201 ; genera un numero casuale tra 0 e 200 LET Y RND ; lo sprite ne assume il valore per Y ADD 100 TO Y ; aumenta di 100 per trovarci nella zona consentita SPAWN 2 4 ; genera uno sprite nemico rimbalzante (2) con immagine sprite 4 REMOVE ; lo sprite "vuoto" sparisce EXIT ; fine dello script Lo sprite "vuoto" non corrompe lo scenario nel momento in cui viene spostato e viene rimosso lasciando il posto al nuovo sprite.
* * *
Hi Francesco. The problem of random sprite position in AGD is that you must care not to spawn them where scenery elements are, particularly if they are made with solid blocks.
In order to generate sprites you need their starting coordinates; you can do this by first placing (option N) a "dummy", i.e. with an empty image, sprite on the screen, that will generate another sprite, set as a bouncing enemy, moving along random coordinates within a set range, and then disappear.
For example, let's suppose that in every screen coordinates between 50 and 150 for X and between 100 and 200 for Y are always free, that sprite event 1 is associated to the "dummy" sprite and event 2 to the "bouncing enemy" sprite. Let's also define image sprite 4 as that of the bouncing enemy.
Sprite event 1:
GETRANDOM 151 ; generate a random number between 0 and 150 LET X RND ; the sprite takes that as its X value ADD 50 TO X ; add 50 in order to move to the allowed zone GETRANDOM 201 ; generate a random number between 100 and 200 LET Y RND ; the sprite takes that as its Y value ADD 100 TO Y ; add 100 in order to move to the allowed zone SPAWN 2 4 ; generate a bouncing enemy sprite (2) with image 4 REMOVE ; the "dummy" sprite disappears EXIT ; end of the script The "dummy" sprite does not corrupt the scenery when it moves and is deleted, leaving room to the newly spawned sprite.
|
|
|
Post by alchemist on Sept 14, 2017 18:43:24 GMT
Thanks Alessandro, i'm going to try. Have a good evening
|
|
|
Post by alchemist on Sept 14, 2017 20:19:12 GMT
It works fine! It's wonderful! Thank you very much!
|
|
|
Post by alessandro on Sept 15, 2017 12:01:08 GMT
Prego
|
|