Moving SKNodes and SKSpriteNodes - objective-c

I have the following setup in my game:
An SKNode with the name _backgroundLayer
A repeating texture which I have added to the _backgroundLayer 9 times to make a larger background
An SKSprite with the name levelButton, which is added to the _backgroundLayer ([_backgroundLayer addChild:levelButton];).
I use levelButton.anchorPoint = CGPointMake(0.5, 0.5); to make the levelButton have an anchor point in the middle.
Now, when I make levelButton.position = CGPointMake(0, 0); and _backgroundLayer.position = CGPointMake(0, 0); the levelButton's middle is correctly situated in (0, 0) with its middle being the in lower left corner of the screen. So that's fine.
However, if I move the levelButton to be levelButton.position = CGPointMake(100, 0); and _backgroundLayer.position = CGPointMake(-100, 0);, as I show below, the levelButton should still have its middle in (0,0) aka the lower left corner of the screen. However, that is not the case, and the levelButton is more to the right, its something like 50 pixels to the right. But it shouldn't be, since I am moving the _backgroundLayer 100 to the left (-100) and _levelButton 100 to the right (100). It should have stayed in place.
These are basic stuff which I do not understand why they are not working as they should. I am probably doing something wrong but I can not find it even though I've read through the iOS Games by Tutorials and a bunch of tutorials and tips.
Please help.
Now, my code is the following:
#implementation LevelSelectScene
{
SKNode *_backgroundLayer;
}
-(id)initWithSize:(CGSize)size {
/* Setup your scene here */
_backgroundLayer = [SKNode node];
_backgroundLayer.name = #"backgroundLayer";
SKTexture *backgroundTexture = [SKTexture textureWithImageNamed:#"levelSelect"];
int textureID = 0;
for (int i = 0; i<3; i++) {
for (int j = 0; j<3; j++) {
SKSpriteNode *background = [SKSpriteNode spriteNodeWithTexture:backgroundTexture];
background.anchorPoint = CGPointZero;
background.position = CGPointMake((background.size.width)*i,
(background.size.height)*j);
background.zPosition = 0;
background.name = [NSString stringWithFormat:#"background%d", textureID];
textureID++;
[_backgroundLayer addChild:background];
}
}
[self addChild:_backgroundLayer];
SKSpriteNode * levelButton = [SKSpriteNode spriteNodeWithImageNamed:#"lock"];
levelButton.anchorPoint = CGPointMake(0.5, 0.5);
levelButton.position = CGPointMake(100, 0); //IMPORTANT
levelButton.zPosition = 150;
levelButton.name = #"test";
[_backgroundLayer addChild:levelButton];
_backgroundLayer.position = CGPointMake(-100, 0); //IMPORTANT
}
return self;
}

Found it!
I was changing the scale of the _backgroundLayer SKnode so the coordinates were different.

Related

contactTestBitMask fails to detect contact?

I am detecting contacts from a ball and two edges of the screen (left and right one)
For that purpose I created:
– SKSpriteNode *ball
– SKNode *leftEdge
– SKNode *rightEdge
Here I am setting up the bit masks
static const uint32_t kCCBallCategory = 0x1 << 0;
static const uint32_t kCCEdgeCategory = 0x1 << 1;
Here I am adding the edges
leftEdge.physicsBody = [SKPhysicsBody bodyWithEdgeFromPoint:CGPointZero toPoint:CGPointMake(0.0, self.size.height)];
leftEdge.position = CGPointZero;
leftEdge.physicsBody.categoryBitMask = kCCEdgeCategory;
[self addChild:leftEdge];
Right edge is added exactly the same, except it has different name and position is set to
rightEdge.position = CGPointMake(self.size.width, 0.0);
Here I am configuring and adding the ball
ball.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:6.0];
ball.physicsBody.categoryBitMask = kCCBallCategory;
ball.physicsBody.collisionBitMask = kCCEdgeCategory;
ball.physicsBody.contactTestBitMask = kCCEdgeCategory;
[_mainLayer addChild:ball];
Later in didBeginContact I am checking if first body is the ball and the second body is the edge and adding some explosion to it
SKPhysicsBody *firstBody;
SKPhysicsBody *secondBody;
if (firstBody.categoryBitMask == kCCBallCategory && secondBody.categoryBitMask == kCCEdgeCategory) {
// Collision between ball and the edge
[self addExplosion:contact.contactPoint withName:#"BallEdgeBounce"];
}
And the strange thing is – when the ball hits the left edge - code works fine and explosion effect added to the scene. But the right edge wont do the same. It came by surprise for me, because collisions works just fine. So why does the right edge behave like that? How do I fix this?
You could look at the project on the github, settings is done in GameScene.m file https://github.com/Fenkins/Space-Cannon
I've looked into your Git project and this is happening because you have wrongly set category for rightEdge ( to be more precise, you haven't set it at all). You should do something like this:
SKNode *rightEdge = [[SKNode alloc]init];
rightEdge.physicsBody = [SKPhysicsBody bodyWithEdgeFromPoint:CGPointZero toPoint:CGPointMake(0.0, self.size.height)];
rightEdge.position = CGPointMake(self.size.width, 0.0);
// leftEdge.physicsBody.categoryBitMask = kCCEdgeCategory;//Error when copy/pasting ;-)
rightEdge.physicsBody.categoryBitMask = kCCEdgeCategory;
[self addChild:rightEdge];
You might have missed a simple practice carried out in the didBeginContact: method, of assigning the first and second bodies based on their categoryBitMask values.
-(void)didBeginContact:(SKPhysicsContact *)contact {
SKPhysicsBody *firstBody;
SKPhysicsBody *secondBody;
if (contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask) {
firstBody = contact.bodyA;
secondBody = contact.bodyB;
} else {
firstBody = contact.bodyB;
secondBody = contact.bodyA;
}
...
}
This needs to be done because there is no guarantee which body will be the first or the second.
This is a different direction than where you seem to be going with your app, but it works.
SKSpriteNode *edgeLeft = [SKSpriteNode spriteNodeWithColor:[SKColor clearColor] size:CGSizeMake(1, self.frame.size.height)];
edgeLeft.physicsBody.collisionBitMask = kCCEdgeCategory;
edgeLeft.position = CGPointMake(3, self.frame.size.height / 2);
[self addChild:edgeLeft];
Then do the same for the top edge.
Hope this helped.

Sprite Kit turn node by Y

I have 2 nodes: SKShapeNode coin and SKSpriteNode hero with delegate and didBeginContact method. Contact works fine, but after i reverse my hero texture, nodes are don't interact.
The logic is: hero is go on line, under line and above line positioned coins. When hero is go above line all good, but when i use changeHeroSide method, hero reversed and go under line, didBeginContact method don't response.
- (void)changeHeroSide
{
self.yScale = -fabs(self.yScale); // this part don't interact under line
/** BUT THIS PART WORKS WELL AND INTERACTS UNDER LINE
self.zRotation = M_PI;
self.xScale = fabs(self.xScale);
**/
self.position = CGPointMake(self.position.x, self.position.y - self.frame.size.height);
self.physicsBody.affectedByGravity = NO;
}
Creating of nodes
- (void)create
{
self.hero = [[VZHero alloc] initAtPosition:CGPointZero withPlayer:nil];
self.hero.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:self.hero.frame.size];
self.hero.xScale = -fabs(self.hero.xScale);
SKShapeNode *platform = [self getCurrentPlatform];
self.hero.position = CGPointMake([self getHeroEdgeXCoordinateForPlatform:platform], platform.frame.size.height + _hero.frame.size.height/2);
self.hero.physicsBody.dynamic = YES;
self.hero.physicsBody.categoryBitMask = monsterCategory;
self.hero.physicsBody.contactTestBitMask = projectileCategory;
self.hero.physicsBody.usesPreciseCollisionDetection = YES;
[self addChild:self.hero atWorldLayer:VZWorldLayerCharacter];
SKShapeNode *coin = [SKShapeNode shapeNodeWithCircleOfRadius:radius];
coin.name = #"coin";
coin.strokeColor = [SKColor blackColor];
coin.fillColor = [SKColor yellowColor];
coin.lineWidth = 1;
coin.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:radius];
coin.physicsBody.dynamic = NO;
coin.position = pos;
coin.physicsBody.categoryBitMask = projectileCategory;
coin.physicsBody.contactTestBitMask = monsterCategory;
// coin.physicsBody.collisionBitMask = 0;
coin.physicsBody.usesPreciseCollisionDetection = YES;
[self addChild:coin atWorldLayer:VZWorldLayerCharacter];
}
Mirroring the y scale that way is messing up the physics body properties. Looks like a bug in SpriteKit (at least on iOS 7). You can solve this by wrapping your sprite in a container node. This related question might be of some help.

