SpriteKit applyForce not working - ios

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

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.

Related

iOS SpriteKit - ray(line) is reflected from bounds

We have line(ray) which moves to bounds and reflects when bound is reached. These images demonstrates the dynamic of movement and reflections.
And i'd like to implement it in SpriteKit(obj-c is prefer) but don't understand from which point I should start.
I found how to implement it. Hope it'll be useful for others
#import "GameScene.h"
static const float GUIDE_MASS = .0015;
static const int SEGMENTS_COUNT = 10;
static const int SEGMENT_LENGTH = 5;
#interface GameScene(private)
-(void) createGuidesAndShot;
#end
#implementation GameScene
SKShapeNode *shot;
NSMutableArray* shotSegments;
-(void)didMoveToView:(SKView *)view {
[self setBackgroundColor:[SKColor whiteColor]];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
/* Called when a touch begins */
}
-(void)update:(CFTimeInterval)currentTime {
CGMutablePathRef pathToDraw = CGPathCreateMutable();
if (shotSegments!=nil) {
bool isFirst = YES;
for (SKNode* segment in shotSegments) {
if(isFirst){
CGPathMoveToPoint(pathToDraw, NULL,
segment.position.x,
segment.position.y);
isFirst =NO;
} else {
CGPathAddLineToPoint(pathToDraw, NULL,
segment.position.x,
segment.position.y);
}
}
shot.path = pathToDraw;
}
}
-(void)didBeginContact:(SKPhysicsContact *)contact {
uint32_t collision = (contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask);
if (collision == (shotCategory|screenBoundsCategory)){
}
}
-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
shotSegments = [NSMutableArray new];
self.physicsWorld.gravity = CGVectorMake(0.0f, 0.0f);
SKPhysicsBody* borderBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
self.physicsBody = borderBody;
self.physicsBody.friction = 0.0f;
self.physicsBody.categoryBitMask = screenBoundsCategory;
self.physicsBody.contactTestBitMask = shotCategory;
self.physicsWorld.contactDelegate = self;
[self createGuidesAndShot];
}
return self;
}
-(void) createGuidesAndShot{
for (int i = 0; i<SEGMENTS_COUNT*SEGMENT_LENGTH; i+=SEGMENT_LENGTH) {
SKShapeNode* guide = [SKShapeNode shapeNodeWithCircleOfRadius:1];
guide.position = CGPointMake(self.frame.size.width/2+i,
self.frame.size.height/2-i);
// guide.fillColor = [SKColor blackColor];
guide.name = [NSString stringWithFormat:#"guide%i", i];
[self addChild:guide];
[shotSegments addObject:guide];
guide.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:guide.frame.size.width/2];
guide.physicsBody.friction = 0.0f;
guide.physicsBody.restitution = 1.0f;
guide.physicsBody.linearDamping = 0.0f;
guide.physicsBody.allowsRotation = NO;
guide.physicsBody.categoryBitMask = shotCategory;
guide.physicsBody.contactTestBitMask = screenBoundsCategory;
guide.physicsBody.collisionBitMask = screenBoundsCategory;
guide.physicsBody.mass = GUIDE_MASS;
[guide.physicsBody applyImpulse:CGVectorMake(0.1f, -0.1f)];
}
shot = [SKShapeNode node];
[shot setStrokeColor:[UIColor redColor]];
[self addChild:shot];
}
#end

Sprite Kit didBeginContact not called between body and edgeRect

