Advertisement

Linked Lists

Started by May 12, 2001 06:37 PM
29 comments, last by JSCFaith 23 years, 8 months ago
Hey, I am making a asteroids clone and I have a class called "Bullet." Right now I have it so that when you click the space bar, the "Ship" class calls the "Bullet::CreateBullet." The only problem is that when It tries to add the bullet to the linked list it crashes. Here is some of the code.
  
// This function creates a bullet dynamically.  I am I using the 'new' command correctly?


void Bullet::CreateBullet(float x, float y, int Direction) {

	Bullet *NewBullet;

	NewBullet = new Bullet;// This calls the constructor correct? 

// Set the bullets properties

	NewBullet->SetPosition(x,y);
	NewBullet->SetObjectState(ALIVE);
	NewBullet->SetBulletVelocity(Direction);
	NewBullet->SetSize(4, 4);
	NewBullet->SetAnimationProperties(8, 8, 1, 1, DDSCAPS_SYSTEMMEMORY, "Data\\Art\\Weapons\\bullet");
	NewBullet->SetFrame(1);

}

// The class Bullet's constructor adds bullet to linked list

         Bullet() {

		Type = BULLET;
		Damage = 5;
		DistanceFlown = 0;

		// Add to linked List

		if( pBulletFirst == 0 )
		{
			// this is the first node

			pBulletFirst = this;
			pBulletPrev = NULL;
		}
		else
		{
			pBulletLast->pBulletNext = this;
			this->pBulletPrev = pBulletLast;

		}

		pBulletLast = this;
		this->pBulletNext = NULL;
	
	}

  
Well, I think the problem is in the constructor, but maybe I am using the 'new' thing wrong. I have never used it before. Well thanks a lot guys. Later. Edited by - JSCFaith on May 12, 2001 7:37:36 PM
Your pFirstBullet is declared as a static member of the class isn''t it?
Advertisement
did u declare the pBulletFirst, pBulletPrev as static ?? ..

since i think there will be multiple copies ..

? .. just asking ..

hehe .. i answered same time as beelzebub .. sorry for the duplicate answer

Edited by - jwalker on May 12, 2001 10:29:28 PM
Yeah, it is. So is the pBulletLast.
No, pBulletNext and Prev are not static.
make them static .. and then try .
Advertisement
I know that pBulletNext and pBulletPrev are not supposed to be static becuase each bullet has to point to the next and prev bullet of itself. Any other ideas.
Does anyone know how I would go about removing an item from a linked list?
assuming u want to delete node2..

node1->next = node3;
delete node2;

as simple as that ..

You forgot something, jwalker... You also need to change
the node3->prev pointer if it''s a doubly linked list... And looking at JSCFaith''s code tells that it is. :p
AquDev - It is what it is.

This topic is closed to new replies.

Advertisement