Merge pull request #1172 from walzer/gles20

fix the out-of-range crash in CCArray::randomObject()
This commit is contained in:
Walzer 2012-08-11 08:34:58 -07:00
commit d99b7463b7
1 changed files with 12 additions and 2 deletions

View File

@ -299,9 +299,19 @@ CCObject* CCArray::lastObject()
CCObject* CCArray::randomObject()
{
if(data->num==0) return NULL;
if (data->num==0)
{
return NULL;
}
return data->arr[(int)(data->num*CCRANDOM_0_1())];
float r = CCRANDOM_0_1();
if (r == 1) // to prevent from accessing data-arr[data->num], out of range.
{
r = 0;
}
return data->arr[(int)(data->num * r)];
}
bool CCArray::containsObject(CCObject* object)