2013-12-18 47 views
3

我在Sprite工具包中編寫了一個遊戲,並且我遇到了CGPoint的問題,此刻如果手機是肖像,對象從屏幕右側進入並轉到左邊,我需要它從屏幕頂部進入並落到底部,我如何使它從屏幕頂部產生?同時,它也傳播權的那一刻,我想旅行向下這裏是所有相關的「導彈」的代碼..Sprite工具包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]; 
     } 
    } 
} 
} 

回答

1

導彈動畫是簡單,所以你不需要GDPointAddMultiplyByScalar在這種情況下

,如果你需要從頂部移到底部這裏是代碼導彈

- (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]        ] 
    ]]; 
} 

所有你需要的是當你想拍攝導彈時調用此功能

現在你可以添加所有的物理導彈

祝您好運!

+0

我該如何更新?我目前的方法不起作用 – user3110546

+0

如果您從觸碰中調用shootMissileBegin,則每次觸摸屏幕時都會出現導彈,如果您在更新中調用導彈,導彈將每秒出現一次。或者更新它意味着什麼?給我更多的信息,我會幫你 –

+0

嘿,我得到它的工作,乾杯:) – user3110546

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代表什麼。