How To

Sprite Animations disabling other sprites?

2

LRCFresh 2018-11-18 05:21

So I’ve got this program where you shoot from the main sprite, but when you do shoot, the bullet goes and stuff but while the bullet is moving, the main sprite is disabled and can’t move. Any suggestions? Thanks :)

-Fresh


Timo 2018-11-18 09:43 (Edited)

I have an idea what you probably do, but it's easier to help if you post your program here. You can attach it to your question post.


was8bit 2018-11-18 16:19

It's best to have one MAIN DO LOOP that handles everything in passes.... use GOTO with RETURN to branch off and one branch increment your bullets position and in another branch increment you're player..


was8bit 2018-11-18 16:22

Or, you can check out my example game STARFALL for another example of how i get 2 players and 2 gullets all moving about :)


was8bit 2018-11-18 16:26

I my game PIXEL PACMAN I branch off for the movements, which I think is easier to branch off for things as it organizes your code better, doesn't get messy, and you can more easily focus on one thing and fix it without worring about other things...

So, as you are making your game, you create a GOTO RETURN branch that controls your player, and get that working... then you add a GOTO RETURN branch than controls your bullet... etc.. :)


Timo 2018-11-18 16:45

Note: It should be GOSUB RETURN.


was8bit 2018-11-19 04:31

Oops... correct, that's what I get for typing when I'm tired,, sorry ...


LRCFresh 2018-11-19 06:46

Here is the source code of the program, any help is appreciated :)


Timo 2018-11-19 07:32

It's important to understand, that a program is executed one line at a time and only at one position at a time. It executes one line and goes to the next. Of course, there are several commands which change the current position, like GOTO, DO/LOOP, CALL etc.

Your program is running the main loop with the player control. But when you fire, it jumps to the "firea" subprogram. It has its own loop (WHILE/WEND), so it will run it until the bullet is out of the screen and then it returns to the main program. Both parts of the code are NOT executed at the same time.

So, if you want things to happen at the same time, you have to update them for one step only and return to the main program.

Here is some code which should work (it's not the complete program, so replace the lines in your's):

MOVEMENT:
DO
IF UP(0) AND PY>0 THEN PY=PY-1
IF DOWN(0) AND PY<120 THEN PY=PY+1
IF LEFT(0) AND PX>8 THEN PX=PX-1
IF RIGHT(0) AND PX<128 THEN PX=PX+1
SPRITE 0,PX,PY,1

'FIRE ON BUTTON PRESS
IF BUTTON TAP(0,0) THEN CALL FIREA
'UPDATE ALWAYS
CALL UPDATEFIRE

WAIT VBL
LOOP

SUB FIREA
SPRITE 2,PX,PY-4,2
PLAY 0,49,5
END SUB

SUB UPDATEFIRE
IF SPRITE.Y (2)>0 THEN
SPRITE 2,SPRITE.X (2),SPRITE.Y (2)-8,2
ELSE
SPRITE OFF 2
END IF
END SUB


was8bit 2018-11-19 08:17

As I was saying <<It's best to have one MAIN DO LOOP that handles everything in passes.... use GOSUB with RETURN to branch off and one branch increment your bullets position and in another branch increment you're player>>

the key word here is INCREMENT... where you completely move your bullet, you need a MBX,MBY for MoveBulletX,MoveBulletY (for firing in any direction)? Set these at the time you fire, then use these to INCREMENT your bullet one little bit each time your main loop goes to that subroutine..

Also consider a variable to know IF a bullet even exists...if not yet fired ... FB for FiredBullet set to 0 for not fired and 1 if fired..

Use variables of your choice that you like, the point is that you make your subroutine SMART by using these variables and that subroutine will do different things based on what information you give it based on these variables... your main loop, or even other subroutines, can then communicate information it needs via these variables..

This basic idea applies to ALL subroutines, give each subroutine a specific function, create variables it needs to do its job, and any other part of your code can communicate to it (or it can communicate to any other part of your code) using its own variables...


LRCFresh 2018-11-19 09:14

Thanks for your guys' help! I understand the language a lot better now LOL

so uh, now that that's cleared and working, anyone know how to make it choose a random X coordinate?


was8bit 2018-11-19 11:51

INT(RND*10) yields a number from 0 to 9

So, there are 20 characters wide on the screen at 8 pixels each, s 20*8= 160, but remember you don't start your count with 1 but with 0, so

INT(RND*180) yields a number from 0 to 127

Also, for random x movement..
INT(RND*3) yields a number from 0 to 2, so

INT(RND*3)-1 yields a number from these results: -1 or 0 or +1 :)

If you want to avoid the 0 the trick is

INT(RND*2)*2 -1 yields only -1 or +1 ;)


Log in to reply.