Sprite工具包CGPoint无法按预期工作

时间:2013-12-18 12:20:30

标签: ios objective-c game-engine game-physics sprite-kit

我正在使用Sprite Kit编写一个游戏,我遇到了CGPoint的问题,此时如果手机是纵向的,对象从屏幕右侧进入并向左移动,我需要它来从屏幕顶部进入底部,如何从屏幕顶部生成?此刻它也向右行驶,我想向下行驶这里是所有与“导弹”相关的代码..

static inline CGPoint CGPointAdd(const CGPoint a, const CGPoint b)
{
return CGPointMake(a.x + b.x, a.y + b.y);
}

static inline CGPoint CGPointMultiplyScalar(const CGPoint a, const CGFloat b)
{
return CGPointMake(a.x  * b, a.y * b);
}



-(void)addMissile
{
//initalizing spaceship node
SKSpriteNode *missile;
missile = [SKSpriteNode spriteNodeWithImageNamed:@"red-missile.png"];
[missile setScale:.01];

//Adding SpriteKit physicsBody for collision detection
missile.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:missile.size];
missile.physicsBody.categoryBitMask = obstacleCategory;
missile.physicsBody.dynamic = YES;
missile.physicsBody.contactTestBitMask = shipCategory;
missile.physicsBody.collisionBitMask = 0;
missile.physicsBody.usesPreciseCollisionDetection = YES;
missile.name = @"missile";

//selecting random y position for missile
//int t = arc4random() % 300;
missile.position = CGPointMake(100, 100);

[self addChild:missile];
}



- (void)moveObstacle
{
    NSArray *nodes = self.children;//1

for(SKNode * node in nodes){
    if (![node.name  isEqual: @"bg"] && ![node.name  isEqual: @"ship"]) {
        SKSpriteNode *ob = (SKSpriteNode *) node;
        CGPoint obVelocity = CGPointMake(-OBJECT_VELOCITY, 0);
        CGPoint amtToMove = CGPointMultiplyScalar(obVelocity,_dt);

        ob.position = CGPointAdd(ob.position, amtToMove);
        if(ob.position.y < 100)
        {
            [ob removeFromParent];
        }
    }
}
}

2 个答案:

答案 0 :(得分:1)

试试这段代码:

- (void)moveObstacle
{
    NSArray *nodes = self.children;//1

    for(SKNode * node in nodes){
        if (![node.name  isEqual: @"bg"] && ![node.name  isEqual: @"ship"]) {
            SKSpriteNode *ob = (SKSpriteNode *) node;
            CGPoint obVelocity = CGPointMake(0, -OBJECT_VELOCITY);
            CGPoint amtToMove = CGPointMultiplyScalar(obVelocity,_dt);

            ob.position = CGPointAdd(ob.position, amtToMove);
            if(ob.position.y < 0)
            {
                [ob removeFromParent];
            }
        }
    }
}

虽然我不知道_dt代表什么。

答案 1 :(得分:1)

导弹动画很简单,因此在这种情况下你不需要 GDPointAdd MultiplyByScalar

如果你需要一个从上到下移动的导弹,那就是代码

- (void)shootMissile {
     // Sprite Kit knows that we are working with images so we don't need to pass the image’s extension
     SKSpriteNode *missile = [SKSpriteNode spriteNodeWithImageNamed:@"red-missile"];
     // Position the missile outside the top
     missile.position = CGPointMake(self.size.width/2, self.size.height + missile.size.height/2);
     // Add the missile to the scene
     [self addChild:missile];

     // Here is the Magic
     // Run a sequence
     [missile runAction:[SKAction sequence:@[
         // Move the missile and Specify the animation time
         [SKAction moveByX:0 y:-(self.size.height + missile.size.height) duration:3],
         // When the missile is outside the bottom
         // The missile will disappear
         [SKAction removeFromParent]                               ]
     ]];
}

当您想要射击导弹时,您只需要调用此功能

现在你可以将所有物理学添加到导弹

祝你好运!!