Post by fadgeplaysgames on Oct 7, 2018 4:48:52 GMT -5
Has anyone got some example code for random enemy movement? I have tried various was to get a horizontally patrolling enemy that changes direction at random intervals. I must be doing something wrong because they only seem to go in one direction.
Hi, I don't know if this suits you because it was made for the fifth chapter of Cousin Horace, a platform and ladders enviroment disguised as a maze viewed from above. You might want to adapt the script to your needs. The script operates a random value check so that, if the sprite hits a ladder (IF LADDERABOVE / IF LADDERBELOW) and that value is 0, then it goes up or down accordingly, otherwise it checks whether it should continue moving in the same horizontal direction or change it. IF DIRECTION = 0 IF LADDERABOVE SPRITEUP ELSE GETRANDOM 2 IF RND = 0 LET DIRECTION 2 ELSE LET DIRECTION 3 ENDIF ENDIF ENDIF IF DIRECTION = 1 IF LADDERBELOW SPRITEDOWN ELSE GETRANDOM 2 IF RND = 0 LET DIRECTION 2 ELSE LET DIRECTION 3 ENDIF ENDIF ENDIF IF DIRECTION = 2 IF CANGOLEFT SPRITELEFT ELSE LET DIRECTION 3 ENDIF GETRANDOM 4 IF RND = 0 IF LADDERABOVE LET DIRECTION 0 ELSE IF LADDERBELOW LET DIRECTION 1 ENDIF ENDIF ENDIF ENDIF IF DIRECTION = 3 IF CANGORIGHT SPRITERIGHT ELSE LET DIRECTION 2 ENDIF GETRANDOM 4 IF RND = 0 IF LADDERABOVE LET DIRECTION 0 ELSE IF LADDERBELOW LET DIRECTION 1 ENDIF ENDIF ENDIF ENDIF
Post by fadgeplaysgames on Oct 28, 2018 15:32:01 GMT -5
thanks for the advice. Is there any way to get the sprite to change direction without it passing a certain trigger (like a ladder)? What I was trying to do was have a simple loop (e.g. if a=8 let a=0 else add 1 to a), then in the sprite event, have something like if a=2 getrandom 2 if rnd = 0 let parama = 0 else let parama = 1 endif endif
then have the parama decide the direction the sprite walks in. all that happens is the sprite walks in one direction continuously. I thought the loop would trigger the getrandom over and over, but it doesnt seem to do that?