Collisions and Collectables: How To Make a Tile-Based Game with Cocos2D 2.X Part 2
9 posts
• Page 1 of 1
Collisions and Collectables: How To Make a Tile-Based Game with Cocos2D 2.X Part 2
This is the official thread to discuss the following blog post: Collisions and Collectables: How To Make a Tile-Based Game with Cocos2D 2.X Part 2
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
Ray Wenderlich
Blog: http://www.raywenderlich.com
Twitter: http://twitter.com/rwenderlich
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
Ray Wenderlich
Blog: http://www.raywenderlich.com
Twitter: http://twitter.com/rwenderlich
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
-

rwenderlich - Site Admin
- Posts: 1776
- Joined: Thu Dec 23, 2010 4:14 pm
- Has thanked: 26 times
- Been thanked: 187 times
Re: Collisions and Collectables: How To Make a Tile-Based Ga
Brilliant tutorials, clear and easy to follow thanks!
Can you clarify that the method you use to detect collectables and collisions is the best approach to use? I mean is this the most popular way most game developers use when using a tool such as Tiled?
I ask this because I notice that the ninja does not always reach the wall set as a collision, probably because the wall has half background, half wall in the tile so it makes it appear that there is an invisible barrier between the ninja and the wall. I'm thinking different artwork would resolve this?
Thanks again
Can you clarify that the method you use to detect collectables and collisions is the best approach to use? I mean is this the most popular way most game developers use when using a tool such as Tiled?
I ask this because I notice that the ninja does not always reach the wall set as a collision, probably because the wall has half background, half wall in the tile so it makes it appear that there is an invisible barrier between the ninja and the wall. I'm thinking different artwork would resolve this?
Thanks again
- elpuerco63
- Uber Haxx0r
- Posts: 226
- Joined: Tue Jun 28, 2011 10:16 am
- Has thanked: 1 time
- Been thanked: 2 times
Re: Collisions and Collectables: How To Make a Tile-Based Ga
elpuerco63 Thanks for the kind words.
Welcome to the world of programmer art
This art comes from Tiled and was used to test functionality. So yeah, you can definitely get weird looking artifacts because the collision detection is on the entire tile.
There are definitely other ways to do collision detection. I'm not sure which of them are the most popular.
Welcome to the world of programmer art
There are definitely other ways to do collision detection. I'm not sure which of them are the most popular.
- charlie
- iOS Tutorial Team Member
- Posts: 80
- Joined: Sun Jan 02, 2011 7:40 am
- Has thanked: 2 times
- Been thanked: 9 times
Re: Collisions and Collectables: How To Make a Tile-Based Ga
Great tutorial!!!!
but when I tried to run the project, I am getting following error:
Cocos2d.h: No such file or directory.
I tried to add my cocos2d files but no luck.
How can I add cocos2d library to existing project?
Thanks a lot!
but when I tried to run the project, I am getting following error:
Cocos2d.h: No such file or directory.
I tried to add my cocos2d files but no luck.
How can I add cocos2d library to existing project?
Thanks a lot!
Re: Collisions and Collectables: How To Make a Tile-Based Ga
romox wrote:Great tutorial!!!!
but when I tried to run the project, I am getting following error:
Cocos2d.h: No such file or directory.
I tried to add my cocos2d files but no luck.
How can I add cocos2d library to existing project?
Thanks a lot!
Hey, the sample project did not have cocos2d included. To add it to the sample project or any other existing project do these steps:
- download cocos2d 2.1 rc0
in finder browse to the location of the download
drag the file cocos2d-ios.xcodeproj into your Xcode project.
then setup your build phases and build settings like it mentions in the tutorial
Oh one more thing the sample project expects cocos2d (via relative path setting) in ~/Downloads/cocos2d-iphone-1.1-RC0 so if you download cocos2d to that direct everything should be good to go.
I'll try to upload a new project today that already has cocos2d setup as an external reference in it.
- charlie
- iOS Tutorial Team Member
- Posts: 80
- Joined: Sun Jan 02, 2011 7:40 am
- Has thanked: 2 times
- Been thanked: 9 times
Re: Collisions and Collectables: How To Make a Tile-Based Ga
Great tutorial(as always) but Im on a small detail..
Here's the deal I just finished collision part and Im trying to run the game but it pauses...It compiles but then the simulator automatically pauses(yes, I checked if there was any breaks..) and it points out something like this:
On rect.origin.x there's a green arrow with the following info:
Thread 1: EXC_ARITHMETIC(code=EXC_1386_DIV,subcode=0x0)
Here's the debugger:
cocos2d: **** WARNING **** CC_ENABLE_GL_STATE_CACHE is disabled. To improve performance, enable it by editing ccConfig.h
From what I can tell it isn't finding the files but they're there....
I tried redoing the map but still didn't work.
Also, I posted something on PT.1 of this tutorial, I don't know if it has something to do with it.
http://www.raywenderlich.com/forums/viewtopic.php?f=20&t=6506&p=39980#p39980
Here's the deal I just finished collision part and Im trying to run the game but it pauses...It compiles but then the simulator automatically pauses(yes, I checked if there was any breaks..) and it points out something like this:
- Code: Select all
-(CGRect) rectForGID:(unsigned int)gid
{
CGRect rect;
rect.size = tileSize_;
gid &= kCCFlippedMask;
gid = gid - firstGid_;
int max_x = (imageSize_.width - margin_*2 + spacing_) / (tileSize_.width + spacing_);
// int max_y = (imageSize.height - margin*2 + spacing) / (tileSize.height + spacing);
rect.origin.x = (gid % max_x) * (tileSize_.width + spacing_) + margin_;
rect.origin.y = (gid / max_x) * (tileSize_.height + spacing_) + margin_;
return rect;
}
On rect.origin.x there's a green arrow with the following info:
Thread 1: EXC_ARITHMETIC(code=EXC_1386_DIV,subcode=0x0)
Here's the debugger:
cocos2d: **** WARNING **** CC_ENABLE_GL_STATE_CACHE is disabled. To improve performance, enable it by editing ccConfig.h
- Code: Select all
2013-04-17 11:04:47.058 TileGame[12552:15203] cocos2d: cocos2d v2.0.0
2013-04-17 11:04:47.058 TileGame[12552:15203] cocos2d: Using Director Type:CCDirectorDisplayLink
2013-04-17 11:04:47.408 TileGame[12552:15203] cocos2d: animation started with frame interval: 60.00
2013-04-17 11:04:47.410 TileGame[12552:15203] cocos2d: surface size: 480x320
2013-04-17 11:04:47.613 TileGame[12552:15203] cocos2d: CCTexture2D: Using RGB565 texture since image has no alpha
2013-04-17 11:04:49.480 TileGame[12552:15203] -[CCFileUtils fullPathFromRelativePath:resolutionType:] : cocos2d: Warning: File not found: TileGameResources/tmw_desert_spacing.png
2013-04-17 11:04:49.482 TileGame[12552:15203] cocos2d: CCTexture2D. Can't create Texture. cgImage is nil
2013-04-17 11:04:49.483 TileGame[12552:15203] cocos2d: Couldn't add image:TileGameResources/tmw_desert_spacing.png in CCTextureCache
(lldb)
From what I can tell it isn't finding the files but they're there....
I tried redoing the map but still didn't work.
Also, I posted something on PT.1 of this tutorial, I don't know if it has something to do with it.
http://www.raywenderlich.com/forums/viewtopic.php?f=20&t=6506&p=39980#p39980
You get what you give.
Here's me trying to play guitar :
http://www.soundcloud.com/xiiix
Thanks to everyone that has been patient enough to help me haha
If you need something translated to spanish, let me know!
Here's me trying to play guitar :
http://www.soundcloud.com/xiiix
Thanks to everyone that has been patient enough to help me haha
If you need something translated to spanish, let me know!
-

