~~NOTOC~~ ====== Tiledbox ====== ---- dataentry extension ---- type : extension #or 'mod' if it requires changing the Sparrow source author_mail : matt62king@gmail.com Matt King description : An interface between Tiled, Box2D, and Sparrow #enter a short description of the extension lastupdate_dt : #the date you created the extension compatible : v1.X #the Sparrow version you tested the extension with depends : Box2d (Included) #if the ext. depends on others, list them here tags : TileMap, Tiled, Box2d, Physics #enter a few tags, separated by commas homepage_url : #if the ext. has an URL (e.g. a Gist-page), add it here download_url : https://github.com/matt62king/TiledBox #a direct link to the download (e.g. the Gist-archive) ---- ===== Usage ===== TiledBox is an interface between Tiled, Box2D, and Sparrow. It is capable of rendering graphics and Box2D world based on a TMX file. TiledBox is pretty young right now. A lot of box2d functions are not yet support. The design of it is being based around a platform game. Graphics rendering, body placement, collisions and movement are currently supported. Implementation is very simple. mBXWorld = [BXWorld sharedWorldWithGravity:b2Vec2(0.0, 25.0) edgeInsets:BXWorldMakeEdgeInsets(-0.1, 10.0, -0.1, -0.1)]; mBXWorld.autoAnimate = YES; mBXWorld.contactDelegate = self; SXTileMap *tileMap = [self tileMapForWorld:_world]; //NSLog(@"%@", tileMap); SXTileViewport *viewport = [[SXTileViewport alloc] initWithLayer:[tileMap layerNamed:@"Background"]]; [self addChild:viewport]; mViewport = viewport; TATom *tom = [[TATom alloc] initWithPosition:CGPointMake((self.width / 2.0) - 15.0, 100.0)]; tom.canJump = NO; tom.canMove = YES; SXTileViewport *levelViewport = [[SXTileViewport alloc] initWithLayer:[tileMap layerNamed:@"Forground"] detectObjects:YES]; levelViewport.automaticallyDrawObjects = YES; levelViewport.delegate = self; levelViewport.actor = tom; [self addChild:levelViewport]; mLevelViewport = levelViewport; #if BX_DRAW_WORLD SXDebugDraw *debugDraw = [[SXDebugDraw alloc] initWithWorld:mBXWorld]; [self addChild:debugDraw]; #endif From there you just have to handle movement and collisions on your own. TiledBox supports the drawing of interactive tile objects through call backs via a delegate: - (SPDisplayObject *)displayObjectForObject:(SXTileObject *)object viewport:(SXTileViewport *)viewport { if ([object.name hasPrefix:@"Nut"]) { CGPoint location = [mLevelViewport locationInViewport:object.rect.origin]; TACoin *coin = [[TACoin alloc] initWithPosition:location bodyType:BXStaticBody]; coin.x = location.x; coin.y = location.y; return coin; } return nil; } The limitation is that the custom object must be placed in Tiled in a specific format under one of two distinct object groups. DisplayObjects - Custom objects for display that will not interact with the player. (Currently only "Text" types are supported) b2Objects - Custom objects that can interact with the player. (Currently only "StaticBody" types are supported) When you layout your object group ensure you label the object group name and object types correctly. It is also important to make sure all your objects have a different name. Here is an example of what the object output should look like: Here is the end result: Note: The below screen shot is with box2d debug draw enabled. {{ :extensions:screenshot_2013.02.05_21.41.21.png |}} ===== Changelog ===== * //2013/02/06 03:29//: First public version ===== Source Code ===== [[https://github.com/matt62king/TiledBox]] Source includes the box2d source as well ===== Discussion ===== //No comments so far. Feel free to edit this part of the page.//