2 Energy bars

Talk about making games.

2 Energy bars

Postby foleyjo » Sat Jul 31, 2010 4:34 pm

Just wondering if someone can help and have a quick scan over this code as I'm not sure what I'm doing wrong
I have 2 ships with visible energy bars that reduce when they collide with objects. They bars change colour according to the damage.

The problem is I can't seem to get the energy bars to work independantly of each other. They won't move until one of the ships is destroyed. When there is one ship on screen it works fine

I have been working with the example code I got from DilloDude in my thread about AI. I thought I understood it but clearly I've misunderstood something

First I have a trigger for the ships which gives them full energy


Code: Select all
Actor * SHIP;
//make a ship, and store it so we can adjust its values
    SHIP = CreateActor("ship", "ship1", "(none)", "(none)", 0, 0, false);
    SHIP->energy = 100;  //energy is declared as Actor Integer )
DestroyActor("Event Actor");



In the ships CREATE code I create the energy bar which uses a CANVAS actor

Code: Select all
//make energy bar
Actor * NRG = CreateActor("nrgbar", "icon", "Event Actor", "(none)", 0,80, true);  //positioned slightly below ship
strcpy(Fleetnrg, NRG->clonename);

stats = _fleet; //set type
xvelocity = plyrspeed;


For the collision I have a simple energy - value command which works as the ships do get destroyed when they are supposed to. It's just the first one will have a full energy bar before it goes

In the energy bars draw code I have the following

Code: Select all
int i = 0 ;
int xstart =parent.x;
int xend = parent.energy

//check if alive
if (parent.cloneindex < 0)
{
   DestroyActor("Event Actor");
}


//First clear bar
r=0;g=0;b=0;i=0;
fleetnrg : setpen(r,g,b,0,i);
for (i=0; i<=5; i++)
{
  moveto(xstart,i);
  lineto(100, i);
}

//Check energy status and change colour accordingly
if (xend > 50)
{
  r=0; g =255; b= 0;i=0;
}
if (xend <= 50)
{
  r=255; g =255; b= 0;i=0;
}
if (xend <=25 )
{
  r=255; g =0; b= 0;i=0;
}

// Draw Energy
setpen(r,g,b,0,i);
for (i=0; i<=5; i++)
{
  moveto(xstart,i);
  lineto(xend, i);
}
KISS -Keep It Simple Stoopid
foleyjo
 
Posts: 275
Joined: Mon Jul 09, 2007 1:15 pm
Score: 15 Give a positive score

Re: 2 Energy bars

Postby Bee-Ant » Sun Aug 01, 2010 2:11 am

foleyjo wrote:Just wondering if someone can help and have a quick scan over this code as I'm not sure what I'm doing wrong
I have 2 ships with visible energy bars that reduce when they collide with objects. They bars change colour according to the damage.

Well, I will use different method here...

#Requirements :
- Ship (normal actor)
- Bar (normal actor)
- id (int actor variable)
- hp (int actor variable)

#Script :
1. Ship -> Create Actor -> Script Editor :
Code: Select all
CreateActor("Bar", "BarAnimation", "(none)", "(none)", 0, 0, false);
Bar.id=cloneindex;
hp=100; //or any value you want

2. Bar -> Draw Actor -> Script Editor :
Code: Select all
actor* check;
char str[32];
sprintf(str,"Ship.%i",id); //Get the clone full name
check=getclone(str); //Get the clone access

x=check->x; //set the x position
y=check->y-check->height/2; //set the y position

r=(animpos<50)*255; //set the red value
g=255*animpos/50; //set the green value
b=0; //set the blue value

if(check->hp<=0)DestroyActor("Event Actor");

3. Ship -> Collision -> Object :
Code: Select all
hp-=1; //or something


For the Bar, you can use this :D
Image
User avatar
Bee-Ant
 
Posts: 3723
Joined: Wed Apr 11, 2007 12:05 pm
Location: http://www.instagram.com/bee_ant
Score: 210 Give a positive score

Re: 2 Energy bars

Postby foleyjo » Sun Aug 01, 2010 8:33 am

Thanks for that Bee-Ant I probably will use your method. However I am more interested in what I'm doing wrong with the code I posted so I can use the knowledge when I work on other bits rather than asking for help all the time.

What I don't understand is that the bit in my code for xstart works. It puts the bar on the ship it's supposed to be.
My xend also works when there is one ship but when there is 2 it gives strange results.

Using your code as a guide I gave the bar an id number when creating it. Then I used the following

Code: Select all

sprintf(str,"Ship.%i",id); //Get the clone full name
check=getclone(str); //Get the clone access
xstart=check->x; //set the start of the bar
xend=check->fenergy; //set the end position


It gave me more or less the same result. 1 bar works the other doesnt
KISS -Keep It Simple Stoopid
foleyjo
 
Posts: 275
Joined: Mon Jul 09, 2007 1:15 pm
Score: 15 Give a positive score

Re: 2 Energy bars

Postby Bee-Ant » Sun Aug 01, 2010 8:57 am

Are xstart and xend actor variables?
If yes, they should work...
Also, what do you do to both after you set their value?
User avatar
Bee-Ant
 
Posts: 3723
Joined: Wed Apr 11, 2007 12:05 pm
Location: http://www.instagram.com/bee_ant
Score: 210 Give a positive score

Re: 2 Energy bars

Postby foleyjo » Sun Aug 01, 2010 9:11 am