GumbiRo - Uber Haxx0r
- Posts: 58
- Joined: Sun Oct 21, 2012 3:18 pm
- Location: Mexico City
- Has thanked: 22 times
- Been thanked: 0 time
Re: Collisions and Collectables: How To Make a Tile-Based Ga
Hello everyone!
So...I decided to do everything myself once again from scratch. I hadn't noticed but the map takes the information previously given to it for the tiles...(I had move the entire project to another folder before the error). It turns out the map wasn't finding the files so I had to redo the map again with the correct paths.
And it worked like a charm!(hope this helps someone else in the future)
Have a great day everyone!
So...I decided to do everything myself once again from scratch. I hadn't noticed but the map takes the information previously given to it for the tiles...(I had move the entire project to another folder before the error). It turns out the map wasn't finding the files so I had to redo the map again with the correct paths.
And it worked like a charm!(hope this helps someone else in the future)
Have a great day everyone!
You get what you give.
Here's me trying to play guitar :
http://www.soundcloud.com/xiiix
Thanks to everyone that has been patient enough to help me haha
If you need something translated to spanish, let me know!
Here's me trying to play guitar :
http://www.soundcloud.com/xiiix
Thanks to everyone that has been patient enough to help me haha
If you need something translated to spanish, let me know!
-

GumbiRo - Uber Haxx0r
- Posts: 58
- Joined: Sun Oct 21, 2012 3:18 pm
- Location: Mexico City
- Has thanked: 22 times
- Been thanked: 0 time
Re: Collisions and Collectables: How To Make a Tile-Based Ga
Hey glad you figured it out. If you still have the old project around you can actually manually edit the tile map files, tmx file to point to the correct location using the correct path to the image file.
- charlie
- iOS Tutorial Team Member
- Posts: 80
- Joined: Sun Jan 02, 2011 7:40 am
- Has thanked: 2 times
- Been thanked: 9 times
Collisions not working!!!
I have been going through these awesome tutorials for a while and trying to make the next BIG game...lol... I have started over I think a hundred times. So now I am working on the whole walking through walls part. I am using the accelerometer and here is my code from that:
That works great... I think. So I think I am following the example pretty close and here is what I have.
So when my sprite hits the wall he just runs right through it. In the console it is outputting that the properties are there and it is true:
any clue why this is happening?
- Code: Select all
- (void)update:(ccTime)dt {
[self updateOfficer];
// [self setPlayerPosition:_officer.position];
[self setViewPointCenter:self.officer.position];
}
-(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration { //new
//constant of 6.0 is for tilt of device calibration
officerSpeedY = 6.0 + acceleration.x*10;
officerSpeedX = -acceleration.y*10;
}
-(void)updateOfficer { //new
[self setPlayerPosition:_officer.position];
float maxY = 1600 - _officer.contentSize.height/2;
float minY = _officer.contentSize.height/2;
float newY = _officer.position.y + officerSpeedY;
newY = MIN(MAX(newY, minY), maxY);
float maxX = 1600 - _officer.contentSize.width/2;
float minX = _officer.contentSize.width/2;
float newX = _officer.position.x + officerSpeedX;
newX = MIN(MAX(newX, minX), maxX);
_officer.position = ccp(newX, newY);
CGPoint moveTo = _officer.position;
[_officer moveToward:moveTo];
}
That works great... I think. So I think I am following the example pretty close and here is what I have.
- Code: Select all
-(void)setPlayerPosition:(CGPoint)position {
CGPoint tileCoord = [self tileCoordForPosition:position];
int tileGid = [_meta tileGIDAt:tileCoord];
if (tileGid) {
NSDictionary *properties = [_tileMap propertiesForGID:tileGid];
if (properties) {
CCLOG(@"NSDictionary 'properties' contains:\n%@", properties);
NSString *collision = properties[@"Collidable"];
if (collision && [collision isEqualToString:@"True"]) {
NSLog(@"coll");
return;
}
}
}
_officer.position = position;
}
- (CGPoint)tileCoordForPosition:(CGPoint)position {
int x = position.x / _tileMap.tileSize.width;
int y = ((_tileMap.mapSize.height * _tileMap.tileSize.height) - position.y) / _tileMap.tileSize.height;
return ccp(x, y);
}
So when my sprite hits the wall he just runs right through it. In the console it is outputting that the properties are there and it is true:
- Code: Select all
2013-05-09 14:21:40.391 10-8[5447:907] NSDictionary 'properties' contains:
{
Collidable = True;
}
any clue why this is happening?
9 posts
• Page 1 of 1
Who is online
Users browsing this forum: No registered users and 3 guests