怎么让球推方?

时间:2014-03-22 16:46:17

标签: ios objective-c sprite-kit

我有一个SKSpriteNode和一个SKShapeNode。我希望SKShapeNode推送SKSpriteNode,因为它的质量比SKSpriteNode大。然而,即使我已经明确地将SKShapeNode的质量设置为更大,但相反似乎正在发生。这是我的两个节点:

    - (void)newBallNode {
// Create the sprite.
SKShapeNode *shape = [[SKShapeNode alloc]init];
//// Color Declarations
UIColor* color = [UIColor colorWithRed: 1 green: 0.114 blue: 0.114 alpha: 1];

//// Oval Drawing
UIBezierPath* ovalPath = [UIBezierPath bezierPathWithOvalInRect: CGRectMake(0, 0, 17, 18)];
[color setFill];
[ovalPath fill];
const CGAffineTransform *transform = NULL;

shape.path = CGPathCreateWithEllipseInRect(CGRectMake(0, 0, 20, 20),transform);

shape.position = CGPointMake(100, 100);
shape.zPosition = 10;
shape.strokeColor = shape.fillColor = [SKColor colorWithRed:0.0/255.0
                                                                 green:128.0/255.0
                                                                  blue:255.0/255.0
                                                                 alpha:1.0];

// Assign a random angle to the ball's velocity.
const CGFloat ballSpeed = 200;
const CGFloat angle = DegreesToRadians(RandomFloat() * 360);
const CGVector velocity = CGVectorMake(cos(angle)*ballSpeed, sin(angle)*ballSpeed);

// Create a circular physics body. It collides with the borders and
// with other balls. It is slightly smaller than the sprite.
SKPhysicsBody *body = [SKPhysicsBody bodyWithCircleOfRadius:30];
body.dynamic = YES;
body.velocity = velocity;
body.friction = 0.0;
body.linearDamping = 0.0;
body.angularDamping = 0.0;
body.restitution = 0.0;
body.mass = 10;
body.categoryBitMask = ballCategory;
body.collisionBitMask = goalCategory | bumperCategory | swipeShapeCategory;
body.contactTestBitMask = 0x0;
shape.physicsBody = body;
[_worldLayer addChild:shape];
   }

-(void)addNewSwipeShape
   {

uint32_t colorCategoryArray[4] = {blueColorCategory, redColorCategory, greenColorCategory, yellowColorCategory};
//NSArray *colorArray = @[[SKColor blueColor], [SKColor redColor], [SKColor greenColor], [SKColor yellowColor]];
int r = self.randomInt;
_swipeShape = [[SKSpriteNode alloc] init];
_swipeShape.name = swipeShapeName;
_swipeShape.size = CGSizeMake(50, 50);
_swipeShape.physicsBody.affectedByGravity = NO;
_swipeShape.zPosition = 10;
_swipeShape.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:_swipeShape.size];
_swipeShape.physicsBody.dynamic = YES;
_swipeShape.physicsBody.friction = 0.2;
_swipeShape.physicsBody.linearDamping = 0.1;
_swipeShape.physicsBody.angularDamping = 0.0;
_swipeShape.physicsBody.restitution = 0;
_swipeShape.physicsBody.mass = .1;
_swipeShape.color = _colorArray[self.randomInt];
_swipeShape.colorBlendFactor = 1;
_swipeShape.position = CGPointMake(self.frame.size.width / 2, self.frame.size.height / 2);
_swipeShape.physicsBody.categoryBitMask = colorCategoryArray[r] | swipeShapeCategory;
_swipeShape.physicsBody.contactTestBitMask = colorCategoryArray[r] | goalCategory | coinCategory;
_swipeShape.physicsBody.collisionBitMask = bumperCategory;
_swipeShape.physicsBody.restitution = 0;
[_worldLayer addChild:_swipeShape];

CGFloat ballSpeed = self.gameLevel / 2;

if (ballSpeed > 0) {
    ballSpeed = 15;
}

const CGFloat angle = DegreesToRadians(RandomFloat() * 360);
const CGVector velocity = CGVectorMake(cos(angle)*ballSpeed, sin(angle)*ballSpeed);
[_swipeShape.physicsBody applyImpulse:velocity atPoint:CGPointMake(0, 30)];

  }

0 个答案:

没有答案