ios - SpriteKit 两个物体弹跳。

标签 ios iphone objective-c sprite-kit

所以,我在 SpriteKit 中有一个游戏,涉及圆形对象,并且由玩家控制。

问题:

当玩家点击或沿着屏幕拖动时,第一个对象也会随之移动。 第二个对象不受玩家或计算机控制,而是一个可以弹跳的物理对象。然而,启用物理体只能让我插入它,但它不会反弹。我将两个物体的恢复设置为 1.0,摩擦力设置为 0.0。没有重力。我移动玩家对象的方式是在玩家点击或拖动时调用移动操作的 runAction ,这似乎让第二个对象被插入。我觉得我错过了一些东西,可能是显而易见的。

最佳答案

使用 SKAction move 只会让你的节点移动到特定的点。如果你想让你的播放器节点朝着触摸点的方向移动,那么你需要使用applyImpulse。以下是实现该目标的方法:

在此处查看 Ray Wenderlich 教程中的射弹部分:

http://www.raywenderlich.com/42699/spritekit-tutorial-for-beginners

从教程中复制 rwAdd、rwSub、rwMult、rwLength 和 rwNormalize 方法。

然后,在您的 -touchesEnded 方法中:

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

    // 1 - Choose one of the touches to work with
    UITouch * touch = [touches anyObject];
    CGPoint location = [touch locationInNode:self];

    // 2 - Set up initial location of projectile
    SKSpriteNode * projectile = [self childNodeWithName:@"desirednode"];
    //make projectile variable point to your desired node.

    // 3- Determine offset of location to projectile
    CGPoint offset = rwSub(location, projectile.position);

    // 4 - Get the direction of where to shoot
    CGPoint direction = rwNormalize(offset);

    // 5 - Make it shoot far enough to be guaranteed off screen
    float forceValue = 200; //Edit this value to get the desired force.
    CGPoint shootAmount = rwMult(direction, forceValue);

    //6 - Convert the point to a vector
    CGVector impulseVector = CGVectorMake(shootAmount.x, shootAmount.y);
    //This vector is the impulse you are looking for.

    //7 - Make the node ignore it's previous velocity (ignore if not required)
    projectile.physicsBody.velocity = CGVectorMake(0.0, 0.0);

    //9 - Apply impulse to node.
    [projectile.physicsBody applyImpulse:impulseVector];

}

代码中的射弹对象代表您的节点。此外,您还需要编辑 forceValue 以获得所需的冲量。

关于ios - SpriteKit 两个物体弹跳。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22675889/

相关文章:

ios - 无粘贴:canPerformAction:withSender中的操作:

ios - 如何控制音乐播放器的耳机?

ios - 从 UITextField 收集用户输入并将其存储在某个地方 - Xcode

objective-c - 调用新窗口时是否可以显示停靠图标?

ios - 64 位 iOS 设备中的 UIViewControllerHierarchyInconsistency

ios - 在 Objective-C 中设置日期格式

ios - 尝试在 UIView 中为 SKNode 设置动画

iPhone 框架

iphone - sdwebImage setImageWithURL : placeholderImage: completed:

iOS 存储 CLLocation 核心数据作为可转换的 NSKeyedUnarchiveFromDataTransformerName 错误 CoreData : warning: no NSValueTransformer