如何在联系中更改对象

时间:2014-04-24 07:53:30

标签: ios sprite-kit

我在场景中有一个对象,由以下内容决定:

SKSpriteNode* object = [SKSpriteNode spriteNodeWithTexture:_objectTexture];
[object setScale:2];
object.position = CGPointMake(0, y);
object.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:object.size];
object.physicsBody.dynamic = NO;

然后,如果我的角色击中它,它将留在那里。现在,我希望每当角色击中对象时,对象也会移动它的上半部分。它的移动方式取决于索引。

if index value in range 1, object change texture
if index value in range 2, object and character reflex by normal physical contact law
if index value in range 3, object broken into half

我试图把

object.physicsBody.dynamic = YES;
然后我看到它被拉下来并在重力作用下消失了。 如何设置对象动态和条件以满足上述需求? 非常感谢你的帮助!

添加(根据要求):我的委托方法:

@interface MyScene () <SKPhysicsContactDelegate> {
SKSpriteNode* _character;
SKTexture* _object1;
SKTexture* _object2;
SKAction* _moveObjects;
SKNode* _moving;
SKNode* _objects;
BOOL _canRestart;
NSInteger _index;
SKTexture* characterTexture;

SKSpriteNode* _characterSprite;

}

以后的部分(不确定是否相关)

-(id)initWithSize:(CGSize)size{


if (self = [super initWithSize:size]) {
    /*Setup Scene here */


    self.physicsWorld.gravity = CGVectorMake(0.0, -6.0);
    self.physicsWorld.contactDelegate = self;

通过以下方式移动我的物体:

CGFloat distanceToMove = self.frame.size.width + 2 * _object1.size.width;
    SKAction* moveObjects = [SKAction moveByX:-distanceToMove y:0 duration:0.01* distanceToMove];
    SKAction* removeObjects = [SKAction removeFromParent];
    _moveObjectsAndRemove = [SKAction sequence:@[moveObjects,removeObjects]];

Ps:添加didBeginContact:

-(void)didBeginContact:(SKPhysicsContact *)contact{
if (_moving.speed > 0) {

    //Score node
    if ((contact.bodyA.categoryBitMask & scoreCategory) == scoreCategory || (contact.bodyB.categoryBitMask & scoreCategory) == scoreCategory)
    {
        _score++;

        // Add a little visual
        [_scoreLabelNode runAction:[SKAction sequence:@[[SKAction scaleTo:1.5 duration:0.1], [SKAction scaleTo:1.0 duration:0.1]]]];
    }

    else
    //Collided with world
    {            
    _moving.speed = 0;
    _character.physicsBody.collisionBitMask = worldCategory;
    [_character runAction:[SKAction rotateByAngle:M_PI * _character.position.y*0.01 duration:_character.position.y * 0.03] completion:^{_character.speed = 0; }];
     ....   
 }

}

1 个答案:

答案 0 :(得分:0)

您需要设置

object.physicsBody.affectedByGravity = NO;