0
votes

I want my character to move to the right by force. But it is not working on this code below. I don't know what I missed here, but the player stands still on the bottom without any movement. Any clue?

// MyScene.h
-(void)update:(CFTimeInterval)currentTime {
    /* Called before each frame is rendered */

    [self.player.physicsBody applyForce: CGVectorMake(100, 100)];
}


-(void)createSceneContents {
    self.currentBackground = [Background generateBackground];
    [self addChild: self.currentBackground];
    self.physicsWorld.gravity = CGVectorMake(0, _gravity);
    self.physicsWorld.contactDelegate = self;

    Player *player = [[Player alloc]init];
    player.size = CGSizeMake(80, 70);
    player.position = CGPointMake([UIScreen mainScreen].applicationFrame.size.width/2, 40);
    [self addChild:player];
}

//Player.h

#import "Player.h"
#import "common.h"
@implementation Player
- (instancetype)init {
    self = [super initWithImageNamed:@"player.png"];
    self.name = @"player";
    NSLog(@"%f %f", self.size.width, self.size.height);
    self.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(20, 70)];
    self.physicsBody.dynamic = YES;
    self.physicsBody.allowsRotation = NO;
    self.physicsBody.affectedByGravity = YES;
    self.physicsBody.collisionBitMask = ~poopCategory & groundCategory;
    self.physicsBody.categoryBitMask = playerCategory;
    self.physicsBody.contactTestBitMask = poopCategory;
    self.zPosition = 100;
    //self.anchorPoint = CGPointMake(0.5, 0);

    return self;
}
1
What's value of _gravity?Andrey Gordeev
Try to make the force stronger: CGVectorMake(3000, 3000)Andrey Gordeev

1 Answers

2
votes

It looks like you are using a property named player in the scene, but you are initializing a sprite named player but never storing it in the scene's property. NSLog self.player and you'll see it's nil.