Reposition sprite nodes after SKAction is completed

I am trying to reposition my sprites after an SKAction moveTo action is completed. I have programmed the enemies to enter the screen from (self.frame.size.width/2, 0). They are moving only on the y axis. I want to reposition them on the initial position when their y position is bigger than (self.frame.size.height) and move them again and again in the same way until the player kills all of the enemies. I am struggling on this point.What part of code should I add? Any ideas? This code might help you understand my implementation :
-(void) addEnemies {
for (int j = 0; j < 6; j++) {
SKSpriteNode* enemy = [SKSpriteNode spriteNodeWithImageNamed:#"player"];
enemy.position = CGPointMake(((self.frame.size.width) -20) - j * (enemy.frame.size.width) , 0);
CGPoint realDest = CGPointMake((enemy.position.x), (self.frame.size.height));
float velocity = 50/1.0;
float realMoveDuration = self.size.height / velocity;
SKAction * actionMove = [SKAction moveTo:realDest duration:realMoveDuration];
[enemy runAction:actionMove];
[self addChild:enemy];
}
}
-(void)addEnemies{
for (int j = 0; j < 6; j++) {
SKSpriteNode* enemy = [SKSpriteNode spriteNodeWithImageNamed:#"player"];
enemy.position = CGPointMake(((self.frame.size.width) -20) - j * (enemy.frame.size.width) , 0);
[self addChild:enemy];
[self moveEnemyNode:enemy];
}
}
-(void)moveEnemyNode:(SKSpriteNode *)enemy{
enemy.position = CGPointMake(enemy.position.x, 0);
float velocity = 50/1.0;
float realMoveDuration = self.size.height / velocity;
SKAction *actionMove = [SKAction moveToY:self.frame.size.height + enemy.frame.size.height duration:realMoveDuration];
[self runAction:actionMove completion:^{
[self moveEnemyNode:enemy];
}];
}
Create a SKSpriteNode that replicates a roof -> Rectangle(self.frame.size.width,1)
Have it sit along the top edge of the screen with a non-dynamic physics body
Set the "roof" and enemy to contact each other
Call a method on contact that resets there position and calls the SKaction
This allows you to have direct access to each enemy exactly when it hits the top of the screen.
Here is an example of how to create a roof and have enemies interact with it. https://stackoverflow.com/a/24195006/2494064

CATiledLayers on OS X

This has been driving me crazy.. I have a large image, and need to have a view that is both zoomable, and scrollable (ideally it should also be able to rotate, but I've given up on that part). Since the image is very large, I plan on using CATiledLayer, but I simply can't get it to work.
My requirements are:
I need to be able to zoom (on mouse center) and pan
The image should not change its width:height ratio (shouldn't resize, only zoom).
This should run on Mac OS 10.9 (NOT iOS!)
Memory use shouldn't be huge (although up to like 100 MB should be ok).
I have the necessary image both complete in one file, and also tiled into many (even have it for different zoom levels). I prefer using the tiles, as that should be easier on memory, but both options are available.
Most of the examples online refer to iOS, and thus use UIScrollView for the zoom/pan, but I can't get to copy that behaviour for NSScrollView. The only example for Mac OS X I found is this, but his zoom always goes to the lower left corner, not the middle, and when I adapt the code to use png files instead of pdf, the memory use gets around 400 MB...
This is my best try so far:
#implementation MyView{
CATiledLayer *tiledLayer;
}
-(void)awakeFromNib{
NSLog(#"Es geht los");
tiledLayer = [CATiledLayer layer];
// set up this view & its layer
self.wantsLayer = YES;
self.layer = [CALayer layer];
self.layer.masksToBounds = YES;
self.layer.backgroundColor = CGColorGetConstantColor(kCGColorWhite);
// set up the tiled layer
tiledLayer.delegate = self;
tiledLayer.levelsOfDetail = 4;
tiledLayer.levelsOfDetailBias = 5;
tiledLayer.anchorPoint = CGPointZero;
tiledLayer.bounds = CGRectMake(0.0f, 0.0f, 41*256, 22*256);
tiledLayer.autoresizingMask = kCALayerNotSizable;
tiledLayer.tileSize = CGSizeMake(256, 256);
self.frame = CGRectMake(0.0f, 0.0f, 41*256, 22*256);
self.layer = tiledLayer;
//[self.layer addSublayer:tiledLayer];
[tiledLayer setNeedsDisplay];
}
-(void)drawRect:(NSRect)dirtyRect{
CGContextRef context = [[NSGraphicsContext currentContext] graphicsPort];
CGFloat scale = CGContextGetCTM(context).a;
CGSize tileSize = tiledLayer.tileSize;
tileSize.width /= scale;
tileSize.height /= scale;
// calculate the rows and columns of tiles that intersect the rect we have been asked to draw
int firstCol = floorf(CGRectGetMinX(dirtyRect) / tileSize.width);
int lastCol = floorf((CGRectGetMaxX(dirtyRect)-1) / tileSize.width);
int firstRow = floorf(CGRectGetMinY(dirtyRect) / tileSize.height);
int lastRow = floorf((CGRectGetMaxY(dirtyRect)-1) / tileSize.height);
for (int row = firstRow; row <= lastRow; row++) {
for (int col = firstCol; col <= lastCol; col++) {
NSImage *tile = [self tileForScale:scale row:row col:col];
CGRect tileRect = CGRectMake(tileSize.width * col, tileSize.height * row,
tileSize.width, tileSize.height);
// if the tile would stick outside of our bounds, we need to truncate it so as
// to avoid stretching out the partial tiles at the right and bottom edges
tileRect = CGRectIntersection(self.bounds, tileRect);
[tile drawInRect:tileRect];
}
}
}
-(BOOL)isFlipped{
return YES;
}
But this deforms the image, and doesn't zoom or pan correctly (but at least the tile selection works)...
I can't believe this is so hard, any help would be greatly appreciated. Thanks :)
After a lot of research and tries, I finally managed to get this to work using this example. Decided to post it for future reference. Open the ZIP > CoreAnimationLayers> TiledLayers, there's a good example there. That's how CATiledLayer works with OS X, and since the example there doesn't handle zoom very well, I leave here my zoom code
-(void)magnifyWithEvent:(NSEvent *)event{
[super magnifyWithEvent:event];
if (!isZooming) {
isZooming = YES;
BOOL zoomOut = (event.magnification > 0) ? NO : YES;
if (zoomOut) {
[self zoomOutFromPoint:event.locationInWindow];
} else {
[self zoomInFromPoint:event.locationInWindow];;
}
}
}
-(void)zoomInFromPoint:(CGPoint)mouseLocationInWindow{
if(zoomLevel < pow(2, tiledLayer.levelsOfDetailBias)) {
zoomLevel *= 2.0f;
tiledLayer.transform = CATransform3DMakeScale(zoomLevel, zoomLevel, 1.0f);
tiledLayer.position = CGPointMake((tiledLayer.position.x*2) - mouseLocationInWindow.x, (tiledLayer.position.y*2) - mouseLocationInWindow.y);
}
}
-(void)zoomOutFromPoint:(CGPoint)mouseLocationInWindow{
NSInteger power = tiledLayer.levelsOfDetail - tiledLayer.levelsOfDetailBias;
if(zoomLevel > pow(2, -power)) {
zoomLevel *= 0.5f;
tiledLayer.transform = CATransform3DMakeScale(zoomLevel, zoomLevel, 1.0f);
tiledLayer.position = CGPointMake((tiledLayer.position.x + mouseLocationInWindow.x)/2, (tiledLayer.position.y + mouseLocationInWindow.y)/2);
}
}

How to slice/cut sprites in iOS (Core Graphics)

I am working on a game and I would like to add a proper slicing feature in it.. so when a sprite sliced, 2 new sprites should be created.. please check here
At the moment, I am just reducing the size and duplicating the sprites.. Something like this.. Thanks in advance..
- (BOOL) sliceSprite: (Sprite *) sprite withPath: (UIBezierPath *) slicePath
{
CGSize size = sprite.size;
size.width /= 2;
size.height /=2;
sprite.size = size;
sprite.sliced = YES;
Sprite *newSprite = [[Sprite alloc] initWithImage: sprite.image];
newSprite.position = sprite.position;
newSprite.size = size;
newSprite.sliced = YES;
newSprite.inView = YES;
newSprite.xVelocity = SLICE_SPEEDUP * sprite.yVelocity;
newSprite.yVelocity = SLICE_SPEEDUP * sprite.xVelocity;
newSprite.angularVelocity = -SLICE_REVUP * sprite.angularVelocity;
[sprites addObject: newSprite];
[newSprite release];
sprite.angularVelocity = SLICE_REVUP * sprite.angularVelocity;
sprite.xVelocity = -SLICE_SPEEDUP * sprite.xVelocity;
sprite.yVelocity = -SLICE_SPEEDUP * sprite.yVelocity;
return YES;
}
- (void) sliceSpritesInSwipePath
{
CGRect swipeRect = [swipePath bounds];
for (NSUInteger i = 0; i < [sprites count]; i++)
{
Sprite *sprite = [sprites objectAtIndex: i];
if ([sprite intersectsWithPathInArray: swipePoints
inRect: swipeRect])
if ([self sliceSprite: sprite withPath: swipePath])
{
[self resetSwipe];
if (![sliceSound isPlaying])
[sliceSound play];
break;
}
}
}
Is the specific line of splitting required? Fruit Ninja just spawns two halves of the fruit, as if it was split down the middle, this would be quite easy to do:
Create two sprites which are half the width of the original sprite
Position them 1/4 and 3/4 of the way along the original sprite's horizontal centre line
Add rotation/acceleration etc.
Modify texture coordinates so that the left sprite has the left half of the texture and the right sprite has the right half of the texture.
Since you're using CoreGraphics here, why not simply use a clipping path when drawing the sprite(s)?
Duplicate the sprite to be sliced, then apply simple polygons masking the two halves as their respective clipping paths. The function you need is called CGContextClip and a short tutorial can be found here.
Edit: The tutorial lists this example:
CGContextBeginPath (context);
CGContextAddArc (context, w/2, h/2, ((w>h) ? h : w)/2, 0, 2*PI, 0);
CGContextClosePath (context);
CGContextClip (context);
This sets the current path to a circle, then applies the current path as the clipping region.