Slow down rotation speed in cocos2d - iphone

I have my sprite rotating, but a little too fast, here is my code, any idea how to control the speed of the rotation?
//enemySprite
enemySprite = [CCSprite spriteWithFile:#"Asteroid.png"];
enemySprite.anchorPoint = ccp(0.5f, 0.5f);
enemySprite.position = ccp(arc4random()%480, winSize.height -60);
//Rotation
id rotate = [CCRotateBy actionWithDuration:1 angle:360];
id repeatRotate = [CCRepeatForever actionWithAction:rotate];
[enemySprite runAction:repeatRotate];
[self addChild:enemySprite z:5];
[self schedule:#selector(callEveryFrame:)];

You can try to make it slower by increasing its duration:
id rotate = [CCRotateBy actionWithDuration:X angle:360];
where you can use, e.g., 5.0 for X. This will make it 5 times slower as it currently is.

Related

Apply Angular Impulse

Here is my code for the player and the ball that interact with each other. What I want to do is to apply force to the ball like if my player is shooting it. I want the ball to move away from my player with force. how can I apply Impulse or force to this. I have tried many times but I am a newbie with Sprite Kit.
- (void) Player {
_Player = [SKSpriteNode spriteNodeWithImageNamed:#"player1"];
_Player.xScale = 0.09;
_Player.yScale = 0.09;
_Player.position = CGPointMake(self.size.width/4, self.size.height/2);
_Player.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:_Player.size];
_Player.physicsBody.dynamic = NO;
[self addChild:_Player];
}
- (void) TheMethodForBall {
SKSpriteNode *sprites = [SKSpriteNode spriteNodeWithImageNamed:#"ball"];
sprites.xScale = 0.19;
sprites.yScale = 0.19;
sprites.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:sprites.size];
sprites.physicsBody.dynamic = YES;
sprites.physicsBody.affectedByGravity = NO;
sprites.physicsBody.allowsRotation = YES;
sprites.physicsBody.restitution = YES;
sprites.physicsBody.angularVelocity = 4;
sprites.physicsBody.usesPreciseCollisionDetection = YES;
[self addChild:sprites];
}
I think you want to apply an impulse like a kick ?
You need the following, maybe when you touch the screen / or a button
[_myBall.physicsBody applyImpulse:CGVectorMake(somePowerX, somePowerY)];
Here is another post that will help get you started
Also, this is a good tutorial for beginners.

iphone - CGMotion move UIImageView in Landscape Mode

My app is simple: I want to move a small uiimageview around a view of an iPad with the accelerometer (now Core Motion). So, I can move the uiimageview around with the accelerometer, but I am having trouble establishing working borders in landscape mode. So, here is my code. The problem is that the uiimageview tends to stick to the borders and some of the borders are not perfectly on the edge of the view. Here is my code, any help is greatly appreciated:
- (void)startMyMotionDetect
{
__block float stepMoveFactor = 15;
[self.motionManager
startAccelerometerUpdatesToQueue:[[NSOperationQueue alloc] init]
withHandler:^(CMAccelerometerData *data, NSError *error)
{
dispatch_async(dispatch_get_main_queue(),
^{
CGRect rect = self.sumbarine.frame;
float movetoY = rect.origin.x + (data.acceleration.x * stepMoveFactor);
float maxY = self.view.frame.size.width-rect.size.width;
float movetoX = (rect.origin.y + rect.size.height)
- (data.acceleration.y * stepMoveFactor);
float maxX = self.view.frame.size.height;
if ( movetoX >0 && movetoX < maxX ) {
rect.origin.x += (data.acceleration.y * stepMoveFactor);
};
if ( movetoY > 0 && movetoY < maxY ) {
rect.origin.y += (data.acceleration.x * stepMoveFactor);
};
[UIView animateWithDuration:0 delay:0
options:UIViewAnimationOptionCurveEaseIn
animations:
^{
self.sumbarine.frame = rect;
}
completion:nil
];
}
);
}
];
}
The sticking issue comes from your conditions for moving. Right now, you say, if "moveToX/Y" is > 0, and less than the width/height, then add Acceleration X/Y.
What happens if origin is == or >greater than "maxX/Y"? Answer: Nothing. And thus, the submarine image acts like it's stuck to a board. Probably it can move on the alternate axis, until it's finally stuck at a condition where it no longer can move.
You'll want to play around with your conditional rules. I do not know what type of actions/input you're trying to implement for gameplay, so I can't tell you what to do next. I'm assuming the sub is supposed to go up/down depending on some user gesture/action.
One last piece of advice. Accelerometers are based on G-Force... What this means for you is even though acceleration on some axis is positive when you move say left... There's also an inverse curve when you need apply the force needed to stop. Try looking at the motiongraphs app (available in documentation) to help you get a better feel/sense of this stuff.
And remember, have fun!

Rotate or flip the square image in cocos2d

Assume that one square image is for showing road. Now In my game I have to flip this square image on Y axis so that it looks like road and person can walk on that road. I want this type of rotation.
But I want the same rotation in cocos2d then how I have to do it?
Thanks for help
I don't completely understand what you want to do, but I'll do my best here :)
You could use the flipX or flipY properties of a CCSprite
E.X.
CCSprite *sprite = [CCSprite spriteWithFile:#"file.png"];
sprite.position = position;
sprite.flipX = YES;
[self addChild:sprite];
or you could use a rotate action as Anish said.
Hope that helped.
Rotating Turrets in Cocos2D
Try like this...
CCRotateBy *rot = [CCRotateBy actionWithDuration: 2 angle: 720];
[sprite runAction:rot];
CCSprite *sprite = [CCSprite spriteWithFile:#"file.png"];
sprite.rotation = 45;
I Flip CCSprite at Y axis like that in android...
CCSprite sprite=CCSprite.sprite("icon.png");
CCIntervalAction a = (CCIntervalAction)CCOrbitCamera.action(2, 1, 0, 0, 360, 0, 0);
addChild(sprite,1);
sprite.runAction(CCRepeatForever.action(a));

How to make cocos2d sprite scale up and down (for a pulsating effect) every second?

I want to have a sprite scale up and down once every second to make it seem like it is bulging and pulsating. How can I do this?
As the post before contains syntax errors. To be more precise, I post working code:
CCSprite * sprite = ...; // create the sprite.
id scaleUpAction = [CCEaseInOut actionWithAction:[CCScaleTo actionWithDuration:1 scaleX:1.0 scaleY:1.0] rate:2.0];
id scaleDownAction = [CCEaseInOut actionWithAction:[CCScaleTo actionWithDuration:0.5 scaleX:0.8 scaleY:0.8] rate:2.0];
CCSequence *scaleSeq = [CCSequence actions:scaleUpAction, scaleDownAction, nil];
[sprite runAction:[CCRepeatForever actionWithAction:scaleSeq]];
You could use a simple [CCScaleTo ..] action or if you want to create your own "effect" you could advance the CCFiniteTimeAction. I would prefer the first one :
CCSprite * sprite = ...; // create the sprite.
sprite.anchorPoint = ccp( 0.5, 0.5 ); center the pivot
id myAction = [CCRepeatForEver actionWithActions:[CCScaleTo actionWithDuration:0.5 scaleX:2.0 ScaleY:2.0],[CCScaleTo actionWithDuration:0.5 scaleX:0.5 ScaleY:0.5], nil];
[sprite runAction:myAction];
use CCEase to make the animation nonlinear
id myAction = [CCRepeatForEver actionWithActions:[CCEaseInOut actionWithAction:[CCScaleTo actionWithDuration:0.5 scaleX:2.0 ScaleY:2.0] rate:2.0],[CCEaseInOut actionWithAction:[CCScaleTo actionWithDuration:0.5 scaleX:0.5 ScaleY:0.5] rate:2.0], nil];
this post may contain errors. but I hope you understand the way to come to the goal.

Problem with smoke bubble in cocos2d

I am having a problem. When the smokeMoveBy action starts a small smoke bubble is spotted on the screen at other place then the moving path of the smoke.
This is only happening when I am using scaleX and scaleY.
The method smokeLoop is being called at each 1 seconds in the scheduler.
Here self is a layer.
Any solutions?
My code follows,
CGPoint dummyPosition=ccp(600, 600);
ParticleSystem *smoke = [ParticleSmoke node];
ccColor4F startColor;
startColor.r = 1.f;
startColor.g = 1.f;
startColor.b = 1.f;
startColor.a = 1.f;
[smoke setStartColor:startColor];
ccColor4F endColor;
endColor.r = 0.8f;
endColor.g = 0.8f;
endColor.b = 0.8f;
endColor.a = 1.0f;
[smoke setEndColor:endColor];
[smoke setLife:0.1f];
[smoke setScaleX:0.1f];
[smoke setScaleY:0.2f];
[smoke setStartSize:30.f];
[self addChild:smoke z:2];
[smoke setPosition:dummyPosition];
-(void)smokeLoop{
id smokeMoveBy = [MoveBy actionWithDuration:durTime position:ccp(0.f, (-1.f*480))]];
id smokeSeq=[Sequence actions:[Place actionWithPosition:smokeInitPosition], smokeMoveBy, nil];
[smoke runAction:smokeSeq];
}
Not sure if this is your issue, but I had an issue with Cocos2D, scaling and moving, which I solved with moving the anchorPoint.
What I wanted to do is zoom (scale) and move a layer. Zooming would move fine if position was {0,0} and transform point was {0.5,0.5}. But then if I'd move it, it would still transform around {0.5,0.5}, which might by then be out of screen, so it would scale really weird.
Solution was to move the transform point to the middle of the screen, every time I moved the layer's position. This was not an easy formula for me to fix, as when I moved the transform point, the scale operation would have a new center point.
The formula I ended up using was the following:
layer = self.foreground;
ccpAdd(
ccpDivide(
ccpNeg(layer.position),
(CGPoint){layer.contentSize.width, layer.contentSize.height}),
(CGPoint){0.5f,0.5f}
);
Basically: Divide the inverse of the layers position (meaning, {300,200} would become {-300,-200}) by the size of the layer {480,320}, and then add {0.5,0.5} (as I want my anchor to be always center + offset)
Anyway, you may need to work out a completely different formula, but this worked for me. I had to apply this to my anchor point every time I moved the layer.
Good luck, hope this helps!