Thinking about the vector operation -- sometimes it is necessary to use!

Source: Internet
Author: User

To achieve an effect today, each object may have several effects of blood reduction, that is, a blood reduction value is floating up

Vector is the first choice because the maximum value is unknown.

Paste the basic code first:

 

/***//**
* Administrators with reduced blood Effects
* @ Author OK
*
*/
Public class decbloodmanager ...{
Vector vbloods;

Public decbloodmanager (tank )...{
Vbloods = new vector ();
}

/***//**
* Render all blood loss
* @ Param g
* @ Param ox
* @ Param Oy
*/
Public void render (Graphics g, int ox, int Oy )...{
Decblood DB = NULL;

For (INT I = vbloods. Size (); -- I> = 0 ;)...{
DB = (decblood) vbloods. elementat (I );
DB. Render (G, ox, Oy );
}
}

Public void add (decblood dB )...{
Vbloods. addelement (db );
}

Public void remove (decblood dB )...{
Vbloods. removeelement (db );
}

Public void clear ()...{
Vbloods. removeallelements ();
}
}

 

/***//**
* Blood Reduction Effect
* @ Author OK
*
*/
Public class decblood ...{
Decbloodmanager dBm;

Private int X, Y;

Private int lifecounter;

Public decblood (decbloodmanager dBm )...{
This. DBM = dBm;
}

Public void setposition (INT wx, int WY )...{
X = wx;
Y = WY;
}

Public void setlifecounter (INT max )...{
Lifecounter = max;
}

Public void render (Graphics g, int ox, int Oy )...{
// Render


// Update
If (-- lifecounter <0 )...{
// Life over
DBm. Remove (this );
}
}
}

 

The basic idea is as follows:

Render all subnodes in the Manager, and update the status when each subnode is rendered. If the subnode reaches the specified time, it is automatically destroyed; that is, remove yourself from the manager;

However, after the vector operation is removed, the vector. Size () will be reduced immediately. Therefore, it may cause dislocation.

In this case, if ++ is used,

For (INT I = 0; I <v. Size (); I ++ );

This may cause potential risks;

However, if we use --, after removing one, we will only move the elements behind this index forward, and these elements are already round-robin, the previous elements that do not need to be moved are just the nodes that we haven't checked in this cycle;

It seems that sometimes "--" must be used!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.