Only run skaction if collision is in view - ios

I'm working on a game where if there is a collision between two of the same bodies then a sound plays. I've got this part working fine, but I only want the sound to play if the collision happens in the view. I have a scrolling background and collisions further ahead are making a noise.
Is there anyway to limit this sound from only playing when a collision happens in view?
Right now i'm using this code:
let rocksCollide = SKAction.playSoundFileNamed("rocks.wav", waitForCompletion: false)
if nodeB.name == "SMALLASTEROID" && nodeA.name == "SMALLASTEROID"{
runAction(rocksCollide)
}
Any help would be appreciated.

You can check if the node.position is inside the visible frame. For example.
if (CGRectContainsPoint(visibleFrame, nodeA.position)) {
// Play sound.
}

Related

Tile-based movement with Lance game engine?

I'm using Lance for a game where the gameplay area is a tiled map. When a player presses the left-arrow key, their character should move one tile to the left, etc. I tried two approaches, see below, but got neither to work.
Could either approach be modified to work with tile-based movement? Or is a third approach needed? Or is Lance not suited to this kind of game?
Approach 1: Adjust the player's position directly when a key is pressed. From my GameEngine class:
if (inputData.input == 'left') {
player.position.x -= 32;
player.angle = 180;
}
While this works well for a single player, it doesn't in multiplayer. When player A moves, their position is not updated on player B's screen.
Approach 2: Set the player's state when a key is pressed:
if (inputData.input == 'left') {
player.state = 'walkLeft';
}
Then add a postStep handler in the common GameEngine class. (Adding it to Player didn't work). This code turns the player (over many steps) to face 180 degrees and then accelerates the player in that direction:
onPostStep(event) {
let players = this.world.queryObjects({instanceType: Player});
players.forEach(player => {
if (player.state == 'walkLeft') {
if (Math.abs(player.angle - 180) > 2)
player.turnLeft(2);
}
else {
player.accelerate(1);
player.state = '';
}
}
})
}
With this approach, if a player presses the left arrow key, their angle changes as expected at first, but the acceleration and movement is erratic. Also, Player A's position appears different on their screen vs the screen of Player B.
The Spaaace demo is the base for my project, so my project uses the same bending, physics engine, etc.
The first approach is better. The Brawler game in the sample collection does exactly what you describe. You can look at the BrawlerGameEngine.js code in https://github.com/lance-gg/tinygames/tree/master/brawler
Make sure that the action is processed in the method
GameEngine::processInput(inputData, playerId)

Adding SKPhysicsBody causes the SKSpriteNode to shake

