在我的项目中,我希望使用_character
方法移动applyForce:
,我希望在我的手指有_character
帮助的情况下,将力量持续应用于button
但我也不希望velocity
超过一定数量。
我尝试过以下方法:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:location];
if ([node.name isEqualToString:@"moveRightButton"]){
_held = YES;
for (SKPhysicsContact* contact in [self.contactQueue copy]){
[self handleContact:contact];
[self.contactQueue removeObject:contact];
}
while (_held == YES) {
[_character.physicsBody applyForce:CGVectorMake(1000, 0)];
}
}
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:location];
if ([node.name isEqualToString:@"moveRightButton"]){
_held = NO;
}
}
_held
是全班可以使用的ivar。
现在,当我运行此操作时,我遇到了某种类型的内存泄漏,导致它使用GB的内存,大约3分钟内达到7 GB(可能是因为我使用CGVectoryMake:
而不是设置CGVector每次都没有创建的变量)当我点击按钮时不再发生任何事情,但是当我没有使用while语句时它工作得很好但我不得不连续按下它来到达某个地方。
答案 0 :(得分:0)
回答如何限制物体速度的问题:
if(mySpriteA.physicsBody.velocity.dx > 50)
mySpriteA.physicsBody.velocity = CGVectorMake(50, mySpriteA.physicsBody.velocity.dy);
if(mySpriteA.physicsBody.velocity.dx < -50)
mySpriteA.physicsBody.velocity = CGVectorMake(-50, mySpriteA.physicsBody.velocity.dy);
if(mySpriteA.physicsBody.velocity.dy > 50)
mySpriteA.physicsBody.velocity = CGVectorMake(mySpriteA.physicsBody.velocity.dx, 50);
if(mySpriteA.physicsBody.velocity.dy < -50)
mySpriteA.physicsBody.velocity = CGVectorMake(mySpriteA.physicsBody.velocity.dx, -50);