我正在制作我的第一个 iOS 应用程序,但我遇到了一个问题......将对象从屏幕顶部移动到底部。游戏的目标是在移动物体(具有随机位置.y)从屏幕中出来之前杀死它们。如果物体出来,您将失去 3 条生命中的一条。我这样做:
if(movingObject.position.y < 0)
_lives--;
但是当我启动我的应用程序时,屏幕上出现了一个物体,我立即失去了我的 3 条生命......
我该怎么做才能通过移动物体失去最多 1 条生命?
这是我创建新移动对象的代码
double curTime = CACurrentMediaTime();
if (curTime > _nextMovingObjectSpawn)
{
float randSecs = [self randomValueBetween:3 andValue:5];
_nextMovingObjectSpawn = randSecs + curTime;
float randX = [self randomValueBetween:25 andValue:winSize.width/2-20];
float randDuration = [self randomValueBetween:4 andValue:6];
CCSprite *movingObject = [_movingObjects objectAtIndex:_nextMovingObject];
_nextMovingObject++;
if (_nextMovingObject >= _movingObjects.count) _nextMovingObject = 0;
[movingObject stopAllActions];
movingObject.position = ccp(randX, winSize.height+movingObject.contentSize.height/2);
movingObject.visible = YES;
[movingObject runAction:[CCSequence actions: [CCMoveBy actionWithDuration:randDuration position:ccp(0, -winSize.height-movingObject.contentSize.height)], [CCCallFuncN actionWithTarget:self selector:@selector(setInvisible:)], nil]];
}
谢谢!:)