Main Menu

PB Invaders

Started by Ian Price, September 05, 2005, 07:47:39 AM

Previous topic - Next topic

Ian Price

Hi there

Here's an early WIP of my very first crack at making something in PlayBASIC. So far it's taken a few hours to get this done, however I've been having trouble with one part of it. When creating an alien bullet, sometimes the Player bullet gets destroyed or even destroys an invader. I've commented out this code and everything works as it should.

The code I've written for alien fire will only fire a weapon if the alien exists and stops when it doesn't (I'm not worried about that), but the disappearing Player bullet only occurs when aliens are actually firing, so it's got be something to do with the commented out routine.

Can anyone help?

Cheers

Ian
I came. I saw. I played some Nintendo.

thaaks

QuoteHi there

Here's an early WIP of my very first crack at making something in PlayBASIC. So far it's taken a few hours to get this done, however I've been having trouble with one part of it. When creating an alien bullet, sometimes the Player bullet gets destroyed or even destroys an invader. I've commented out this code and everything works as it should.

Hi Ian, haven't tested the code but looking at your source I found the following:

; Have invaders reached edge of playfield?
For n=0 To 100
j=GetSpriteX(n)

Your one loop runs from 0 to 100, but you only create 50 aliens! All your other loops go from 0 to 50... Don't know what happens if you call sprite functions with an invalid id? Maybe it messes up something...

Maybe you should define and use some constants?!

Will look at your code at home tonight.

Ian Price

I forgot to change that value, however, that is definitely not the cause of the problem and it hasn't caused any other problems either.

The code is a bit of a mess - I was doing this at about 3:00 in the morning having never coded anything in PB. I'll be using functions to handle the update of aliens, player, player bullets etc. but I wanted to get everything in and working first (which it pretty much does).

As for CONST - there is nothing in the game that is constant and it is definitely to do with the commented out code. Like I said, I was coding it early this morning and probably missed something obvious.

Cheers :)
I came. I saw. I played some Nintendo.

JokerZ

#3
Hi Ian,

Sweet first program for playbasic. :)

I modified your alien fire routine a bit.
What I found was that you basically had a problem where the invaders fire was killing the invaders.
Setting the sprite collision for the invader bullet to something other than 1, solved that problem.

Also based on how you had it setup I think that you'd be generating fire for aliens that had been previously removed. So a change to the random number routine as well.

Anyway here is the changed section..

I think there is still a further issue with player bullet to alien bullet collision,
but it's getting a bit late and I got to get some sleep. :)

PlayBASIC Code: [Select]
; Alien fire
If alien_fire=0
While GetSpriteStatus(a)=No
a=RndRange(1,number)
EndWhile

If GetSpriteY(a)>0
CreateSprite 500
SpriteCollision 500,%0010
SpriteImage 500,12+fire_anim
PositionSprite 500,GetSpriteX(a)+14,GetSpriteY(a)
a=0
EndIf

alien_fire=1
EndIf



and just as I was about to sleep. :-)


If SpriteHit(500,200,%0001)=200


That will sort out your player bullets hitting alien fire/bullets.

Ian Price

#4
Cheers JokerZ :)

I still believe the problem is something to do with the alien/player fire collision detection - there is no collision detection between the alien and alien fire, so that couldn't cause a problem (besides if you sit and watch, the aliens fire properly without problem and without destroying themselves).

I'm convinced it's a player/alien fireproblem.  Both routines work if the other is commented out.  :blink:


I was aware about the non-available (destroyed) alien sprite problem preventing further firing(as stated) and I was going to sort that next, but you beat me to it. Thanks :)
I came. I saw. I played some Nintendo.

JokerZ

#5
QuoteI still believe the problem is something to do with the alien/player fire collision detection - there is no collision detection between the alien and alien fire, so that couldn't cause a problem (besides if you sit and watch, the aliens fire properly without problem and without destroying themselves).

The problem manifested itself when three elements occurred.
1) An alien bullet hit 2) an alien sprite and 3) you had your own ships bullet on screen

Your spritehit routine then deleted your own ships bullet
The way the routine was configured, it didn't delete the alien but the event of alien bullet touching alien ship was what triggered your ships bullets disappearing!

Changing the alien bullets collision value to %0010 solved that problem, so all three elements no longer shared the collision value of %0001

Editing the IF statement later to avoid checking anything other than bullet to bullet collision fixed the rest.

Have a little faith.  :P

Ian Price

#6
A combination of all three things is bizarre, but I'll accept that. You have more experience in PB than I do, but it still makes no sense to me!

Anyway, what's with the %0010 sprite collision value? This is not mentioned in the help files - what's the difference between this and the methods outlined? And why did having just the one value have such a strange effect on the alien, alien_fire and bullet sprites? I thought the collision value just affected the the type of collision check - the bullets didn't even have to collide to cause the problem.

I've just implemented your changes to the code now and it all works fine. Thankyou.

I'm used to coding in Div and Blitz and this really does seem bizarre.

Cheers :D
I came. I saw. I played some Nintendo.

kevin

#7
Ian,

   I think your after the SpritesOverlap(Sprite1,Sprite2) here (so you can manually check sprite pairs), rather than SpriteHit.  

   SpriteHit is intended for polling the entire sprite list at once.  When you call this function and specify a 'starting sprite'  it'll start scanning from here and continue on to any sprites that were created (linked) before the starting point.  If these sprtes are the same class (which they will be since sprites have a default class of 1) , then the sprite your testing, will return impacts on not only the StartingSprite but any other sprite after the notimated starting point.

   For more info about Collision Classes look into then 'SpriteCollisionClass' command

   By assign groups of sprites a unique class bit, you can selectively poll sprite collision against a certain class, or a mixtures of classes if need be.

  I.e

   if your aliens were class  %0001  (default for all sprites anyway)
 and your Alien bullets were class %0010  

 You could selectively check for an impacts like this.

; test player ship against only Alien sprites
 IndexOfSpriteThatWasHit  = SpriteHit(MyShipSprite, GetFirstSprite(), %0001)     ; only consider Aliens ships

; test player ship against the Alien Bullet sprites
 IndexOfSpriteThatWasHit  = SpriteHit(MyShipSprite, GetFirstSprite(), %0010)     ; only consisder Aliens Bullets


; test player ship against both Alien ships and Alien Bullets
 IndexOfSpriteThatWasHit  = SpriteHit(MyShipSprite, GetFirstSprite(), %0011)     ; only consider Alien ship + AlienBullets
 

 Also, the valued return is the Index Of the Sprite That Was Hit, rather than being a True/False.  This allows you to poll for multiple impacts using the "GetNextSprite" function

I.e.

PlayBASIC Code: [Select]
  ScanSpritesFrom = GetFirstSprite()

IndexOfSpriteThatWasHit = SpriteHit(MyShipSprite, ScanSpritesFrom, %0001) ; only consider Aliens ships

While IndexOfSpriteThatWasHit<>0
Print "My Ship sprite hit sprite "+ str$(IndexOfSpriteThatWasHit)
ScanSpritesFrom = GetNExtSprite(IndexOfSpriteThatWasHit)
IndexOfSpriteThatWasHit = SpriteHit(MyShipSprite, ScanSpritesFrom, %0001) ; only consider Aliens ships
EndWhile




Internally sprites are a linked listed, so you should use the GetFirst/GetNext functions to interate through the sprite list when using SpriteHit.   The list will be in the order of creation. the most recently created sprites will be first and link back to older sprites..

Ian Price

OK. Cheers.

I'm not used to having so many collision detection/sprite routines

:)
I came. I saw. I played some Nintendo.

BlinkOk

it may be just my pc but it crashes when i hit the spacebar.

Ian Price

#10
Quoteit may be just my pc but it crashes when i hit the spacebar.

:huh:  :blink: Ummm... Do you have the most recent PB update? I'm using 1.088.
I came. I saw. I played some Nintendo.

BlinkOk

yeah. i was using an earlier version. looks great. nice work

Ian Price

Quoteyeah. i was using an earlier version. looks great. nice work

Cheers.

I've now added protective bases that can be destroyed by alien/player fire.

I've also placed routines in their respective functions, to make the code easier to read. I'll be implementing explosions etc. soon.
I came. I saw. I played some Nintendo.