I have two SkSpriteNodes called left-ball and right-ball.THey flow from sides of the screen.
I implemented the collision detection for these two balls. However I noticed that before I add collision detection, the balls were moving smoothly without any shaking effect, but after I add the collision detection, the balls were shaking and moving in their path.
I am using the same bit category for both of these balls:
static const int ballHitCategory = 1;
without shaking:
with shaking:
Here is what I tried in code:
_leftBall.physicsBody=[SKPhysicsBody bodyWithRectangleOfSize:_leftBall.frame.size];
_leftBall.physicsBody.categoryBitMask=ballHitCategory;
_leftBall.physicsBody.contactTestBitMask=ballHitCategory;
_leftBall.physicsBody.collisionBitMask=ballHitCategory;
_leftBall.physicsBody.dynamic=YES;
_leftBall.physicsBody.usesPreciseCollisionDetection=YES;
_rightBall.physicsBody=[SKPhysicsBody bodyWithRectangleOfSize:_rightBall.frame.size];
_rightBall.physicsBody.categoryBitMask=ballHitCategory;
_rightBall.physicsBody.contactTestBitMask=ballHitCategory;
_rightBall.physicsBody.collisionBitMask=ballHitCategory;
_rightBall.physicsBody.dynamic=YES;
_rightBall.physicsBody.usesPreciseCollisionDetection=YES;
-(void)didBeginContact:(SKPhysicsContact *)contact
{
_leftBall = (SKSpriteNode*)contact.bodyA.node;
_rightBall = (SKSpriteNode *)contact.bodyB.node;
if(_leftBall.physicsBody .categoryBitMask == ballHitCategory || _rightBall.physicsBody.categoryBitMask == ballHitCategory)
{
NSLog(#" hit the ");
//setup your methods and other things here
[_leftBall removeFromParent];
[_rightBall removeFromParent];
}
}
Please note that shake happens only after I add the collision detection, If I remove the above code, everything is fine.
What is the proper way to add collision detection without effecting the skspritenode ?
so youre moving the sprites using non physics code.. but theyre still part of the physics simulation. I'm guessing theyre fighting against the force of gravity.
either set affectedByGravity on each sprite to false
or you can turn off gravity in your game completely
[self.physicsWorld setGravity:CGVectorMake(0, 0)];
Don't worry about the Gravity. You should set your spritenode:
_leftBall.physicsBody.dynamic=YES;
_leftBall.physicsBody.affectedByGravity=NO;
Same on _rightBall

Pausing SKSpriteNode (Swift, SpriteKite)

I need to pause the balls for my game. I want them to stop in place but they are moving by impulse and if I make them non-dynamic to stop them, the impulse goes away. I'm trying to pause them and un-pause them, and they still keep going in the same direction. I tried
ball.paused = true
but that didn't work. Anyone know?
Instead of freezing the nodes, I froze the scene with
scene?.physicsWorld.speed = 0
because that freezes all of the nodes and not my time or score integers which is exactly what I needed.
Here is how I solved it in my game:
var ballVelocity = CGVector()
func pauseAction(ball: SKSpriteNode){
if ball.dynamic {
//Ball is moving. Save the current velocity of ball.
ballVelocity = ball.velocity
//Stop the ball
ball.physicsBody.dynamic = false
}else{
//Ball is paused.
//Resume ball movement.
ball.physicsBody.dynamic = true
//Add the saved velocity.
ball.velocity = ballVelocity
}
}
You can use ball.physicsBody.velocity = CGVectorMake(0, 0); but keep in mind that if the ball is affected by gravity, it will still drop.

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

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, but it is always set to 1.0 by default.
Any ideas?
You can check a node's velocity by using something like this:
if((self.physicsBody.velocity.dx == 0.0) && (self.physicsBody.velocity.dy == 0.0)) {
NSLog(#"node has stopped moving");
}
The usual place to put the above code would be in the update method.
Since you are using physics you can use the resting property of SKPhysicsBody.
if sprite.physicsBody?.resting == true {
println("The sprite is resting")
} else {
println("The sprite is moving")
}
Hope this helps.
You can subclass SKSpriteNode and add a previousPosition property on it, which you can update on -didFinishUpdateMethod. If there is no change in the position you can assume the node is stopped (you might have to get a median of the last few positions to smooth it out). The speed property is something different, according to the class reference, speed is:
A speed modifier applied to all actions executed by a node and its descendants.
Hope this helps!
Danny

Set A bool to true for two seconds then change back to false

As the question states, in spritekit I am trying to find a way to change my value of a bool from false, too true for x amount of seconds then change it back to false.
the logic is i have this var, isStunned, when my player comes in contact with x sprite i call it and my joystick is disabled since the player is "stunned"
if (controlButtonDown) { // If i pressed joystick
if (!isStunned) { // IF i am not stunned
//move player
When my player touches the glue, i set isStunned to yes and the joystick is disabled, however I am wondering of a way to only disable my joystick or not move my sprite for x amount of seconds. I know of SKAction and the wait sequence, but those are actions and I dont think they will apply here. Any help is appreciated.
Thank you
Since you ask specifically with a spritekit tag.
Don't use NSTimer or actions to work with time in a game. Instead use the update function. If you're running at 60fps that means the update function will be called 120 times for two seconds. Using the frames instead of seconds to keep your game updated will avoid the gameplay being changed by lag. Imagine for instance that the players game lags a little, in other words runs slower. 2 seconds is still 2 seconds regardless of game lag, so now he is affected less by the glue than a person who has no lag. So...
First off you should have a variable keeping track of in game time: int _time;
In every update cycle you add one to time: _time++;
You should have a variable keeping track of when the user touched the glue: int _glueTouchedAtTime;
When the user clicks glue you set: _glueTouchedAtTime = time;
You should have a constant defining how long the glue is active: #define GLUE_TIME 120;
When you initiate the game set _glueTouchedAtTime to: _glueTouchedAtTime = -GLUE_TIME; To prevent the glue from being on when (_time == 0)
Testing if the user has touched glue now works like this:
if(_glueTouchedAtTime+GLUE_TIME>time) {
// Glue is active
} else {
// Glue is not active
}
Different glues for different sprites
To have different glues for different sprites I would suggest first doing a general game object (GameObject) as a subclass of either SKNode or SKSpriteNode depending on your needs. Then create a subclass of GameObject called GameObjectGluable. This should have a property called: #property int gluedAtTime;
You glue the glueable game object by: aGameObjectGluable.gluedAtTime = time;
Now you can test:
if(aGameObjectGluable.gluedAtTime +GLUE_TIME>time) {
// Game Object is glued
} else {
// Game object is not glued
}
Set your value-of-interest to true and then fire a NSTimer after two seconds to set it back to false.
For an explanation of how you might use NSTimer, see this SO thread: How do I use NSTimer?
You can use SKAction also. The runBlock method let's you execute blocks of code as actions.
SKAction *wait = [SKAction waitForDuration:2.0];
SKAction *reenableJoystick = [SKAction runBlock:^{
joystick.userInteractionEnabled = TRUE;
}];
SKAction *sequence = [SKAction sequence:#[wait, reenableJoystick]];
[self runAction:sequence];

Resources