Well I'm an artist and figured I'd run through some tutorials on Objective C and Cocos 2D to learn how all the parts fit together so I can better create artwork for the iPhone/iPad. I'm in over my head but I'm sticking with it. I want to at least put together a few proof in concepts to show others hey look what I can do.
Anyway here is my first phase:
Move a boat around in the water. User taps boat to select the boat then taps screen to indicate where they want to boat to go. Boat moves to selected position. Keep the selected boat in the center of the screen.
Pinch open and close to view more/less of the map.
I'd also like to add a wake affect/animation when the boat moves thought the water and have the boat move in a realistic patter ie smooth gradual turns.
Latter phases will include adding other ships/obstacles and shooting at them.
I've stated with a tutorial from Bucky on TheNewBoston on Cocos 2D. I'm having a problem in that whenever I click on the screen the program quits.
So my first questions keeping in mind that I'm a designer and still learning thing the lingo:
1. What elements/resources do I need to create? Ive got a background a ship but I'm not sure what I need to create to make it animate
2. what is causing my program to quit?
Code:
//
// HelloWorldLayer.m
// carrier_command
//
// Created by Jared Bishop on 10/20/11.
// Copyright Bishop Arts 2011. All rights reserved.
//
// Import the interfaces
#import "HelloWorldLayer.h"
// HelloWorldLayer implementation
@implementation HelloWorldLayer
@synthesize theMap;
@synthesize bgLayer;
@synthesize stLayer;
@synthesize player;
+(CCScene *) scene
{
// 'scene' is an autorelease object.
CCScene *scene = [CCScene node];
// 'layer' is an autorelease object.
HelloWorldLayer *layer = [HelloWorldLayer node];
// add layer as a child to scene
[scene addChild: layer];
// return the scene
return scene;
}
// on "init" you need to initialize your instance
-(id) init
{
// always call "super" init
// Apple recommends to re-assign "self" with the "super" return value
if( (self=[super init])) {
self.isTouchEnabled = YES;
self.theMap = [CCTMXTiledMap tiledMapWithTMXFile:@"tiled_map_space_01.tmx"];
self.bgLayer = [theMap layerNamed:@"bg"];
self.stLayer = [theMap layerNamed:@"st"];
stLayer.visible = NO;
CCTMXObjectGroup *objects = [theMap objectGroupNamed:@"oj"];
NSMutableDictionary *startPoint = [objects objectNamed:@"StartPoint"];
int x = [[startPoint valueForKey:@"x"]intValue];
int y = [[startPoint valueForKey:@"y"]intValue];
self.player = [CCSprite spriteWithFile:@"carrier_top_01.png"];
player.position = ccp(x,y);
[self addChild:player];
[self setCenterOfScreen:player.position];
[self addChild:theMap z:-1];
}
return self;
}
-(void) setCenterOfScreen:(CGPoint) position{
CGSize screenSize = [[CCDirector sharedDirector]winSize];
int x = MAX(position.x, screenSize.width/2);
int y = MAX(position.y, screenSize.height/2);
x = MIN(x, theMap.mapSize.width * theMap.tileSize.width - screenSize.width/2);
y = MIN(y, theMap.mapSize.height * theMap.tileSize.height - screenSize.height/2);
CGPoint goodPoint = ccp(x,y);
CGPoint centerOfScreen = ccp(screenSize.width/2, screenSize.height/2);
CGPoint difference = ccpSub(centerOfScreen, goodPoint);
self.position = difference;
}
// on "dealloc" you need to release all your retained objects
- (void) dealloc
{
self.theMap = nil;
self.bgLayer = nil;
self.stLayer = nil;
self.player = nil;
[super dealloc];
}
-(void) registerWithTouchDispatcher{
[[CCTouchDispatcher sharedDispatcher]addTargetedDelegate:self priority:0 swallowsTouches:YES];
}
-(BOOL) ccTouchesBeganWithEvent:(UITouch *)touch withEvent:(UIEvent *)event{
return YES;
}
-(void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event{
CGPoint touchLocation = [touch locationInView: [touch view]];
touchLocation = [[CCDirector sharedDirector] convertToGL:touchLocation];
touchLocation = [self convertToNodeSpace:touchLocation];
CGPoint playerPos = player.position;
CGPoint diff = ccpSub(touchLocation, playerPos);
//move horisonal or vertical?
if (abs(diff.x) > abs(diff.y)) {
if (diff.x > 0) {
playerPos.x += theMap.tileSize.width;
} else {
playerPos.x-= theMap.tileSize.width;
}
} else {
if (diff.y > 0) {
playerPos.y += theMap.tileSize.height;
} else {
playerPos.y-= theMap.tileSize.height;
}
}
//make sure the new position off the map
if (playerPos.x <= (theMap.mapSize.width * theMap.tileSize.width) &&
playerPos.y <= (theMap.mapSize.height * theMap.tileSize.height) &&
playerPos.y >= 0 &&
playerPos.x >= 0 )
{
[self setPlayerPosition:playerPos];
}
[self setCenterOfScreen:player.position];
}
-(void) setPlayerPosition:(CGPoint)position{
CGPoint tileCoord = [self tileCoordForPosition:position];
int tileGid = [stLayer tileGIDAt:tileCoord];
if (tileGid) {
NSDictionary *properties = [theMap propertiesForGID:tileGid];
if(properties){
NSString *collision = [properties valueForKey:@"Collidable"];
if (collision && [collision compare:@"True"] ==NSOrderedSame) {
return;
}
}
}
player.position = position;
}
-(CGPoint)tileCoordForPosition:(CGPoint)position{
int x = position.x/theMap.tileSize.width;
int y = ((theMap.mapSize.height * theMap.tileSize.height)-position.y)/theMap.tileSize.height;
return ccp(x,y);
}
@end