我有一个BaseUnit
类,它有一个方法
- (void)attackUnit:(__weak BaseUnit *)unit
它在一个循环中攻击另一个单位的地方。
当只有一个单位攻击一切都很好并且死单位被解除分配时,但当两个用户互相攻击时,没有人会被永久解除分配。我很确定我在这里有一个保留周期。
我在这个方法中有一个块,它被调用直到敌人用户没有死(是的...因为他从未解除分配:))
我尝试使用“弱强舞”,但仍然没有运气
这是我的代码:
- (void)attackUnit:(__weak BaseUnit *)unit
{
[self removeAllActions];
[_unitNode removeAllActions];
if (_unitType == ERangedUnit)
{
__weak typeof(self) weakSelf = self;
__weak typeof(unit) weakUnit = unit;
__weak typeof (self.parent) wekaSelfParent = self.parent;
SKAction * action = [SKAction runBlock:^
{
__strong typeof(weakUnit) refUnit = weakUnit;
__strong typeof(weakSelf) refSelf = weakSelf;
if ([refUnit isDead])
{
NSLog(@"Attacking Dead Unit ");
}
if ([refUnit isDead] || !unit)
{
[refSelf removeAllActions];
[refSelf->_unitNode removeAllActions];
refSelf->_state = EUnitResting;
}
else
{
__strong typeof(wekaSelfParent) refSelfParent = wekaSelfParent;
SKSpriteNode * projectile = [[ProjectileNode alloc]initWithTexture:_projectile.texture bitmask:_projectile.physicsBody.contactTestBitMask];
[projectile setScale:0.5];
[refSelfParent addChild:projectile];
CGPoint target = [refUnit.parent convertPoint:refUnit.position toNode:refSelfParent];
[projectile setPosition:refSelf.position];
[projectile setZRotation:AngleBetween(projectile.position, target)];
CGFloat time = EuclidianDistance(target, refSelf.position)/400;
[projectile runAction:[SKAction moveTo:target duration:time] completion:^
{
[projectile removeFromParent];
}];
//_state = EUnitResting;
}
}];
[_unitNode runAction:[SKAction repeatActionForever:[SKAction sequence:@[_attackSpeedDelay, _attackBackAction, action]]]];
}
_state = EUnitAttacking;
}
当projectile
命中一个单位时,此代码执行:
- (void)onHitBy:(__weak SKNode *)hitNode
{
_currHp --;
//Play some action.
CGFloat hpFillHeight = CalculateHpFrame(_currHp, _hitPoints, _hpNodeSize.height);
[_hpFillNode runAction:[SKAction resizeToHeight:hpFillHeight duration:0.2]];
if (_currHp <= 0)
{
_isDead = YES;
[_unitNode removeAllActions];
[self removeAllActions];
[self.delegate unitIsDead:self];
[self runAction:[SKAction removeFromParent]];
}
}
CGFloat CalculateHpFrame(CGFloat value, CGFloat hpMax, CGFloat heightMax)
{
CGFloat retVal;
value = (value)/hpMax;
retVal = value * heightMax;
if (retVal<0)
{
retVal = 0;
}
return retVal;
}
使用仪器进行分析显示以下结果: