Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Game Development

Reply
 
LinkBack Thread Tools Display Modes
Old 10-21-2011, 06:14 PM   #1 (permalink)
Registered Member
 
Join Date: Jul 2011
Posts: 1
Jared is on a distinguished road
Default Who says artists can't program

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
Jared is offline   Reply With Quote
Old 10-21-2011, 11:17 PM   #2 (permalink)
Registered Member
 
Objective Zero's Avatar
 
Join Date: Oct 2010
Posts: 1,210
Objective Zero is on a distinguished road
Default

Theres usually a crash log to help you fix what the issue was, look in the console when it crashes, it should tell you.
__________________
Questions?

Check out my OCR app!
http://itunes.apple.com/app/ocr-pro/id486512712?mt=8
Objective Zero is offline   Reply With Quote
Old 10-25-2011, 09:22 AM   #3 (permalink)
Knows SQL
 
iisword's Avatar
 
Join Date: Oct 2009
Location: Somewhere the streets are on fire, the sewers are flooded, and the cats are high on catnip
Posts: 529
iisword is on a distinguished road
Default

To answer your first question, depends on how fancy you want the boat to look. You might want to look at How To Use Animations and Sprite Sheets in Cocos2D | Ray Wenderlich
__________________
iisword is offline   Reply With Quote
Old 03-15-2012, 09:57 AM   #4 (permalink)
Registered Member
 
Join Date: Mar 2012
Posts: 1
actionBob is on a distinguished road
Default

Make sure your tmx file is not referencing your tileset map through a bunch of folders. It should just look in its root directory.
actionBob is offline   Reply With Quote
Old 03-21-2012, 03:38 AM   #5 (permalink)
Registered Member
 
Games For Life's Avatar
 
Join Date: Mar 2012
Location: Los Angeles
Posts: 43
Games For Life is on a distinguished road
Send a message via ICQ to Games For Life Send a message via AIM to Games For Life
Default

GO artists go!
__________________
Jumpin Puppy
Games For Life is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 379
10 members and 369 guests
apatsufas, comicool, Creativ, Dalia, dansparrow, LunarMoon, mer10, Murphy, pbart, Tomsky
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,676
Threads: 94,127
Posts: 402,916
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jleannex55
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 06:59 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0