Page 1 of 1

2 Energy bars

PostPosted: Sat Jul 31, 2010 4:34 pm
by foleyjo
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);
}

Re: 2 Energy bars

PostPosted: Sun Aug 01, 2010 2:11 am
by Bee-Ant
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

Re: 2 Energy bars

PostPosted: Sun Aug 01, 2010 8:33 am
by foleyjo
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

Re: 2 Energy bars

PostPosted: Sun Aug 01, 2010 8:57 am
by Bee-Ant
Are xstart and xend actor variables?
If yes, they should work...
Also, what do you do to both after you set their value?

Re: 2 Energy bars

PostPosted: Sun Aug 01, 2010 9:11 am
by foleyjo
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

Re: 2 Energy bars

PostPosted: Sun Aug 01, 2010 9:18 am
by Bee-Ant
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);
}

Re: 2 Energy bars

PostPosted: Sun Aug 01, 2010 9:48 am
by foleyjo
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);
 }

Re: 2 Energy bars

PostPosted: Sun Aug 01, 2010 9:55 am
by Bee-Ant
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;

Re: 2 Energy bars

PostPosted: Sun Aug 01, 2010 10:19 am
by foleyjo
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.

Re: 2 Energy bars

PostPosted: Sun Aug 01, 2010 11:34 am
by MrB
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.

Re: 2 Energy bars

PostPosted: Sun Aug 01, 2010 6:23 pm
by foleyjo
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

Re: 2 Energy bars

PostPosted: Mon Aug 02, 2010 1:50 am
by DilloDude
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.