HGE Homepage Relish Games
Innovative indie games and indie game development
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Help with Killing Particles

 
Post new topic   Reply to topic    Relish Games Forum Index -> HGE Game Engine
View previous topic :: View next topic  
Author Message
Darkkermi



Joined: 23 Feb 2010
Posts: 14
Location: Melbourne, FL

PostPosted: Tue Mar 09, 2010 3:48 am    Post subject: Help with Killing Particles Reply with quote

Hey I was doing one of the tutorials where you make a coin collecting game. I wanted to add an effect to it with a particle that make the coin shine and when the coin is collected the particle should disappear. I am having problems with killing the particle when the coin disappears. Can you help me out?

Code:

HGE *hge = 0;
hgeResourceManager* myRes;
hgeSprite* bgSprite;
hgeAnimation* coinAnim;
hgeFont* font1;
HCHANNEL chan[2];
HEFFECT coinSound;
hgeParticleSystemInfo sparkles;
hgeParticleManager *particleManager;

float mouseX, mouseY;  //coordinates of the mouse cursor
int collected = 0; //how many coins were collected
bool done = false;

struct Coin
{
 bool exists;  //does the coin exist?
 hgeRect loc;  //the location of the coin on the screen
};

Coin coins[MAXCOINS];

bool FrameFunc()
{
  hge->Input_GetMousePos(&mouseX, &mouseY);  //get the current mouse position
  float dt=hge->Timer_GetDelta();  //get the time since the last call to FrameFunc
  coinAnim->Update(dt);  //update the coin animation
  particleManager->Update(dt);  //update all particles
  if(hge->Input_GetKey()==HGEK_ESCAPE){return true;}
  //when left mouse is clicked, check to see if coin is clicked
  if(hge->Input_GetKey()==HGEK_LBUTTON)
  {
   for(int i=0; i<MAXCOINS>Effect_Play(coinSound);  //play a sound
     particleManager->SpawnPS(&sparkles, mouseX, mouseY);
    coins[i].exists = false;
     collected++;
   }
   }
  }
 
 hge->Gfx_BeginScene();
 hge->Gfx_Clear(0);  //clear the screen, filling it with black
 bgSprite->RenderStretch(0, 0, 800, 600);  //render the background sprite stretched

 //render all coins
 for(int i=0; i<MAXCOINS>RenderStretch(coins[i].loc.x1, coins[i].loc.y1, coins[i].loc.x2, coins[i].loc.y2);
       
 }
 font1->SetScale(1.0); //set text size to normal
 font1->SetColor(ARGB(255,0,0,0));  //set color of text to black
 font1->printf(5, 5, 0, "Coins collected: %d", collected);  //display amount of coins collected
 
 particleManager->Render();  //render all particles
 
 hge->Gfx_EndScene();
 
 return done;
}

int WINAPI WinMain (HINSTANCE,HINSTANCE,LPSTR,int)

{
  hge = hgeCreate(HGE_VERSION);
  hge->System_SetState(HGE_WINDOWED, true);
  hge->System_SetState(HGE_FRAMEFUNC, FrameFunc);
  hge->System_SetState(HGE_HIDEMOUSE, false);
  hge->System_SetState(HGE_TITLE, "Coin Collector");
  hge->System_SetState(HGE_LOGFILE, "test.log");
 
  if(hge->System_Initiate())
  {
    myRes = new hgeResourceManager("resource.res");
    hge->Random_Seed(0);
    bgSprite = myRes->GetSprite("bgSprite");

    coinAnim = myRes->GetAnimation("coin");
    coinAnim->Play();  //start playback of animation
    font1 = myRes->GetFont("font1");
    coinSound = myRes->GetEffect("coinSound");
    sparkles = myRes->GetParticleSystem("coinCollected")->info;
    particleManager= new hgeParticleManager();
   
    //initialize all coins
    for(int i=0; i<MAXCOINS>Random_Float(0, 770);
     randY = hge->Random_Float(0, 570);
     coins[i].loc = hgeRect(randX, randY, randX+32, randY+32);
//partcile created on coin a of giving a lifetime of -1 so that it can be killed manually
    sparkles.fLifetime=-1;
    particleManager->SpawnPS(&sparkles,randX,randY);
   
   
    }
   
    hge->System_Start();
  }
  else
  {   
    MessageBox(NULL, hge->System_GetErrorMessage(), "Error", MB_OK | MB_ICONERROR | MB_SYSTEMMODAL);
  }
 
  delete myRes;
  delete particleManager;
 
  hge->System_Shutdown();
  hge->Release();

  return 0;
}


Last edited by Darkkermi on Mon Mar 29, 2010 6:57 pm; edited 1 time in total
Back to top
View user's profile Send private message
ProfEclipse
Expert


Joined: 10 Mar 2005
Posts: 1516
Location: Orlando, FL USA

PostPosted: Wed Mar 10, 2010 3:10 am    Post subject: Reply with quote

When posting code, you need to make sure you check the "Disable HTML in this post" box. Otherwise, the forum eats parts of your code and makes it unreadable. Edit your post and repaste your code and check that box. Then we'll be better able to help you.
Back to top
View user's profile Send private message
Darkkermi



Joined: 23 Feb 2010
Posts: 14
Location: Melbourne, FL

PostPosted: Mon Mar 29, 2010 7:02 pm    Post subject: Reply with quote

I did what you said. Is that better?
Back to top
View user's profile Send private message
ProfEclipse
Expert


Joined: 10 Mar 2005
Posts: 1516
Location: Orlando, FL USA

PostPosted: Mon Mar 29, 2010 7:46 pm    Post subject: Reply with quote

That's much better.

Without commenting on your code, I'll give you some guidance on how to achieve what you're after...

hgeParticleManager::SpawnPS() returns a pointer to an hgeParticleSystem object. hgeParticleSystem has a Stop() method that will stop that particular particle system. So, you need to save the pointers returned from SpawPS() so you can use them later to stop the particles. I would suggest adding a hgeParticleSystem * variable to your Coin structure for this purpose. Then when you want to stop the particles for a particular coin, you can just reference that variable. Something like:
Code:

struct Coin
{
 bool exists;  //does the coin exist?
 hgeRect loc;  //the location of the coin on the screen
 hgeParticleSystem* particleSystem; // particle system for this coin
};

...

coin[i].particleSystem = particleManager->SpawnPS(&sparkles, mouseX, mouseY);
...

coin[i].particleSystem->Stop();
Back to top
View user's profile Send private message
Darkkermi



Joined: 23 Feb 2010
Posts: 14
Location: Melbourne, FL

PostPosted: Fri Apr 02, 2010 7:29 pm    Post subject: Reply with quote

It worked like a charm i had the stop function but i just didn't have the variable for the particle system in the coin structure. Smile thanks a lot
Back to top
View user's profile Send private message
ProfEclipse
Expert


Joined: 10 Mar 2005
Posts: 1516
Location: Orlando, FL USA

PostPosted: Fri Apr 02, 2010 7:55 pm    Post subject: Reply with quote

You're welcome!
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Relish Games Forum Index -> HGE Game Engine All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group