Exemplo n.º 1
0
void HelloWorld::ccTouchesBegan(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent)
{
	CCNode* node = this->getChildByTag(TileMapNode);	
	//NSAssert([node isKindOfClass:[CCTMXTiledMap class]], @"not a CCTMXTiledMap");
	CCTMXTiledMap* tileMap = (CCTMXTiledMap*)node;
	
	// get the position in tile coordinates from the touch location
	CCPoint touchLocation = this->locationFromTouch((cocos2d::CCTouch *)pTouches->anyObject());	
	CCPoint tilePos = this->tilePosFromLocation(touchLocation, tileMap);	

	//// move tilemap so that touched tiles is at center of screen
	this->centerTileMapOnTileCoord(tilePos, tileMap);

	// Check if the touch was on water (eg. tiles with isWater property drawn in GameEventLayer)
	bool isTouchOnWater = false;	
	CCTMXLayer* eventLayer = tileMap->layerNamed("GameEventLayer");
	int tileGID = eventLayer->tileGIDAt(tilePos);	
	//
	if (tileGID != 0) {
		CCDictionary* properties = tileMap->propertiesForGID(tileGID);
		if (properties) {
			//CCLOG(@"NSDictionary 'properties' contains:\n%@", properties);	
			const CCString * isWaterProperty = properties->valueForKey("isWater");
			isTouchOnWater = isWaterProperty->boolValue();			
		}
	}
	
	// Check if the touch was within one of the rectangle objects
	CCTMXObjectGroup* objectLayer = tileMap->objectGroupNamed("ObjectLayer");
	//NSAssert([objectLayer isKindOfClass:[CCTMXObjectGroup class]], 
	//		 @"ObjectLayer not found or not a CCTMXObjectGroup");
	
	bool isTouchInRectangle = false;
	int numObjects = objectLayer->getObjects()->count();
	for (int i = 0; i < numObjects; i++){
		CCDictionary* properties = (CCDictionary*)objectLayer->getObjects()->objectAtIndex(i);
		CCRect rect = this->getRectFromObjectProperties(properties, tileMap);
		//	
		if (CCRect::CCRectContainsPoint(rect, touchLocation)) {
			isTouchInRectangle = true;
			break;
		}
	}
	
	// decide what to do depending on where the touch was ...
	if (isTouchOnWater) {
		//[[SimpleAudioEngine sharedEngine] playEffect:@"alien-sfx.caf"];
		CCLog("touchOnWater");
	}
	else if (isTouchInRectangle) {
		CCLog("touchObject");
		CCParticleSystem* system = CCParticleSystemQuad::particleWithFile("fx-explosion.plist");
		system->setAutoRemoveOnFinish(true);
		system->setPosition(touchLocation);
		this->addChild(system, 1);
	}
	else {

#if 0
		// get the winter layer and toggle its visibility
		CCTMXLayer* winterLayer = tileMap->layerNamed("WinterLayer");
		winterLayer->setVisible(!winterLayer->isVisible());
		
		// other options you might be interested in are:	

		// remove the touched tile		
		winterLayer->removeTileAt(tilePos);		
		// add a specific tile
		tileGID = winterLayer->tileGIDAt(CCPointMake(0, 19));
		winterLayer->setTileGID(tileGID, tilePos);
#endif

	}
}