Sprite kit 向CGPoint SpriteKit应用脉冲

Sprite kit 向CGPoint SpriteKit应用脉冲,sprite-kit,cgpoint,sknode,Sprite Kit,Cgpoint,Sknode,我是SpriteKit的新手,游戏开发者,事实上我只是在学习。所以我到了一个点,我知道如何将一堆节点移动到一个用户点击的位置。到目前为止,我试图计算一个虚拟的直角三角形,得到基于边的角度的正余弦。不幸的是,这给了我一个非常强烈的冲动,没有真正考虑用户点击位置。 有什么想法吗 提前谢谢 在Ray Wenderlich的教程中查找拍摄射弹部分: 将教程中的代码更改如下: -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event

我是SpriteKit的新手,游戏开发者,事实上我只是在学习。所以我到了一个点,我知道如何将一堆节点移动到一个用户点击的位置。到目前为止,我试图计算一个虚拟的直角三角形,得到基于边的角度的正余弦。不幸的是,这给了我一个非常强烈的冲动,没有真正考虑用户点击位置。

有什么想法吗


提前谢谢

在Ray Wenderlich的教程中查找拍摄射弹部分:

将教程中的代码更改如下:

-(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 point to your desired node.

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

    // 4 - Bail out if you are shooting down or backwards. You can ignore this if required.
    if (offset.x <= 0) return;

    // 5 - OK to add now - we've double checked position
    [self addChild:projectile];

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

    // 7 - 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);

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

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

}
-(void)touchesend:(NSSet*)toucheevent:(UIEvent*)event{
//1-选择要使用的触摸之一
UITouch*touch=[触摸任何对象];
CGPoint位置=[触摸位置Innode:self];
//2-设置弹丸的初始位置
SKSpriteNode*射弹=[自身子节点名称:@“desirednode”];
//使投射物指向所需的节点。
//3-确定位置对射弹的偏移
CGPoint offset=rwSub(位置,射弹位置);
//4-如果你正在向下或向后射击,你可以放弃。如果需要,你可以忽略这一点。

如果(offset.x在Ray Wenderlich的教程中查找拍摄射弹部分:

将教程中的代码更改如下:

-(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 point to your desired node.

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

    // 4 - Bail out if you are shooting down or backwards. You can ignore this if required.
    if (offset.x <= 0) return;

    // 5 - OK to add now - we've double checked position
    [self addChild:projectile];

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

    // 7 - 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);

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

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

}
-(void)touchesend:(NSSet*)toucheevent:(UIEvent*)event{
//1-选择要使用的触摸之一
UITouch*touch=[触摸任何对象];
CGPoint位置=[触摸位置Innode:self];
//2-设置弹丸的初始位置
SKSpriteNode*射弹=[自身子节点名称:@“desirednode”];
//使投射物指向所需的节点。
//3-确定位置对射弹的偏移
CGPoint offset=rwSub(位置,射弹位置);
//4-如果你正在向下或向后射击,你可以放弃。如果需要,你可以忽略这一点。

如果(offset.x)这不完全是你想要的,但将是一个开始谢谢你的建议。我肯定我可以在某个地方使用它。这不完全是你想要的,但将是一个开始谢谢你的建议。我肯定我可以在某个地方使用它。谢谢你的所有帮助,太好了。谢谢你的所有帮助,太好了。