Collisions and Collectables: How To Make a Tile-Based Game with Cocos2D 2.X Part 2

Discussion of the official tutorials published on raywenderlich.com. Please only discuss the official tutorials here - for general questions, use the General Discussion forum instead.

Collisions and Collectables: How To Make a Tile-Based Game with Cocos2D 2.X Part 2

Postby rwenderlich » Fri Feb 08, 2013 12:00 pm

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
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
User avatar
rwenderlich
Team Member
Site Admin
 
Posts: 2058
Joined: Thu Dec 23, 2010 4:14 pm
Has thanked: 28 times
Been thanked: 293 times

Re: Collisions and Collectables: How To Make a Tile-Based Ga

Postby elpuerco63 » Mon Feb 11, 2013 7:31 am

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 ;)
elpuerco63
Subscriber since Mar 3, 2014
Uber Haxx0r
 
Posts: 274
Joined: Tue Jun 28, 2011 10:16 am
Has thanked: 8 times
Been thanked: 7 times

Re: Collisions and Collectables: How To Make a Tile-Based Ga

Postby charlie » Wed Feb 13, 2013 12:16 pm

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.
charlie
Team Member
iOS Tutorial Team Member
 
Posts: 111
Joined: Sun Jan 02, 2011 7:40 am
Has thanked: 3 times
Been thanked: 12 times

Re: Collisions and Collectables: How To Make a Tile-Based Ga

Postby romox » Mon Feb 25, 2013 6:37 am

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!
romox
Subscriber since Feb 11, 2014
Hacker
 
Posts: 37
Joined: Sat Jan 12, 2013 5:13 am
Has thanked: 3 times
Been thanked: 0 time

Re: Collisions and Collectables: How To Make a Tile-Based Ga

Postby charlie » Mon Feb 25, 2013 2:09 pm

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
Team Member
iOS Tutorial Team Member
 
Posts: 111
Joined: Sun Jan 02, 2011 7:40 am
Has thanked: 3 times
Been thanked: 12 times

Re: Collisions and Collectables: How To Make a Tile-Based Ga

Postby GumbiRo » Tue Apr 16, 2013 5:44 pm

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:



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
If you need something translated to spanish, let me know! :D
User avatar
GumbiRo
Uber Haxx0r
 
Posts: 96
Joined: Sun Oct 21, 2012 3:18 pm
Has thanked: 31 times
Been thanked: 1 time

Re: Collisions and Collectables: How To Make a Tile-Based Ga

Postby GumbiRo » Wed Apr 17, 2013 5:27 pm

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!
If you need something translated to spanish, let me know! :D
User avatar
GumbiRo
Uber Haxx0r
 
Posts: 96
Joined: Sun Oct 21, 2012 3:18 pm
Has thanked: 31 times
Been thanked: 1 time

Re: Collisions and Collectables: How To Make a Tile-Based Ga

Postby charlie » Wed Apr 17, 2013 5:45 pm

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
Team Member
iOS Tutorial Team Member
 
Posts: 111
Joined: Sun Jan 02, 2011 7:40 am
Has thanked: 3 times
Been thanked: 12 times

Collisions not working!!!

Postby mykdw » Thu May 09, 2013 11:46 pm

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:
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?
mykdw
Hacker
 
Posts: 13
Joined: Sat Jun 09, 2012 1:17 am
Has thanked: 0 time
Been thanked: 1 time

Re: Collisions and Collectables: How To Make a Tile-Based Ga

Postby quaker_z » Tue Jun 04, 2013 9:37 am

I found it bit of frustrating after building/running the game on my iOS simulator,
the dixdoface ninja(no offence, that's what my ex-gf call this tiny ninja) appears kinda deviate from the place it supposed to display, like a little higher/ left.

finally figured out it's something about ANCHOR, it's mentioned at the first cocos2d simple game example from great raywenderlich's tutorial.

the _player.anchor was 0.5, 0.5 (by default)

so i added below at the -(void)init, before the [self addChild:_player];

Code: Select all

_player.anchorPoint = ccp(0, 1);
[self addChild:_player];



now the little ninja is appeared at the correct place.
thanks RAYWENDERLICH's great tutorial.
quaker_z
n00b
 
Posts: 1
Joined: Tue Jun 04, 2013 9:24 am
Has thanked: 0 time
Been thanked: 0 time

Next

Return to Official Tutorials

Who is online

Users browsing this forum: No registered users and 11 guests