FAQ Database Discussion Community


SpriteKit: Physics bodies breaking under too much force (applyForce)

ios,sprite-kit,skspritenode
I'm experiencing the oddest behaviour where the physicsengine has decided that walls (rectangular SKPhysicsBody) break if too many other objects (circular SKPhysicsBody) are pushing against it (using applyForce()) As far as I can tell SpriteKit has no notion of object breaking points or anything of the sort. Any ideas on...

specific positioning of SKEmitterNode from a class to main scene

ios,objective-c,sprite-kit,skspritenode,skemitternode
Im creating an spark affect for a bomb that rotates and scrolls across the screen using SKEmitterNode. I've come to a sticking point as i can't correctly position the spark effect. I can get the SKEmitterNode and spriteNode to move across the screen together with the spark affect positioned in...

Expose the Object that owns a Property

swift,sprite-kit,skspritenode,touchesbegan
Though I'm using SpriteKit, this is a general Swift question: I am creating game classes that will all use SKSpriteNodes (NPC, Player, Powerup, and so on). I want to store SKSpriteNodes as properties on these objects, rather than inheriting from SKSpriteNode (favoring composition over inheritance). This works great, except I...

How to save and print the high score into the next scene?

ios,swift,sprite-kit,skspritenode
Hey guys im looking for some help. I'm new making games and stuff so it's a little difficult for me. Im trying to save the score and print it into my GameOverScene.swift from the GameScene.swift, any advice?

The SKSpriteNode called runner is moving in the opposite way

ios,swift,skspritenode
I have an SKSpriteNode called runner and whenever I touch the screen to try to drag it from one place to another, runner goes the opposite way? For example, if I try to drag it left, it goes right, if I try to drag it up then it goes down....

Updating game score held in SKScene from a SKSpriteNode sublcass in Swift

