我制作了这个我可以抓取并四处移动的 Sprite 。 我的问题是我希望能够“抛出” Sprite 。意思是,当我释放 Sprite 时,我希望它继续沿着我移动它的方向前进。就像扔球一样。
我该怎么办?
@implementation NPMyScene
{
SKSpriteNode *sprite;
}
-(id)initWithSize:(CGSize)size
{
if (self = [super initWithSize:size])
{
self.backgroundColor = [SKColor colorWithRed:0.15 green:0.15 blue:0.3 alpha:1.0];
sprite.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:sprite.size.width/2];
sprite.physicsBody.dynamic = YES;
self.scaleMode = SKSceneScaleModeAspectFit;
sprite = [SKSpriteNode spriteNodeWithImageNamed:@"GreenBall"];
sprite.position = CGPointMake(CGRectGetMidX(self.frame),
CGRectGetMidY(self.frame));
sprite.physicsBody.velocity = self.physicsBody.velocity;
sprite.physicsBody.affectedByGravity = false;
sprite.physicsBody.dynamic = true;
sprite.physicsBody.friction = 0;
[sprite.physicsBody isDynamic];
[sprite.physicsBody allowsRotation];
[self addChild:sprite];
}
return self;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self touchesMoved:touches withEvent:event];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
[sprite runAction:[SKAction moveTo:[[touches anyObject] locationInNode:self]duration:0.21]];
}
最佳答案
如果您正在谈论将物理应用于球,那么您需要更深入地思考 Sprite Kit 如何处理物理。
当您指定“moveTo” Action 时,实际上您根本没有使用 Sprite 套件的物理引擎。您只需指定一个点来为该 Sprite 设置动画。
要达到您正在寻找的效果,您应该做的是将 Sprite 的位置附加到您的手指上,因为它在屏幕上四处移动,如下所示:
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
sprite.position = [[touches anyObject] locationInNode:self];
}
然后当手指抬起时,您需要计算手指刚刚移动的方向和速度,并使用 applyForce< 向 Sprite 的物理体施加适当的
方法:力
/
[sprite.physicsBody applyForce:calculatedForce];
您将需要弄清楚如何计算要施加的力,但请尝试使用 applyForce 方法并查看您能够从 touchedMoved: 事件返回哪些信息以帮助您施加该力。希望对您有所帮助。
关于ios - Sprite 套件移动 Sprite ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23628155/