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));
Related
I have a sublclass of a CCSprite that rotates throughout the game and there is a shield that has to rotate around the sprite according to the sprite's rotation. So if the sprite's rotation is 75 degrees there should be a CGRect located at 75 degrees. The dimensions of the CGRect are subordinate as it almost resembles a square.
What I did is:
I subclassed CCSprite and added a property called shieldArea.
Upon initialization I set this rect to be
self.shieldArea = CGRectMake(self.position.x-30, self.position.y, 8, 10);
Then I rotate the sprite itself, however, the rect stays at its initial position.
I hoped that the CGrect would be affected by the rotation, but I kind of expected it not to affect it, of course, why should it ? So, my question is, how do I rotate a CGRect at all ? Or do I have to add a new CGRect all the time ?
Side notes: I do not want to use Box2d or anything the like. I handle collision detection myself.
Have you tried CGAffineTransform?
Something like this:
float centerX = myOldRect.origin.x + (myOldRect.size.width / 2.0);
float centerY = myOldRect.origin.y + (myOldRect.size.height / 2.0);
CGAffineTransform rotation = CGAffineTransformMakeRotation(someAngleInRadians);
CGAffineTransform moveAnchor = CGAffineTransformMakeTranslation(centerX, centerY);
CGAffineTransform centeredRotation = CGAffineTransformConcat(moveAnchor, rotation);
CGRect rotatedRect = CGRectApplyAffineTransform(myOldRect, centeredRotation);
Note, this is NOT tested. Use at your own risk :p
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.
I got 2 sprites. I use the boundingbox to check for collision with CGRectIntersectsRect. But it is not working.
HBBall and HBpaddle has a CCSprite called image.
Init:
ball = [[HBBall alloc] init];
ball.position = ccp(150, 50);
[self addChild:ball];
[update addObject:ball];
paddle1 = [[HBPaddle alloc] init];
paddle1.position = ccp(50, 160);
[self addChild:paddle1];
Update:
if (CGRectIntersectsRect([paddle1.image boundingBox], [ball.image boundingBox]))
CCLOG(#"ball hit paddle");
CGRectIntersectsRect retuns always true. Does anyone have an idea?
you cant pass directly the bounding box, because it's relative to the sprite. You MUST pass the absolute CGRect boundingbox like this:
s = CCsprite
s.anchorPoint = ccp(0, 0);
CGRect absoluteBox = CGRectMake(s.position.x, s.position.y, [s boundingBox].size.width, [s boundingBox].size.height);
make necessary adjustments!
hope can help!
http://www.iphonedevsdk.com/forum/iphone-sdk-game-development/17082-cocos2d-collision-detection-between-sprites.html ? Have you googled? This seems like it would be a pretty basic issue in the cocos2d framework.
//CCRotateBy
id action=[CCRotateBy actionWithDuration:1.0 angle:45];
[player runAction:action];
//CCRotateTo
id action=[CCRotateTo actionWithDuration:1.0 angle:45];
[player runAction:action];
the above two code produce same results...I need to know the difference between using rotateTo and rotateBy...
please advise...
CCRotateTo rotates the object to the specified angle, while CCRotateBy rotates the object to its current angle + the specified angle. They would be equivalent if your object's initial rotation is 0. However, if its initial angle was 90, CCRotateTo would rotate it towards angle 45, while CCRotateBy would rotate it towards angle 135.
CCRotateBy is also very handy when it comes to ever rotating sprites:
CCSprite *halo = [CCSprite spriteWithFile:#"halo.png"];
[halo setOpacity:160];
CCRotateTo * rotRight = [CCRotateBy actionWithDuration:0.5 angle:40.0];
[halo runAction:[CCRepeatForever actionWithAction:rotRight]];
with the CCRotateBy action you never need to think about any angles...
I have a sprite which will move right,left and jump.
I need to add the action to a animated sprite ie an animated sprite should jump, turn right and left.
Can anyone please tell me how to do it with sample code.
This is quite simple with cocos2d code here:
Sprite *mySprite = [Sprite spriteWithFile#"mySprite.png"];
[mySprite setPosition:ccp(x,y)];
[self addChild:mySprite]; //This displays the Sprite in your layer
Now for the sequence you intend to do...
id moveRight = [MoveBy actionWithDuration:2 position ccp(x+k,y) //Where k is how much to the right you want it to go.
id moveLeft = [MoveBy actionWithDuration:2 position ccp(x-k,y)];
id jump = [JumpBy actionWithDuration:1 position:ccp (x,y) height:1 jumps:1];
id sequence = [Sequence actions:moveRight,moveLeft,jump,nil];
[mySprite runAction:sequence];
Hope that's clear.
-Oscar