ios,swift,sprite-kit,skspritenode,skscene
We have a main Game Scene SKScene which holds the HUD logic and score. We also have a subclass of SKSpriteNode for an object. When the object is tapped we handle the touch in the SKSpriteNode subclass. override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) { What we would like to...

How do I move a SKSpriteNode up and down around its starting value in Swift?

ios,swift,sprite-kit,skspritenode
I'm currently working on a game, where there's a SKSpriteNode. I want, that this node's location moves up and down all the time. At the beginning, it should have the y value e.g. 500 . Then it should move 200 px down (500+200=700) and then it should move 400 up...

How to retain SKSpriteNode velocity after collision with physicsBody?

ios,sprite-kit,velocity,skspritenode,skphysicsbody
In my game i don't want to decrease velocity of my SKSpriteNode after collision with any physicsBody. Currently velocity of my SKSpriteNode is decresing very much after collision with physicsBody. What did i tried : I tried to get velocity in -(void)update:(CFTimeInterval)currentTime method & i provide it to my SKSpriteNode...

Triggering the end of a while loop using the completion of SKAction

ios,objective-c,sprite-kit,skspritenode
I'm trying to use a "while loop" while my SKSpritenode is animating and I want to trigger the end of the SKAction to trigger the end of the loop. This is my attempt while (orgNode.hasActions) { //render the background } what I want to do is create a bottom up...

Changing the image of a SKSprite to an SKShapeNode

objective-c,sprite-kit,skspritenode,skshapenode
Sprite Kit, Xcode. I need to find a way to change a sprites image within the program itself. I know how to create jpg files and make them into the sprite image... But for this program, I need to draw circles/polygons (which may change inside the program) using SKShapeNode, and...

Trying to get Physics Shape That Matches the Graphical Representation

swift,skspritenode,skphysicsbody
what I'm trying to achieve is that the Node would have the same shape as PhysicsBody/texture (fire has a complicated shape), and then I'm trying to make only fireImage touchable. So far when I'm touching outside of the fireImage on the screen and it still makes a sound. It seems...

Program crashes when function called multiple times

ios,swift,sprite-kit,skspritenode
I have a function that moves a SKSpriteNode from one point to another. It works fine when I call it once. But when I call it multiple times, the whole program crashes. func play() { self.moveNode() self.moveNode() } func moveNode() { let player = SKSpriteNode(imagenamed: "player.png") player.position = CGPointMake(500.0, 500.0)...

Get CGPoint x points in front of node

sprite-kit,trigonometry,skspritenode,cgpoint
I am working on a game in sprite kit and have been trying to get a point in front of a node. I've been reading up on trigonometry but have not been able to do it. The problem: Get a CGPoint x units in front of an SKSpriteNode, relative to...

Affecting a Child From Another Scene Using Sprite Kit and Swift

ios,swift,sprite-kit,skspritenode,skscene
I'm trying to teach myself Sprite Kit and Swift. What I'm trying to do is access a child node from an ArcheryScene.sks file and affect that child from the ArcheryScene.swift file. For Example: In my ArcheryScene.swift file I have added this line of code: let scene = SKScene(fileNamed: "ArcheryScene"). This...

Random movement of sprite

swift,random,sprite-kit,skspritenode,skaction
I'm new here so i apologize if i've entered my question wrongly. That said, i'm having an issue making my sprite move into random locations around the screen. here is my code func random() ->CGFloat{ return CGFloat(Float(arc4random()) / 0xFFFFFFFF) } func random(#min: CGFloat, max:CGFloat) ->CGFloat{ return random()*(max-min)+min } dino.position =...

My game keeps crashing saying “ EXC_BAD_ACCESS(code=1, address=0xb176e978) ” when I run on my iPad

ios,swift,sprite,skspritenode
I am developing an app for iOS and I've run into a problem. Every time I run the app on my iPad with iOS 7.1 the app crashes at random times when the scene changes pointing to different SKSpriteNodes with the error EXC_BAD_ACCESS(code=1, address=0xb176e978). the way I set my sprite...

How to add Highlighted Button in SpriteKit?

ios,objective-c,sprite-kit,skspritenode
I know how to add Button in SpriteKit. I have done with SKSpriteNode. However i don't know how to add highlight button like UIButton. I mean when i pressing SKSpriteNode(Button), i want to show highlighted image like UIButton from UIKit. How can i do that? Here is my codes that...

why i got this error while moving object in spritekit

swift,skspritenode
I am trying make a game something like Tower defense. I am adding units to my map and I want to try movepoint to another point. My code is: func setTheNode(position:[Int],speed:[Int]){ let moveDown1 = SKAction.moveToY(300, duration:4) let moveRight1 = SKAction.moveToX(300, duration: 3) let sequence = SKAction.sequence([moveDown1, moveRight1]); self.runAction(sequence) } This...

Randomizing node movement duration

swift,sprite-kit,skspritenode,skaction
I am making a game in SpriteKit and I have a node that is moving back and forth across the screen and repeating using the code: let moveRight = SKAction.moveByX(frame.size.width/2.8, y: 0, duration: 1.5) let moveLeft = SKAction.moveByX(-frame.size.width/2.8, y: 0, duration: 1.5) let texRight = SKAction.setTexture(SKTexture(imageNamed: "Drake2")) let texLeft =...

Get the type of AnyObject in Swift

swift,skspritenode
I have custom SkSpriteNode subclass. The name of this class is Unit1. In GameScene: var allUnit1:[Unit1]? var enemy1 = Unit1(imageNamed: "1") enemy1.position = CGPointMake(CGFloat(StartPointX), CGFloat(self.frame.height)) // I add this custom node to array. Its not relative with my // question but I want to describe all of them. if(allUnit1 ==...

How to make a node to rotate around a point outside of the node in a spritekit game project

ios,swift,skspritenode
I came across this answer, but the answer is not clear to me. Can someone provide some sample code? Create an SKNode and set its position to the center of rotation. Add the node that should rotate around that center as child to the center node. Set the child node's...

touchesBegan on a node to delete it

swift,sprite-kit,skspritenode,touchesbegan
per exemple, I have a SKSpriteNode generated in a function named "generateNode" every seconds, and I want to call it in the touchesBegin function to detect when the user touch it, and if the user touch it, the node is deleted. I tried to call the node in this function,...

SKLightNode cast shadow issue

sprite-kit,shadow,skspritenode,sklightnode
I have been unsuccessful in getting a SKSpriteNode to cast a shadow AND also be made to disappear when going into a shadow from the same light source. I am able to do one of the two but not both. According to the docs: If the sprite is inside a...

How to make a free falling sprite node sit on another horizontaly moving sprite body.

ios,objective-c,sprite-kit,collision-detection,skspritenode
How to make a free falling sprite node sit on another horizontaly moving sprite body. . Now the both both bodies collide. But the horizontal moving body is deviated from its path and free falling body goes down after hitting horizontal body. How can fix this changing settings of the...

Move several SKSpriteNode in array with delay?

ios,objective-c,sprite-kit,skspritenode,skaction
I have a problem with moving several nodes in array. There is a code: SKSpriteNode* paddle = (SKSpriteNode*)[self childNodeWithName: paddleXCategoryName]; SKAction *move = [SKAction moveTo:CGPointMake(paddle.position.x, CGRectGetMaxY(self.scene.frame)) duration:1.5]; NSMutableArray *shots = [[NSMutableArray alloc] init]; for (int i = 1; i<=10; i++) { SKSpriteNode *shot = [[SKSpriteNode alloc] initWithImageNamed:@"shoot.png"]; shot.size = CGSizeMake(2,...

How to get a child sprite position in the view

swift,sprite-kit,skspritenode,sknode
I am attempting to get the child sprites position in the view based on the answer here and this for child in platformGroupNode.children { CGPoint p = [child.superview convertPoint:child.center toNode:self.view ] println(p) } However I am unsure how to use this with SKSpriteNode Children and SKNode Parents. I have also...

How to properly subclass (at least) 5 complex NPC's to have multiple instances of later

objective-c,ios8,sprite-kit,subclass,skspritenode
I'm trying to call instances (a random amount of: between 3-7) of an NPC class I created. I have 5 (planning to make more) of such different NPC classes and they all subclass SKSpriteNode. Rough layout of each class is unfortunately simple: a method defining the SKPhysicsBody of the NPC...

Sprites not spawning correctly

objective-c,sprite-kit,skspritenode,skaction
I'm using this code to spawn in a row of five bricks on top of one another. The first row is perfect, in position and move properly. However, the rows that spawn with the performSelector action are not being put in properly. The positions of the rows that spawn in...

SpriteKit SKAction group is running, but the SKSpriteNode image isn't being affected

sprite-kit,skspritenode,skaction
I have created an SKAction group that alters the SKSpriteNode image (alpha and rotation) whilst playing a sound effect. However, when I runAction: the sound effect is played, but the image remains fixed. In my subclassed SKSpriteNode (The Sheep) I have the following code that adds another SKSpriteNode (The Food)...

SpriteKit background added outside of scene

ios,objective-c,sprite-kit,skspritenode
I'm trying to position the background all over the scene background, but for some reason it puts it outside of screen. This is what I used: SKTexture *backgroundTexture = [self.atlas textureNamed:@"back"]; SKSpriteNode *background = [SKSpriteNode spriteNodeWithTexture:backgroundTexture size:self.view.frame.size]; background.position = (CGPoint) {CGRectGetMidX(self.view.frame), CGRectGetMidY(self.view.frame)}; [self addChild:background]; This is how it puts it:...

Randomly generating a SKSpriteNode based on possible values in Swift

ios,swift,sprite-kit,skspritenode
We have two custom subclasses of SKSpriteNode. One is called circleNode and the other triangleNode. There are three sub types to the circle option, which are created with a custom init method passing in an enum. For example: enum CircleType: UInt { case Circle1 = 1 case Circle2 = 2...

Spawn nodes at random times combing waitForDuration:withRange and runBlock: in an SKAction sequence

swift,sprite-kit,skspritenode,spawn,skaction
I am making a game with SpriteKit where I have nodes spawning at the top of the screen and falling. However, I want these nodes to spawn at a random time interval between 0.1 and 3 seconds. For example, the first node spawns in 1.3 seconds, the next in 1.8,...

Card dealing, playing

ios,objective-c,sprite-kit,skspritenode
Problem I've never made a card game before, and I'm having quite some difficulty at the moment. However, I've managed to create the deck and such. -(NSMutableArray *)arrayWithDeck:(id)sender { [sender removeAllObjects]; NSArray *faces = [[NSArray alloc] initWithObjects:@"A",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"J",@"Q",@"K", nil]; NSArray *suits = [[NSArray alloc] initWithObjects:@"h",@"d",@"c",@"s", nil]; for (int i = 0;...

Asynchronous moving of 2 nodes sprite kit

ios,swift,sprite-kit,skspritenode
I have 2 nodes, that I want to grab with 2 fingers asynchronously on my iPad. That's code for moving: var ninjaMoving = false var monsterMoving = false var loc = CGPoint(x: 0.0, y: 0.0) var prevLoc = CGPoint(x: 0.0, y: 0.0) override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {...

My character's SpriteNode is shaking when moving, why?

objective-c,sprite-kit,skspritenode,skphysicsbody,shake
My main character, which is a SKSpriteNode with physicsBody, is shaking when moving or jumping. I am moving my character by changing is character.physicsBody.velocity.dx's (in order to simulate acceleration or deceleration), and I use applyImpulse to make him jump. Code : To move : [self.physicsBody setVelocity:CGVectorMake(self.physicsBody.velocity.dx - 30, self.physicsBody.velocity.dy)]; To...

loosing fps after nodes leave screen

objective-c,sprite-kit,skspritenode,skphysicsbody
I am currently working on a simple game in Sprite Kit. In the game, you are a square that moves around and can shoot at enemies. You shoot "Projectiles" which are just a custom class that inherits from SKSpriteNode. The game works great at first but slowly as you continue...

Arrange 20 SKSpriteNode in a circle [closed]

objective-c,sprite-kit,skspritenode
I have an array with the names of 20 SKSpriteNode objects. I want to arrange those nodes in a circle like this: Just with 20 instead of 12 nodes. It should also be more accurate....

How to know whether an SKSpriteNode is not in motion or stopped?

ios,objective-c,sprite-kit,game-physics,skspritenode
In my game, I need to do some setting changes after my SKSpriteNode stops moving. First, I apply force on my SKSpriteNode, so it starts moving due to the force acting on it. Now, i need to know when it stops moving. I know that SKSpriteNode has a speed property,...

Stop SKSpriteNode from slowing down

ios,sprite-kit,skspritenode,skphysicsbody,skphysicsworld
I am trying to keep my SKSpriteNodes moving at a constant speed forever, even after collisions. I have set gravity to 0, friction to 0, and linear and angular dampening to zero, but the Sprites still slowly slow down to zero velocity. How can I keep them moving? Based on...

SKAction moveTo:duration: does not respond correctly on CGPoint value of the touch

ios,objective-c,sprite-kit,skspritenode,skaction
I am using SpriteKit for building a game , where I have a plane, which I want to move on touch. The idea is to relocate the plane to the position where the user has touched. UITouch *touch = [touches anyObject]; CGPoint point = [touch locationInView:self.view]; CGPoint moveToLocation = point;...

iOS SpriteKit run progress bar in infinite loop

ios,sprite-kit,progress-bar,skspritenode,skcropnode
TL;DR - In spritekit, In a while loop, I'm using a for loop to do arbitrary work to delay the next call of the while loop so that my power bar doesn't run ridiculously fast. Is there a better way to make a small delay in spritekit? Hey everyone, I'm...

How to change the color of an SKSpriteNode in touchesBegan?

ios,sprite-kit,skspritenode,touchesbegan
I am new to SpriteKit, and I'm trying to change the color of an SKSpriteNode inside touchesBegan. My attempts failed; can anyone suggest a reason as to why? This is my code: -(id)initWithSize:(CGSize)size { if (self = [super initWithSize:size]) { /* Setup your scene here */ self.backgroundColor = [SKColor colorWithRed:0.15...

How to prevent additional touch from interfering with drag location

ios,swift,sprite-kit,skspritenode
I'm looking for help on a problem I've been stuck on for a while but cannot find an answer to. I have a SpriteKit kids game app in which the user can drag objects (SKspriteNodes) around the screen. This is controlled in the "touches" events like so: override func touchesBegan(touches:...

SpriteKit: How do you highlight a section of the scene, like in a tutorial?

sprite-kit,skspritenode,sktexture
I'm trying to create a tutorial in a game, that goes through parts of the UI and highlights them, whilst dimming the rest of the scene. I think I can do it with Sprites and SKBlendMode, but these are poorly explained in the apple reference guide. Any idea?...

Delete child SKSpriteNode

objective-c,iphone,sprite-kit,skspritenode
Basically I am trying to make it so that when the user hits a fuel tank the old tank disappears and a new one is spawned in a random location. Right now it's detecting the collision but I can't get rid of the child. It will just add a new...

How to model a spriteNode that isnt a rectangle or a circle

swift,ios8,sprite-kit,skspritenode
Hi I have some triangular shapes in my game that need a collisionBitMask to handle contact but I cant figure out how to model the physicsBody other than circleOfRadius or RectangleOfSize. How do I make a custom physics body so that the contacts are accurate. This is an issue for...

SKSpriteNode ScaleUpFrom?

ios,objective-c,image,sprite-kit,skspritenode
Problem Scaling up a 2D image, scales up from the image centre point, however. I need it to scale up from a specific co-ordinate? [backgroundImage setScale:rifleZoom]; My current technique to scaling up the image. I need to scale up from centre screen as opposed to centre image Now my way...

Setting background color for SKLabelNode?

ios,skspritenode,sklabelnode
I want to know is there a way to set background color for a SKLabelNode not font color. I'm looking for something like below mentioned code, which is available in ios apps. label.backgroundColor = [UIColor redColor]; ...

SKSpriteNode moveTo:X for all in array

objective-c,arrays,sprite-kit,skspritenode,sknode
for (int i = 0; i < [playerCards count]; i++) { int cardsLaid = i * (cardDisplay.size.width / 3); CGPoint forPoint = CGPointMake((0.5 - (self.frame.size.width / 4)) + cardsLaid, 0.0); playerCards[i] [??? runAction:[SKAction moveTo:forPoint duration:0.6]]; } I need some sort of SKSpriteNode withName : X move to point :...

SKSpriteNode does not have member named ''- how to access a variable declared in my spawn method?

swift,sprite-kit,private-members,skspritenode
I asked another question before about how to access a global variable in Swift, but realized the global variable did not work for my purposes. Some background: The game is a math/match game. Player's will have an equation, and they have to get the sheep with the matching answer. I...

How to access single sprite in array of sprites?

objective-c,sprite-kit,skspritenode
How do I access the first sprite in an array of sprites and change it's location to this? sprite.position = CGPointMake(arc4random_uniform(self.frame.size.width), arc4random_uniform(self.frame.size.height)); @property (nonatomic, strong) SKSpriteNode *aSprite; @property (nonatomic) NSMutableArray *sprites; one for the sprite one array to hold them...

SpriteNode Perspective?

objective-c,sprite-kit,skspritenode,sknode
Problem Trying to programatically change the SKSpriteNode perspective? i.e making the bottom of the image a greater width than the top of the image, or vice versa? Cannot find any search help for this in Sprite Kit....

Issue with scrolling background vertically with sprite kit

swift,sprite-kit,skspritenode
I am trying to scroll game background vertically and it works for some time and later background become empty. this is what I have tried: var background = SKSpriteNode(imageNamed: "bgPlayScene") func addBG() { let backgroundTexture = SKTexture(imageNamed: "bgPlayScene") let shiftBackground = SKAction.moveToY(-backgroundTexture.size().height, duration: 9) let replaceBackground = SKAction.moveToY(backgroundTexture.size().height, duration: 0)...