I have a game where I'm flicking bodies off the screen. I want to detect when these bodies are coming into contact with the edges (still letting the bodies pass through to off the screen) of the screen and I've implemented everything but still it is not working..here's my code:
GameScene.h
typedef NS_OPTIONS(uint32_t, FAPhysicsCategory) {
FAWallCategory = 1 << 0,
FABottomWallCategory = 1 << 1,
FAAnimalCategory = 1 << 2,
};
GameScene : SKScene <SKPhsyicsContactDelegate>
GameScene.m
-(void)setupWorldPhysics
{
// Set world gravity
self.physicsWorld.gravity = CGVectorMake(0.0, -5.0);
float bottomOffset = 400.0;
CGRect newFrame = CGRectMake(0, -bottomOffset, self.frame.size.width, self.frame.size.height + bottomOffset);
self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:newFrame];
self.physicsBody.categoryBitMask = FAWallCategory;
self.physicsBody.contactTestBitMask = FAAnimalCategory;
self.physicsBody.collisionBitMask = 0;
}
-(void)launchBody {
SKSpriteNode* animalSprite = [SKSpriteNode spriteNodeWithImageNamed:[animalImagesArray objectAtIndex:animalChoice]];
animalSprite.position = CGPointMake(self.size.width/2, -animalSprite.size.height);
animalSprite.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:25];
animalSprite.physicsBody.dynamic = YES;
animalSprite.physicsBody.allowsRotation = YES;
animalSprite.name = #"animalSprite";
animalSprite.physicsBody.categoryBitMask = FAAnimalCategory;
animalSprite.physicsBody.contactTestBitMask = FAWallCategory;
animalSprite.physicsBody.collisionBitMask = 0;
animalSprite.physicsBody.usesPreciseCollisionDetection = YES;
[self addChild:animalSprite];
}
Alright so it's late and I'm exhausted and I feel like an idiot..i was missing:
self.physicsWorld.contactDelegate = self;
tool

spritekit didBeginContact not called

