Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

2D All the way! / What is the best way to loop this code?

Author
Message
Xatnys
20
Years of Service
User Offline
Joined: 8th Jul 2004
Location:
Posted: 11th Jul 2004 05:56
If you were to compile the following code and add media, you would be able to control a simple 2D space shooter. You'll notice that once you run the shoot and blowitup sub routines, that the program ends.

What would be the best way to loop this code so that you can fire multiple times without the program ending?

Code here:
{code}
sync on
sync rate 30
color backdrop 0
hide mouse
load image "yourship.bmp",1
load image "alienship.bmp",2
load image "orb.bmp",3
load image "greenlazer.bmp",4
load image "yellowlazer.bmp",5
load image "exp1.bmp",6
load image "exp2.bmp",7
load image "exp3.bmp",8
load image "exp4.bmp",9
load image "exp5.bmp",10
load image "exp6.bmp",11
load image "stars.bmp",50
load image "title.bmp",9999
LOAD SOUND "phaser03.wav",1
LOAD SOUND "boom2.wav",2
MAKE OBJECT PLAIN 4,600,56
ghost object on 4
POSITION OBJECT 4,0,-4,80
TEXTURE OBJECT 4,50
LOCK OBJECT ON 4
color backdrop rgb(0,0,0)


pf=0:score=0:Level=1:lives=3
ysx=50:ysy=200
sprite 1,ysx,ysy,1
lx=SPRITE X(1)+20
ly=SPRITE Y(1)+10
sprite 3,500,200,2
sprite 9999,10,10,9999
do
set cursor 200,400: Print "SCORE:";score; " LEVEL:";level;" LIVES:";lives
if UPKEY()=1 and ysy>100
dec ysy,10
endif
if DOWNKEY()=1 and ysy<350
inc ysy,10
endif
if inkey$()="z" and pf=0 then gosub shoot:pf=1
if SPRITE EXIST(2)=1
if SPRITE HIT(2,3)=1 then gosub blowitup
endif
sprite 1,ysx,ysy,1
gosub moveall
gosub movestars
sync
loop
movestars:
SCROLL OBJECT TEXTURE 4,0.01,0.0
return
shoot:
sprite 2,lx,ly,4
PLAY SOUND 1
return
moveall:
if SPRITE EXIST(2)=1
If sprite X(2)<640
lx=lx+45
sprite 2,lx,ly,4
endif
endif
if SPRITE EXIST(2)=1
If sprite X(2)>640
pf=0
lx=SPRITE X(1)+20
ly=SPRITE Y(1)+10
endif
endif
return
blowitup:
delete sprite 2
PLAY SOUND 2
for t=6 to 11
sprite 3,500,200,t
wait 1
next t
delete sprite 3
end
return

{/code]

I ask because this is from Reaperman's excellent 2D Space Shooter tutorial, I am using a variation of this code(with my own touches). When my version loops, both the shooting and the collision detection cease to function. I'm hoping to see my error, and then be able to start correcting my code.

Thanks for any and all help!

Xatnys

Oh and fwiw I'm Running DBC Enhanced 1.13 if that helps.

Hello.
Goodbye.
zenassem
22
Years of Service
User Offline
Joined: 10th Mar 2003
Location: Long Island, NY
Posted: 11th Jul 2004 06:48
I took out the end statement in one of the subroutines
and place a temporary shoot: label because it didn't exist in the snippet yoou provided.

Not sure why you are using a wait command in one of the subs, as opposed to locking the framrate with timing code, but you may not be upt to that part in the tutorial, and I havene't read the tutorial -- so i'm not familiar with it.





Xatnys
20
Years of Service
User Offline
Joined: 8th Jul 2004
Location:
Posted: 11th Jul 2004 07:02
Hi, Thanks for the info. It seems I've been looping it correctly, or semi correctly at least, so I'm unsure what the error is in my code.



It runs fine once, you can "shoot" the sprite laser once, it will hit the "enemyship" sprite and collision detection occurs, it then runs my explosion sub routine.

After that, it seems to loop but has errors in it. You can now press fire, but must hold the fire key to get the "laser" to move across the screen. When it passes the "enemyship" sprite, there is no longer any collision detection, I'm not sure exactly what is wrong to be totally honest.

Thank you for your time and help, it's greatly appreciated!

Xatnys

Hello.
Goodbye.
zenassem
22
Years of Service
User Offline
Joined: 10th Mar 2003
Location: Long Island, NY
Posted: 11th Jul 2004 08:12
I'm not on a computer right now that has my DBpro or DBC compiler, but i wouldd guess that it has something to do with you deleting sprite 2 in your blowup: routine without checking if it exists or not.


Login to post a reply

Server time is: 2025-05-16 23:57:28
Your offset time is: 2025-05-16 23:57:28