我在cocos2d中有以下方法在sprite之间进行一些碰撞检测。 我能够阻止阵列_车辆中的“车辆”精灵,之后这种方法会阻止车辆从同一方向后方飞来。
现在,当我停下车辆并且“几乎碰撞”的动作生效时帧速率很快下降......为什么会这样?
-(void)doCollisionDetection {
if (currentVehicle > 0) {
for (Vehicle *vehicle in _vehicles) {
int i = [_vehicles indexOfObject:vehicle];
CGRect Rect1 = CGRectMake(vehicle.position.x - (vehicle.vehicleWidth/2),
vehicle.position.y - (vehicle.vehicleHeight/2),
vehicle.vehicleWidth,
vehicle.vehicleHeight);
for (Vehicle *vehicletoCheck in _vehicles) {
int l = [_vehicles indexOfObject:vehicletoCheck];
if (l == i) { break; }
CGRect TouchingRect2 = CGRectMake(vehicletoCheck.position.x - (vehicletoCheck.vehicleWidth/2),
vehicletoCheck.position.y - (vehicletoCheck.vehicleHeight/2),
vehicletoCheck.vehicleWidth,
vehicletoCheck.vehicleHeight);
CGRect TouchingRectWithPadding = CGRectMake(vehicletoCheck.position.x - (vehicletoCheck.vehicleWidth/2)-10,
vehicletoCheck.position.y - (vehicletoCheck.vehicleHeight/2)-10,
vehicletoCheck.vehicleWidth + 20,
vehicletoCheck.vehicleHeight + 20);
if (CGRectIntersectsRect(Rect1, TouchingRect2))
{
CCLOG(@"Collision detected: %i is touching %i",i,l);
[[CCDirector sharedDirector] pause];
}
if ( vehicletoCheck.GoDirection == vehicle.GoDirection && CGRectIntersectsRect(Rect1, TouchingRectWithPadding))
{
CCLOG(@"Almost Collided: %i is touching %i",i,l);
vehicletoCheck.state = kVehicleStateStopped;
vehicle.state = kVehicleStateStopped;
break;
}
}
// now check if the vehicle has made it
if (vehicle.position.x < 0-(vehicle.vehicleWidth) || vehicle.position.x > screenSize.width + (vehicle.vehicleWidth) || vehicle.position.y < 0 - (vehicle.vehicleHeight) || vehicle.position.y > screenSize.height + (vehicle.vehicleHeight)) {
[vehicle removeFromParentAndCleanup:YES];
[_vehicles removeObject:vehicle];
}
}
}
}