didBeginContact doesnt get called for some reason. How can I fix this? Thanks! I set the category bitmasks and the skphysicsContactDelegate, yet it is still not registering contacts. I've been stuck at this for some time now.
#import "MyScene.h"
#import "FuelNode.h"
#import "SKSpriteNode+DebugDraw.h"
typedef NS_OPTIONS(uint32_t, CollisionCategory) {
CollisionCategoryPlayer = 1 << 0,
CollisionCategoryFuel = 1 << 1,
};
#interface MyScene() <SKPhysicsContactDelegate>
#end
#implementation MyScene
{
SKNode *_playerNode;
SKNode *_backgroundNode;
SKNode *_foreGround;
}
-(id)initWithSize:(CGSize)size
{
if (self = [super initWithSize:size]) {
self.physicsWorld.contactDelegate = self;
_backgroundNode = [self createBackground];
[self addChild:_backgroundNode];
_foreGround = [SKNode node];
[self addChild:_foreGround];
//add a fuelNode
FuelNode *fuel = [self createFuelAtPosition:CGPointMake(160, 440)];
[_foreGround addChild:fuel];
_playerNode = [self createPlayer];
[_foreGround addChild:_playerNode];
SKAction *actionMove = [SKAction moveToY:-100 duration:3.0];
[fuel runAction:actionMove];
NSLog(#"yea");
}
return self;
}
-(SKNode *)createPlayer
{
CGSize playerPhysicsBody;
//Create player
SKNode *playerNode = [SKNode node];
SKSpriteNode *player = [SKSpriteNode spriteNodeWithImageNamed:#"wship-3.png"];
player.position = CGPointMake(self.size.width/2, 120);
[playerNode addChild:player];
//Add physics
playerPhysicsBody = CGSizeMake(player.size.width/2, player.size.height/2);
playerNode.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:playerPhysicsBody];
playerNode.physicsBody.dynamic = NO;
//Setup collision settings
playerNode.physicsBody.usesPreciseCollisionDetection = YES;
playerNode.physicsBody.categoryBitMask = CollisionCategoryPlayer;
playerNode.physicsBody.collisionBitMask = 0;
playerNode.physicsBody.contactTestBitMask = CollisionCategoryFuel;
[player attachDebugRectWithSize:playerPhysicsBody];
return playerNode;
}
-(SKNode *)createBackground
{
//Create background
SKNode *bgNode = [SKNode node];
SKSpriteNode *bg = [SKSpriteNode spriteNodeWithImageNamed:#"purple"];
bg.anchorPoint = CGPointZero;
[bgNode addChild:bg];
return bgNode;
}
- (FuelNode *)createFuelAtPosition:(CGPoint)position
{
// 1
FuelNode *node = [FuelNode node];
[node setPosition:position];
[node setName:#"NODE_FUEL"];
// 2
SKSpriteNode *sprite;
sprite = [SKSpriteNode spriteNodeWithImageNamed:#"fuelBlue"];
[node addChild:sprite];
// 3
CGSize contactSize = CGSizeMake(sprite.size.width/2, sprite.size.height/2);
node.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:contactSize];
// 4
node.physicsBody.dynamic = NO;
//Setup collision settings
node.physicsBody.categoryBitMask = CollisionCategoryFuel;
node.physicsBody.collisionBitMask = 0;
//node.physicsBody.contactTestBitMask = CollisionCategoryPlayer;
[sprite attachDebugRectWithSize:contactSize];
//SKAction *actionMove = [SKAction moveToY:-100 duration:3.0];
//[node runAction:actionMove];
return node;
}
-(void)didBeginContact:(SKPhysicsContact *)contact
{
BOOL fuelCollision = NO;
SKNode *other = (contact.bodyA.node != _playerNode) ? contact.bodyA.node : contact.bodyB.node;
NSLog(#"collision");
fuelCollision = [(GameObjectNode *)other collisionWithPlayer:_playerNode];
}
#end
In order to enable contact detection, you need to set
node.physicsBody.dynamic = YES;
Have a look at the documentation as well.
Fixed! In the create player method I have mistakenly set the position of the player sprite instead of setting the position of the player node!

Spritekit drastic frame rate drop

I have tried my best to boil this question down as simple as possible. I have a coin object in my game:
#implementation
-(CollectableCoin*)initWithLocation:(CGPoint) Location andValue: (int) val
{
self = [super initWithImageNamed:#"coin"];
[self setScale:.35];
_value = val;
_collected = false;
self.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:self.size];
self.physicsBody.categoryBitMask = APAColliderTypeCoin;
self.physicsBody.collisionBitMask = APAColliderTypeBall;
self.physicsBody.mass = 0.00009;
self.physicsBody.restitution = .35;
self.position = Location;
self.name = #"collectableCoin";
return self;
}
#end
I also have a shelf object:
#implementation Shelf
-(Shelf*)initWithLocation:(CGPoint) location andWidth:(NSInteger) width
{
self = [super initWithImageNamed:#"shelf"];
if(self)
{
self.size = CGSizeMake(width, HEIGHT);
self.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:self.size];
self.physicsBody.dynamic = false;
self.physicsBody.restitution = 0;
self.position = location;
self.name = #"shelf";
SKSpriteNode* topOfShelf;
if(width > 5)
topOfShelf = [[SKSpriteNode alloc] initWithColor:[UIColor yellowColor] size:CGSizeMake(width-2, 1)];
else
topOfShelf = [[SKSpriteNode alloc] initWithColor:[UIColor yellowColor] size:CGSizeMake(width, 1)];
topOfShelf.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:topOfShelf.size];
topOfShelf.physicsBody.restitution = 1;
topOfShelf.physicsBody.dynamic = false;
topOfShelf.position = CGPointMake(0, location.y + self.size.height/2);
NSLog([NSString stringWithFormat:#"%f", location.y + self.size.height/2]);
NSLog([NSString stringWithFormat:#"%f", location.y]);
topOfShelf.name = #"shelf";
[self addChild:topOfShelf];
}
return self;
}
#end
I create a scene like so:
-(id)initWithSizeTest:(CGSize)size
{
self.physicsWorld.gravity = CGVectorMake(0, 0);
_gameState = READYTOSTART;
self.physicsWorld.contactDelegate = self;
if (self = [super initWithSize:size])
{
self.physicsWorld.gravity = CGVectorMake(0, 0);
for(int i = 0; i < 25; i++)
{
CollectableCoin* orb = [[CollectableCoin alloc] initWithLocation:CGPointMake(i*10, self.size.height*.75) andValue:1];
[self addChild:orb];
}
Shelf* shelf = [[Shelf alloc] initWithLocation:CGPointMake(self.size.width/2, self.size.height/2) andWidth:self.size.width];
[self addChild:shelf];
}
return self;
}
Here is the touchesBegan method:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
/* Called when a touch begins */
if(_gameState == READYTOSTART)
{
self.physicsWorld.gravity = CGVectorMake(0, -2.0);
_gameState = PLAYING;
[[self childNodeWithName:#"taptostart"] removeFromParent];
}
When the scene starts, I have a row of coins hovering above a shelf, gravity is disabled, and I have a solid 60fps. When I tap the screen, the touchesbegan function enables gravity and the coins fall on the shelf, and the frame rate drops to 5fps. The didBeginContact function is not being called because the shelf object is not dynamic nor does it have contact or collision bitmasks, so I am fairly sure that it is not being overloaded by extraneous calls to didBeginContact. This happens on an iPad mini and an iPhone 4s, but not in any of the simulators. This is a very simple example of the actual problem that I am having. Anyone have any insights?

- (void)didBeginContact:(SKPhysicsContact *) refuses to fire

1) I set the delegate protocol on the SKScene header:
#interface WorldScene : SKScene <SKPhysicsContactDelegate>
2) I set delegate to the physics world:
- (id) init
{
self = [super init];
if (self)
{
self.physicsWorld.gravity = CGVectorMake(0,0);
self.physicsWorld.contactDelegate = self;
self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
}
return self;
}
3) I set my bitmasks:
static const uint32_t playerCategory = 0x1 << 0;
static const uint32_t wallsCategory = 0x1 << 1;
static const uint32_t endCategory = 0x1 << 2;
4) I make my player:
SKSpriteNode *player = [[SKSpriteNode alloc] initWithColor:[SKColor blueColor] size:CGSizeMake(PLAYERSIZE, PLAYERSIZE)];
player.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:player.size];
player.name = #"player";
player.physicsBody.dynamic = YES;
player.physicsBody.categoryBitMask = playerCategory;
player.physicsBody.collisionBitMask = wallsCategory;
player.physicsBody.contactTestBitMask = endCategory;
5) I make my end tile:
SKSpriteNode *tile = [[SKSpriteNode alloc] initWithColor: [SKColor whiteColor] size:CGSizeMake(TILESIZE,TILESIZE)];
if ([cell isEnd]){
tile.color = [SKColor greenColor];
tile.name = #"endTile";
tile.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize: tile.size];
NSLog(#"END TILE OF SIZE %f x %f", tile.size.width, tile.size.height);
tile.physicsBody.dynamic = YES;
tile.physicsBody.affectedByGravity = NO;
tile.physicsBody.categoryBitMask = endCategory;
tile.physicsBody.collisionBitMask = 0;
tile.physicsBody.contactTestBitMask = playerCategory;
}
6) But this never gets called! :(
- (void)didBeginContact:(SKPhysicsContact *)contact
{
NSLog(#"Touched!");
[self removeAllChildren];
}
I'm making a maze game. The player currently collides with all the walls fine, but I want the game to end when it reaches the last tile. Currently, the contact event isn't being called. I don't really know what's wrong, I have my debugger output set to displaying all output (and in any case all the children should be getting removed if it reaches the event anyway).
Whyyyyyy
SOLVED -- turns out init was never being called, so the delegate was not actually being set. Moved all the init code to - (void) didMoveToView:(SKView *)view and it began working just fine.

Resources