I'm relatively new to XCode and Obj C programming but I've been developing games for years now. It was only a month ago that I started learning game development for the iPhone/iPod and I went with Cocos 2D because its a pretty neat lib and it suits my needs well. However, being a total noob, I'm now stuck on tracking touch events w/in my app. I have a class "PlayerShip.m" and I wanted to rotate this ship from my "MainScene.m" file--how do I make a reference to it? Can you help a newbie? Below is my code:
PlayerShip.m:
#import "PlayerShip.h"
#import "Sprite.h"
#import "cocos2d.h"
@implementation PlayerShip
- (id) init
{
self = [super init];
if (self != nil) {
Sprite * ship01 = [Sprite spriteWithFile:@"ship01.png"];
ship01.position = ccp(240, 160);
[self addChild:ship01];
//animate ship infinitely
id anim01 = [[[Animation alloc] initWithName:@"lit01" delay:1/6.0] autorelease];
[anim01 addFrame:@"ship01.png"];
[anim01 addFrame:@"ship01a.png"];
[anim01 addFrame:@"ship01b.png"];
[anim01 addFrame:@"ship01c.png"];
id ship01Action = [Animate actionWithAnimation: anim01];
id repeat01Action = [RepeatForever actionWithAction:ship01Action];
[ship01 runAction:repeat01Action];
//rotate ship
id myRotAction01 = [RotateBy actionWithDuration:12 angle:360];
id RepeatAction01 = [RepeatForever actionWithAction: myRotAction01];
[ship01 runAction: RepeatAction01];
}
return self;
}
And below is my MainScene.m's touch event code:
- (BOOL)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
int i;
NSSet *allTouches = [event allTouches];
int touchcount = [allTouches count];
BOOL handled = kEventIgnored;
for (i = 0; i < touchcount; i++)
{
UITouch *touch = [[allTouches allObjects] objectAtIndex:i];
if(touch == nil)
{
continue;
}
CGPoint point = [touch locationInView: [touch view]];
point = [[Director sharedDirector] convertCoordinate: point];
CGRect rectLeft = CGRectMake(0, 0, 180, 320);
CGRect rectRight = CGRectMake(300, 0, 480, 320);
if(CGRectContainsPoint(rectLeft, point)){
Alien *alienship = [Alien node];
alienship.position = ccp(point.x, point.y);
[self addChild:alienship];
//move alien towards Earth
int speed = 1 + arc4random() % 12;
id moveAction = [MoveTo actionWithDuration:speed position:ccp(240, 160)];
[alienship runAction:moveAction];
//PLAYER SHIP SHOULD ROTATE LEFT HERE
handled = kEventHandled;
}
else if(CGRectContainsPoint(rectRight, point)){
Alien *alienship = [Alien node];
alienship.position = ccp(point.x, point.y);
[self addChild:alienship];
//move alien towards Earth
int speed = 1 + arc4random() % 12;
id moveAction = [MoveTo actionWithDuration:speed position:ccp(240, 160)];
[alienship runAction:moveAction];
//PLAYER SHIP SHOULD ROTATE RIGHT HERE
handled = kEventHandled;
}
}
return handled;
}
How can I rotate the ship to the appropriate direction when the left or right RECT is touched? Any help would be greatly appreciated! :)