I use the following piece of code to generate SKNodes periodically. Is there a way to make the period of generation of these SKNodes random. Specifically, how do I make the "delayFish" in the following code an action with a random delay?
[self removeActionForKey:#"fishSpawn"];
SKAction* spawnFish = [SKAction performSelector:#selector(spawnLittleFishes) onTarget:self];
SKAction* delayFish = [SKAction waitForDuration:3.0/_moving.speed];
SKAction* spawnThenDelayFish = [SKAction sequence:#[spawnFish, delayFish]];
SKAction* spawnThenDelayFishForever = [SKAction repeatActionForever:spawnThenDelayFish];
[self runAction:spawnThenDelayFishForever withKey:#"fishSpawn"];
ObjC:
First set an average delay and range...
#define kAverageDelay 2.0
#define kDelayRange 1.0 // vary by plus or minus 0.5 seconds
and then change your delayFish action to this...
SKAction* delayFish = [SKAction waitForDuration:kAverageDelay withRange:kDelayRange];
Swift:
First set an average delay and range...
let averageDelay:TimeInterval = 2.0
let delayRange:TimeInterval = 1.0 // vary by plus or minus 0.5 seconds
and then change your delayFish action to this...
let delayFish = SKAction.wait(forDuration:averageDelay, withRange:delayRange)
Insert a random float instead of a fixed one.
In your case something like this:
double value = ((double)arc4random() / ARC4RANDOM_MAX)
* (maxValue - minValue)
+ minValue;
SKAction* delayFish = [SKAction waitForDuration:value/_moving.speed];
I see. This won't work in your case as repeatActionForever will run with the last created random value. Forever.
Maybe try this instead. I'm not sure if this works though:
SKAction* delayFish = [SKAction waitForDuration: (((double)arc4random() / ARC4RANDOM_MAX) * (maxValue - minValue)+ minValue)/_moving.speed];
I suggest making the random value an own method though.
-(double) getRandomValue(){
return (((double)arc4random() / ARC4RANDOM_MAX) * (maxValue - minValue)+ minValue);
}
EDIT:
Here is a link to a similar issue. Maybe that might help. Sorry!
SKAction *randomXMovement = [SKAction runBlock:^(void){
NSInteger xMovement = arc4random() % 20;
NSInteger leftOrRight = arc4random() % 2;
if (leftOrRight == 1) {
xMovement *= -1;
}
SKAction *moveX = [SKAction moveByX:xMovement y:0 duration:1.0];
[aSprite runAction:moveX];
}];
SKAction *wait = [SKAction waitForDuration:1.0];
SKAction *sequence = [SKAction sequence:#[randomXMovement, wait]];
SKAction *repeat = [SKAction repeatActionForever:sequence];
[aSprite runAction: repeat];
Source: SKAction: How to Animate Random Repeated Actions
Related
I am trying to create an array of SKActions in Spritekit using objective-c so that two arrays execute in parallel. "rotation" is not an array which is fine with me, unless it is causing the issue which I doubt is the problem here. What I am expecting from this code is that actionMove and rotation will run in parallel and then it moves on to actionMove1 and actionMove2 and finishes the runAction. I am getting the following error in the last line of code shown below (added just the portion of the code needed).
Collection element of type 'SKAction *__strong [3]' is not an Objective-C object
SKAction * actionMove = [SKAction moveTo:CGPointMake(actualX2, actualY2) duration:actualDuration];
SKAction * actionMove1 = [SKAction moveTo:CGPointMake(actualX3, actualY3) duration:actualDuration];
SKAction * actionMove2 = [SKAction moveTo:CGPointMake(actualX4, actualY4) duration:actualDuration];
int rotate = arc4random() % 5;
SKAction * rotation = [SKAction rotateByAngle:M_PI/rotate duration:0.5];
SKAction * moveArray[] = {actionMove, actionMove1, actionMove2};
[game_piece1 runAction:[SKAction group:#[moveArray, rotation]]];
I believe what you want is a combination of group: and sequence: actions. Groups will run together and sequence will wait until the previous action is finished.
SKAction * actionMove = [SKAction moveTo:CGPointMake(actualX2, actualY2) duration:actualDuration];
SKAction * actionMove1 = [SKAction moveTo:CGPointMake(actualX3, actualY3) duration:actualDuration];
SKAction * actionMove2 = [SKAction moveTo:CGPointMake(actualX4, actualY4) duration:actualDuration];
int rotate = arc4random() % 5;
SKAction * rotation = [SKAction rotateByAngle:M_PI/rotate duration:0.5];
SKAction *firstStep = [SKAction group:#[actionMove, rotation]];
SKAction *sequence = [SKAction sequence:#[firstStep, actionMove1, actionMove2]];
[game_piece1 runAction:sequence];
You might find this link helpful Adding Actions to Nodes It does a good job of show different groups and sequences.
Hopefully that will give you the desired result.
Edit
If you are looking to run rotation while running move actions in oder it would look like this.
SKAction * actionMove = [SKAction moveTo:CGPointMake(actualX2, actualY2) duration:actualDuration];
SKAction * actionMove1 = [SKAction moveTo:CGPointMake(actualX3, actualY3) duration:actualDuration];
SKAction * actionMove2 = [SKAction moveTo:CGPointMake(actualX4, actualY4) duration:actualDuration];
int rotate = arc4random() % 5;
SKAction * rotation = [SKAction rotateByAngle:M_PI/rotate duration:0.5];
SKAction *sequence = [SKAction sequence:#[actionMove, actionMove1, actionMove2]];
SKAction *group = [SKAction group:#[sequence, rotation]];
[game_piece1 runAction:group];
I have a row of 5 sprites and they spawn every 4 seconds and move down along the Y-axis. The sprites are spawning the correct amount each time, I have 5 sprites equal spaced apart across the screen, they're all center perfectly.
However when the performSelector action is called, when it spawns them in they don't go to the right position. The move over to the left so far that I can only see half a sprite on the left side of the screen. After testing it, it seems they are all stacked on top of each other at the wrong position.
Any idea whats going on? I'd appreciate any help. Here's all the code I'm using in the scene.
-(void) addBricks:(CGSize)size {
for (int i = 0; i < 5; i++) {
//create brick sprite from image
SKSpriteNode *brick = [SKSpriteNode spriteNodeWithImageNamed:#"brick"];
//resize bricks
brick.size = CGSizeMake(60, 30);
//psoition bricks
int xPos = size.width/7.5 * (i+.5);
int yPos = 450;
brick.position = CGPointMake(xPos, yPos);
//add move action
SKAction *wait = [SKAction waitForDuration:3];
SKAction *move = [SKAction moveByX:0 y:-36.9 duration:1];
SKAction *sequence = [SKAction sequence:#[wait, move]];
SKAction *repeatMove = [SKAction repeatActionForever:sequence];
[brick runAction:repeatMove];
[self addChild:brick];
}
}
-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
self.backgroundColor = [SKColor colorWithRed:(243.0f/255) green:(228.0f/255) blue:(165.0f/255) alpha:1.0];
//add action to spawn bricks
SKAction *spawn = [SKAction performSelector:#selector(addBricks:) onTarget:(self)];
SKAction *delay = [SKAction waitForDuration:4];
SKAction *delayThenSpawn = [SKAction sequence:#[delay, spawn]];
[self runAction:[SKAction repeatActionForever:delayThenSpawn]];
[self addBricks:size];
}
return self;
}
as LearnCocos2D mentioned in your last question.. you cant use a selector when youre passing a parameter into a method.
addBricks expects size .. you arent passing size into it.
change your spawn action to
SKAction *spawn = [SKAction runBlock:^{
// make this whatever size you want, i'm just using the scene's size
[self addBricks:self.size];
}];
that should get you started
I am programming a game and want to move a sprite along a fixed path (straights and curves - think railroad engine) but I want to be able to drive the animation in terms of the sprites velocity - and change that velocity in response to game events.
followPath:duration: in SKAction is close, but there seems no way to adjust the speed "on the fly" - I definitely care about sprite speed - not 'duration to follow path'.
CGPath seems like the right construct for defining the path for the sprite, but there doesn't seem to be enough functionality there to grab points along the path and do my own math.
Can anyone suggest a suitable approach?
You can adjust the execution speed of any SKAction with its speed property. For example, if you set action.speed = 2, the action will run twice as fast.
SKAction *moveAlongPath = [SKAction followPath:path asOffset:NO orientToPath:YES duration:60];
[_character runAction:moveAlongPath withKey:#"moveAlongPath"];
You can then adjust the character's speed by
[self changeActionSpeedTo:2 onNode:_character];
Method to change the speed of an SKAction...
- (void) changeActionSpeedTo:(CGFloat)speed onNode:(SKSpriteNode *)node
{
SKAction *action = [node actionForKey:#"moveAlongPath"];
if (action) {
action.speed = speed;
}
}
Try some think like this :
CGMutablePathRef cgpath = CGPathCreateMutable();
//random values
float xStart = [self getRandomNumberBetween:0+enemy.size.width to:screenRect.size.width-enemy.size.width ];
float xEnd = [self getRandomNumberBetween:0+enemy.size.width to:screenRect.size.width-enemy.size.width ];
//ControlPoint1
float cp1X = [self getRandomNumberBetween:0+enemy.size.width to:screenRect.size.width-enemy.size.width ];
float cp1Y = [self getRandomNumberBetween:0+enemy.size.width to:screenRect.size.width-enemy.size.height ];
//ControlPoint2
float cp2X = [self getRandomNumberBetween:0+enemy.size.width to:screenRect.size.width-enemy.size.width ];
float cp2Y = [self getRandomNumberBetween:0 to:cp1Y];
CGPoint s = CGPointMake(xStart, 1024.0);
CGPoint e = CGPointMake(xEnd, -100.0);
CGPoint cp1 = CGPointMake(cp1X, cp1Y);
CGPoint cp2 = CGPointMake(cp2X, cp2Y);
CGPathMoveToPoint(cgpath,NULL, s.x, s.y);
CGPathAddCurveToPoint(cgpath, NULL, cp1.x, cp1.y, cp2.x, cp2.y, e.x, e.y);
SKAction *planeDestroy = [SKAction followPath:cgpath asOffset:NO orientToPath:YES duration:5];
[self addChild:enemy];
SKAction *remove2 = [SKAction removeFromParent];
[enemy runAction:[SKAction sequence:#[planeDestroy,remove2]]];
CGPathRelease(cgpath);
Just trying to enhance a part of this code, and I was wondering if I could include an if statement in a sequence? I want to slow down the spawning of debris when the debris count is greater than 10.
This is the current code:
-(void)spawnDebris {
//debris
SKSpriteNode * debris = [SKSpriteNode spriteNodeWithTexture:[SKTexture textureWithImageNamed:#"debris1.png"] size:CGSizeMake(40, 40)];
debris.zPosition = 1.0;
debris.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:15];
debris.physicsBody.allowsRotation = NO;
debris.physicsBody.categoryBitMask = CollisionDebris;
RandomPosition = arc4random() %248;
RandomPosition = RandomPosition + 34;
debris.position = CGPointMake (RandomPosition, self.size.height + 40);
[_debris addObject:debris];
[self addChild:debris];
//next Spawn:
[self runAction:[SKAction sequence:#[
[SKAction waitForDuration:2],
[SKAction performSelector:#selector(spawnDebris) onTarget:self],
]]];
if (_dead == YES) {
[self removeAllActions];
}
}
Where _debris is an NSMutableArray. So essentially the SKSpriteNodes are being added every 2 seconds, but to change it up, I wanted to have their waitForDurations to change after the _debris count is over a certain number.
This is what I thought to do:
//next Spawn:
[self runAction:[SKAction sequence:#[
[SKAction waitForDuration:2],
[SKAction runBlock:^{
if (_debris.count > 10) {
[SKAction waitForDuration:7];
}
}],
[SKAction performSelector:#selector(spawnDebris) onTarget:self],
]]];
But this didn't work. Is it even possible to use an if statement in a sequence? How can I get this to work?
The code won't work because the [SKAction waitForDuration:7] is not executed.
Much easier would be:
float time = 2.0;
if (_debris.count > 10) { time = 9.0; }
[self runAction:[SKAction sequence:#[
[SKAction waitForDuration:time],
[SKAction performSelector:#selector(spawnDebris) onTarget:self],
]]];
The block declares a waitForDuration action but never runs it on anything.
Try
[self runAction:[SKAction sequence:#[
[SKAction waitForDuration:2],
[SKAction runBlock:^{
if (_debris.count > 10) {
[self runAction:[SKAction waitForDuration:7]];
}
}],
[SKAction performSelector:#selector(spawnDebris) onTarget:self],
]]];
Why don't you just use a random number generator to make it more interesting.
Instead of the if statement and all that, simply replace 2 with arc4random()% x (< choose a number) then it will spawn randomly every x seconds.
I have two different SKSpriteNodes. The first is stationary and the second, I would like, to move around the first node. I have my second node is flying in from off screen with a zPosition of 11 (the first node has a zPosition of 10). When the second node flies to the opposite end of the screen, I have the node flip and fly back across. At this time I would like to change the zPosition to 9.
witch.position = CGPointMake(self.size.width + 200, self.size.height / 2 + 50);
witch.zPosition = 11;
[self addChild:witch];
SKAction *moveLeft = [SKAction moveToX:-200 duration:8];
SKAction *moveRight = [SKAction moveToX:self.size.width + 200 duration:8];
SKAction *moveDown = [SKAction moveToY:secondNode.position.y - 50 duration:0.3];
SKAction *moveUp = [SKAction moveToY:secondNode.position.y + 50 duration:0.3];
SKAction *flipRight = [SKAction scaleXTo:-1 duration:0];
SKAction *flipLeft = [SKAction scaleXTo:1 duration:0];
[secondNode runAction:
[SKAction repeatActionForever:
[SKAction sequence:
#[moveLeft, moveDown, flipRight, CHANGEZPOSITIONHERE, moveRight, moveUp, flipLeft, CHANGEZPOSITIONHERE]]]];
How would I go about changing the zPosition in the middle of an SKAction?
Thank you in advance for all suggestions and help.
This answer was found due to LearnCocos2D comment.
Adding a runBlock:action I was able to change the zPosition. The final code is:
[secondNode runAction:
[SKAction repeatActionForever:
[SKAction sequence:
#[moveLeft, moveDown, flipRight,
[SKAction runBlock:(dispatch_block_t)^(){
secondNode.zPosition = 9;
}], moveRight, moveUp, flipLeft,
[SKAction runBlock:(dispatch_block_t)^(){
secondNode.zPosition = 11;
}]]]]];