First off I would like to thank Andreas for making his TexturePacker software available for free to development bloggers – http://www.texturepacker.com/ and Mike of www.71squared.com for providing me with a copy of Particle Designer which I will do a separate blog on later once I have had time to play with it some more – http://particledesigner.71squared.com/
I have been playing with Cocos2d for a little while now and have read lots of tutorials, forum posts and blogs and have decided to share this post as I have not seen any posts about rotating a CCsprite in Cocos2d – http://www.cocos2d-iphone.org/ – using the degrees from SneakyInput – https://github.com/sneakyness/SneakyInput and changing the sprite as it rotates using a spritesheet.
I am going to be using this spritesheet:
Here is a quick youTube click of how it all looks running:
If you want to follow along you can download the source code from here – http://dl.dropbox.com/u/60608455/Simple_Ship_With_Rotation.zip
In GameScene.m file, we just create our sprite cache:
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@”Simple_Ship.plist”];
then our player:
player = [[Player alloc] initWithGame:self]; – this also sets our start frame for our sprite to display.
and then our joystick:
gameJoystick = [[JoyStick alloc] initWithGame:self]; – I have set up a right hand side joystick as well which is not used as yet as I want to control my bullets separately to the player. I have included a simple skin for the joystick as well. For some reason I have to create the texture cache again in the Joystick.m file as it can not see it otherwise. If anyone knows how to get around this, please leave a comment.
All of this code I have found out about in other tutorials:
sharedSpriteFrameCache – http://www.learn-cocos2d.com/
SneakyInput Tutorial – http://www.qcmat.com/sneakyinput-joystick-dpad-and-buttons-for-cocos2d/
Changing the CCSprite display frame – http://www.cocos2d-iphone.org/wiki/doku.php/prog_guide:animation#using_sprite_sheets
Then in our update function all the magic happens:
Using CGSize winSize = [[CCDirector sharedDirector] winSize];
We put the player on the other side of the screen when it goes off the screen.
//Keep the Player on the iPad screen by putting player on other side of screen when it goes off the screen
if (player.playerSprite.position.y >= winSize.height+4) {
[player.playerSprite setPosition:ccp(player.playerSprite.position.x, -10)];
}
if (player.playerSprite.position.y <-10) {
[player.playerSprite setPosition:ccp(player.playerSprite.position.x, winSize.height+4)];
}
if (player.playerSprite.position.x >=winSize.width+4) {
[player.playerSprite setPosition:ccp(-10, player.playerSprite.position.y)];
}
if (player.playerSprite.position.x <-10) {
[player.playerSprite setPosition:ccp(winSize.width+4, player.playerSprite.position.y)];
}
Then using the degrees from our joystick we set the appropriate frame to display for our sprite.
int i = (int) gameJoystick.leftJoystick.degrees/6; //work out the animation frame we need depending on the Joystick Degree
//Then we create the frame name using Simple_Ship and the value of the int i
NSString *playerframes = [NSString stringWithFormat:@”Simple_Ship00%02i.png”, i];
//Then get the frame to show
CCSpriteFrame *frame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:playerframes];
//Then assign the frame to display to the player sprite
[player.playerSprite setDisplayFrame:frame];
Our animation has 61 frames going from 0 to 60. You need to include a frame 0 as there is a 0 degrees value.
Also I found out that your animation needs to go in a anti-clockwise direction as that is the way sneakyInput degrees goes.
I divide the degrees by 6 as there are 61 frames.
Have a play with the source code and if there is a better way of doing any thing please feel free to leave a comment with the details.
Bruno Mikoski said:
Hot to add some shooting? to that?
Joe said:
Why not just use the rotate function to rotate your sprite?
craigoz said:
Yes you could use the rotate function but I am not using flat sprites without highlights. I have rendered out the fighter for my game and the lighting needs to be from the same direction for all angles of my sprite – so I went with the above solution
alexander shabetayev said:
This is a great tut i have been looking all over for this!!
How would I go about rotating only the sprite itself?
Thanks in advance!
Alex S.
craigoz said:
mySprite.rotation = 45; or what ever you want to use.
Alexey Kulikov said:
Hi,
thanks for this brilliant tutorial, is really a bliss. There is a problem, however, that SneakyInput does not work with the more recent versions of Cocos2d, have you encountered these perhaps?
craigoz said:
Alexey
I have not had any issues with SneakyInput and the current version of Cocos2d, it all works fine for me.
Craig
craigoz said:
If you are using Cocos2d 2.0 then you need to use the updated sneakyInput code from here:
https://github.com/clops/SneakyInput
Craig
Drusilla Amick said:
THANK YOU! This was great to read and I’m glad these ideas were pointed out !
landscaping ideas for front of house said:
It was an extremely nice theme! Just would like say thank you for the information you might have fanned. Just continue composing this type of post. I’ll be your faithful reader. Thanks once again.
skuteczne pozycjonowanie said:
Hi, Can I just say what a relief to find someone who actually knows what theyre talking about on the internet. You definitely know how to bring an issue to light and make it important. More people need to read this and understand this side of the story. I cant believe youre not more popular because you definitely have the gift.
Val Lingbeck said:
so much fantastic information on here, : D.
allthewayapps said:
Thanks for this, definitely useful 🙂