both xstart and xend are variables declared as integers at the start of the code using
int xstart; int xend;

Once I have the xstart and xend values I perform the draw command on the canvas which is basically

for 1 to 5 draw a line from xstart to xend

Both bars appear at the start.
Then when one ship collides with an object both bars reduce. Then when 1 ship is destroyed the other ships bar reappears to where it should be
KISS -Keep It Simple Stoopid
foleyjo
 
Posts: 275
Joined: Mon Jul 09, 2007 1:15 pm
Score: 15 Give a positive score

Re: 2 Energy bars

Postby Bee-Ant » Sun Aug 01, 2010 9:18 am

Hmmm...what if using this code to draw the bar?
Code: Select all
for (i=0; i<=5; i++)
{
    moveto(xstart,i);
    lineto(xstart+xend, i);
}
User avatar
Bee-Ant
 
Posts: 3723
Joined: Wed Apr 11, 2007 12:05 pm
Location: http://www.instagram.com/bee_ant
Score: 210 Give a positive score

Re: 2 Energy bars

Postby foleyjo » Sun Aug 01, 2010 9:48 am

Doesn't draw anything when using that method.

I've included a demo to show what is happening when I use the following draw code. Sorry for messy code it's my way of learning

Code: Select all
int i = 0 ;
int xstart =creator.x;
int xend;
Actor* check;
char str[32];

//check if alive

if (parent.cloneindex < 0)
{
 DestroyActor("Event Actor");
}


sprintf(str,"Fleeta.%i",id); //Get the clone full name
check=getclone(str); //Get the clone access
xstart=check->x; //set the start of the bar
xend=check->fenergy; //set the end position

//clear bar
  r=0;g=0;b=0;i=0;
fleetnrg : setpen(r,g,b,0,i);
 for (i=0; i<=5; i++)
{
moveto(xstart,i);
lineto(100, i);
}
//Check energy status and change colour accordingly
if (xend > 50)
  {
      r=0; g =255; b= 0;i=0;

  }
if (xend <= 50)
  {
  r=255; g =255; b= 0;i=0;
  }
if (xend <=25 )
   {
  r=255; g =0; b= 0;i=0;

    }
// Draw Energy
setpen(r,g,b,0,i);
for (i=0; i<=5; i++)
{
 moveto(xstart,i);
  lineto(xend, i);
 }
Attachments
test.exe
(1.64 MiB) Downloaded 60 times
test.exe
(1.64 MiB) Downloaded 61 times
KISS -Keep It Simple Stoopid
foleyjo
 
Posts: 275
Joined: Mon Jul 09, 2007 1:15 pm
Score: 15 Give a positive score

Re: 2 Energy bars

Postby Bee-Ant » Sun Aug 01, 2010 9:55 am

Maybe the problem is caused by parenting...
I don't use parenting when creating the bar there...
Also, you shouldn't use parenting when using my method...
That's why I set the bar's x by
Code: Select all
x=check->x;
User avatar
Bee-Ant
 
Posts: 3723
Joined: Wed Apr 11, 2007 12:05 pm
Location: http://www.instagram.com/bee_ant
Score: 210 Give a positive score

Re: 2 Energy bars

Postby foleyjo » Sun Aug 01, 2010 10:19 am

Took out all parenting references but it made little difference.
They still won't draw independently of each other.

One ship works fine. As soon as 2 come into play it does the same as in the demo file.
KISS -Keep It Simple Stoopid
foleyjo
 
Posts: 275
Joined: Mon Jul 09, 2007 1:15 pm
Score: 15 Give a positive score

Re: 2 Energy bars

Postby MrB » Sun Aug 01, 2010 11:34 am

foleyjo wrote:Took out all parenting references but it made little difference.
They still won't draw independently of each other.

One ship works fine. As soon as 2 come into play it does the same as in the demo file.


I'm just a bit of a noober myself, and I have not fully gone through your code and cannot relly understand it all but I have recently made a health bar on a game I posted here, and run across some quirks with local variables while making a drum machine.

Personally I made the energy bar as a normal actor and did the script for drawing the amount of energy in that bar, using a global value to calculate how it was to be drawn.

As for the local vs global I found that sometimes a local variable when called by more than one actor would give me strange results. I know local variables make code more streamlined and saved memory etc.. but have you tried developing a method for calculating ship hp using global variables?

If that all makes no sense to you then ignore me, as I say I am a bit of a noob myself.
MrB
 
Posts: 41
Joined: Sun Jun 14, 2009 8:41 am
Score: 0 Give a positive score

Re: 2 Energy bars

Postby foleyjo » Sun Aug 01, 2010 6:23 pm

I've given up on drawing them on a canvas and am now using the code you provided Bee-Ant.
Though I have used the getclone2 command for the pointer.

Thanks for the tips
KISS -Keep It Simple Stoopid
foleyjo
 
Posts: 275
Joined: Mon Jul 09, 2007 1:15 pm
Score: 15 Give a positive score

Re: 2 Energy bars

Postby DilloDude » Mon Aug 02, 2010 1:50 am

I think the problem is that canvases don't work properly as clones. When you draw on one clone, all the other clones will copy it.
Image
User avatar
DilloDude
 
Posts: 866
Joined: Tue Jan 24, 2006 9:51 am
Location: Nyerellion
Score: 58 Give a positive score


Return to Game Development

Who is online

Users browsing this forum: No registered users and 1 guest