我想要像愤怒的小鸟一样创造一个弹弓,我的问题是,当我加载已发射的弹片并挂在我的弹弓上时,如何创建一个跟随我的精灵(例如一只鸟)的弹性物?
我怎样才能用SpriteKit做到这一点?
感谢您的帮助。
答案 0 :(得分:0)
这种逻辑编码你真的需要自己解决。我会给你一些合作的东西。这来自我的个人原型:
-(void)fireRocket
{
if (timesRan <= 0) {
timesRan++;
}
else
{
[self enumerateChildNodesWithName:@"rocket" usingBlock: ^(SKNode *node, BOOL *stop) {
SKSpriteNode *rocket = (SKSpriteNode *) node;
[rocket removeFromParent];
[rocket removeAllActions];
}];
}
SKSpriteNode * rocket = [SKSpriteNode spriteNodeWithImageNamed:@"rocket-310663_150"];
rocket.size = CGSizeMake(30, 32);
rocket.name = @"rocket";
rocket.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:16];
rocket.position = CGPointMake(50, 185);
//CGFloat angle = M_PI_4;
CGFloat magnitude = 26;
CGFloat angle = [self angleOfRocketFire];
//CGFloat angle = M_PI_4;
[self addChild:rocket];
[rocket.physicsBody applyImpulse:CGVectorMake(magnitude*cos(angle),
magnitude*sin(angle))];
}
-(CGFloat)angleOfRocketFire
{
__block CGFloat angle;
__block CGPoint handlePosition;
[self enumerateChildNodesWithName:@"handle" usingBlock: ^(SKNode *node, BOOL *stop) {
SKSpriteNode *handle = (SKSpriteNode *) node;
CGFloat xCoord = handle.position.x + (handle.size.width / 2);
CGFloat yCoord = handle.position.y + (handle.size.height / 2);
handlePosition = CGPointMake(xCoord, yCoord);
}];
[self enumerateChildNodesWithName:@"rocketLauncher" usingBlock: ^(SKNode *node, BOOL *stop) {
SKSpriteNode *rocketLauncher = (SKSpriteNode *) node;
CGFloat xCoord = rocketLauncher.position.x + (rocketLauncher.size.width / 2);
CGFloat yCoord = rocketLauncher.position.y + (rocketLauncher.size.height / 2);
CGPoint launcherPosition = CGPointMake(xCoord, yCoord);
CGFloat slope = (handlePosition.y - launcherPosition.y) / (handlePosition.x - launcherPosition.x);
//angle = atan(slope);
angle = atan(slope);
NSLog(@"%f", slope);
}];
return angle;
}