PushBoxScene::PushBoxScene()
{
    TMXTiledMap* mytmx = TMXTiledMap::create("Pushbox/map.tmx");
    mytmx->setPosition(visibleSize.width / 2, visibleSize.height / 2);
    mytmx->setAnchorPoint(Vec2(0,0));
    myx = (visibleSize.width - mytmx->getContentSize().width) / 2;
    myy = (visibleSize.height - mytmx->getContentSize().height) / 2;
    this->addChild(mytmx, 0);

    count = 0;
    success = 0;
    /*mon = Sprite::create("Pushbox/player.png");
    mon->setAnchorPoint(Vec2(0, 0));
    mon->setPosition(Vec2(SIZE_BLOCK*1+myx, SIZE_BLOCK*8+myy));
    mon->setTag(TAG_PLAYER);
    this->addChild(mon, 1);*/



    TMXObjectGroup* objects = mytmx->getObjectGroup("wall");
    //从对象层中获取对象数组
    ValueVector container = objects->getObjects();
    //遍历对象
    for (auto obj : container) {
        ValueMap values = obj.asValueMap();
        int x = values.at("x").asInt();
        int y = values.at("y").asInt();
        Sprite* temp = Sprite::create("Pushbox/wall.png");
        temp->setAnchorPoint(Point(0, 0));
        temp->setPosition(Point(x,y+64));
        mywall.pushBack(temp);
        this->addChild(temp, 1);
    }

    TMXObjectGroup* objects1 = mytmx->getObjectGroup("box");
    //从对象层中获取对象数组
    ValueVector container1 = objects1->getObjects();
    //遍历对象
    for (auto obj : container1) {
        ValueMap values = obj.asValueMap();
        int x = values.at("x").asInt();
        int y = values.at("y").asInt();
        Sprite* temp = Sprite::create("Pushbox/box.png");
        temp->setAnchorPoint(Point(0, 0));
        temp->setPosition(Point(x, y+64));
        mybox.pushBack(temp);
        this->addChild(temp, 3);
    }

    TMXObjectGroup* objects2 = mytmx->getObjectGroup("player");
    //从对象层中获取对象数组
    ValueVector container2 = objects2->getObjects();
    //遍历对象
    for (auto obj : container2) {
        ValueMap values = obj.asValueMap();
        int x = values.at("x").asInt();
        int y = values.at("y").asInt();
        Sprite* temp = Sprite::create("Pushbox/player.png");
        temp->setAnchorPoint(Point(0, 0));
        temp->setPosition(Point(x, y+64));
        mon = temp;
        this->addChild(temp, 2);
    }

    TMXObjectGroup* objects3 = mytmx->getObjectGroup("goal");
    //从对象层中获取对象数组
    ValueVector container3 = objects3->getObjects();
    //遍历对象
    for (auto obj : container3) {
        ValueMap values = obj.asValueMap();
        int x = values.at("x").asInt();
        int y = values.at("y").asInt();
        Sprite* temp = Sprite::create("Pushbox/goal.png");
        temp->setAnchorPoint(Point(0, 0));
        temp->setPosition(Point(x, y+64));
        mygoal.pushBack(temp);
        this->addChild(temp, 1);
    }
}
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !Scene::init() )
    {
        return false;
    }

    visibleSize = Director::getInstance()->getVisibleSize();
    origin = Director::getInstance()->getVisibleOrigin();
	en = false;

	TMXTiledMap* tmx = TMXTiledMap::create("map.tmx");
	tmx->setPosition(visibleSize.width / 2, visibleSize.height / 2);
	tmx->setAnchorPoint(Vec2(0.5, 0.5));
	tmx->setScale(Director::getInstance()->getContentScaleFactor());
	this->addChild(tmx, 0);

	//创建一张贴图
	auto texture = Director::getInstance()->getTextureCache()->addImage("$lucia_2.png");
	//从贴图中以像素单位切割,创建关键帧
	auto frame0 = SpriteFrame::createWithTexture(texture, CC_RECT_PIXELS_TO_POINTS(Rect(0, 0, 113, 113)));
	//使用第一帧创建精灵
	player = Sprite::createWithSpriteFrame(frame0);
	player->setPosition(Vec2(origin.x + visibleSize.width / 2,
		origin.y + visibleSize.height / 2));
	addChild(player, 3);

	//hp条
	Sprite* sp0 = Sprite::create("hp.png", CC_RECT_PIXELS_TO_POINTS(Rect(0, 320, 420, 47)));
	Sprite* sp = Sprite::create("hp.png", CC_RECT_PIXELS_TO_POINTS(Rect(610, 362, 4, 16)));

	//使用hp条设置progressBar
	hp = 100;
	pT = ProgressTimer::create(sp);
	pT->setScaleX(90);
	pT->setAnchorPoint(Vec2(0, 0));
	pT->setType(ProgressTimerType::BAR);
	pT->setBarChangeRate(Point(1, 0));
	pT->setMidpoint(Point(0, 1));
	pT->setPercentage(hp);
	pT->setPosition(Vec2(origin.x + 14 * pT->getContentSize().width, origin.y + visibleSize.height - 2 * pT->getContentSize().height));
	addChild(pT, 1);
	sp0->setAnchorPoint(Vec2(0, 0));
	sp0->setPosition(Vec2(origin.x + pT->getContentSize().width, origin.y + visibleSize.height - sp0->getContentSize().height));
	addChild(sp0, 0);

	// 静态动画
	idle.reserve(1);
	idle.pushBack(frame0);

	// 攻击动画
	attack.reserve(17);
	for (int i = 0; i < 17; i++) {
		auto frame = SpriteFrame::createWithTexture(texture, CC_RECT_PIXELS_TO_POINTS(Rect(113 * i, 0, 113, 113)));
		attack.pushBack(frame);
	}

	// 可以仿照攻击动画
	// 死亡动画(帧数:22帧,高:90,宽:79)
	auto texture2 = Director::getInstance()->getTextureCache()->addImage("$lucia_dead.png");
	// Todo
	dead.reserve(22);
	for (int i = 0; i < 22; ++i) {
		auto frame= SpriteFrame::createWithTexture(texture2, CC_RECT_PIXELS_TO_POINTS(Rect(79 * i, 0, 79, 90)));
		dead.pushBack(frame);
	}

	// 运动动画(帧数:8帧,高:101,宽:68)
	auto texture3 = Director::getInstance()->getTextureCache()->addImage("$lucia_forward.png");
	// Todo
	run.reserve(8);
	for (int i = 0; i < 8; ++i) {
		auto frame = SpriteFrame::createWithTexture(texture3, CC_RECT_PIXELS_TO_POINTS(Rect(68 * i, 0, 68, 101)));
		run.pushBack(frame);
	}

	auto w = Label::createWithTTF("W", "fonts/arial.ttf", 36);
	auto wItem = MenuItemLabel::create(w, CC_CALLBACK_1(HelloWorld::moveWCallback,this));
	wItem->setPosition(100, 100);

	auto s = Label::createWithTTF("S", "fonts/arial.ttf", 36);
	auto sItem = MenuItemLabel::create(s, CC_CALLBACK_1(HelloWorld::moveSCallback, this));
	sItem->setPosition(100, 50);

	auto a = Label::createWithTTF("A", "fonts/arial.ttf", 36);
	auto aItem = MenuItemLabel::create(a, CC_CALLBACK_1(HelloWorld::moveACallback, this));
	aItem->setPosition(50, 50);

	auto d = Label::createWithTTF("D", "fonts/arial.ttf", 36);
	auto dItem = MenuItemLabel::create(d, CC_CALLBACK_1(HelloWorld::moveDCallback, this));
	dItem->setPosition(150, 50);

	auto x = Label::createWithTTF("X", "fonts/arial.ttf", 36);
	auto xItem = MenuItemLabel::create(x, CC_CALLBACK_1(HelloWorld::moveXCallback, this));
	xItem->setPosition(visibleSize.width-50, 100);

	auto y = Label::createWithTTF("Y", "fonts/arial.ttf", 36);
	auto yItem = MenuItemLabel::create(y, CC_CALLBACK_1(HelloWorld::moveYCallback, this));
	yItem->setPosition(visibleSize.width-100, 50);

	auto menu = Menu::create(wItem, sItem, aItem, dItem, xItem, yItem, NULL);
	menu->setPosition(Vec2::ZERO);
	this->addChild(menu, 1);

	dtime = 160;
	auto temp = CCString::createWithFormat("%d", dtime);
	time = Label::createWithTTF(temp->getCString(),"fonts/arial.ttf",36);
	time->setPosition(visibleSize.width / 2, visibleSize.height - 100);
	this->addChild(time,1);
	schedule(schedule_selector(HelloWorld::timeCallback), 1.0f, kRepeatForever, 1.0f);
	schedule(schedule_selector(HelloWorld::moveCallback), 3.0f, kRepeatForever, 1.0f);

	sc = 0;
	UserDefault::getInstance()->setIntegerForKey("score", sc);
	auto temp1 = CCString::createWithFormat("%d", sc);
	score = Label::createWithTTF(temp1->getCString(), "fonts/arial.ttf", 36);
	score->setPosition(visibleSize.width / 2, visibleSize.height - 50);
	this->addChild(score, 1);

	auto fac = Factory::getInstance();
	for (int i = 0; i < 10; ++i) {
		Sprite* m = fac->createMonster();
		float x1 = random(origin.x, visibleSize.width);
		float y1 = random(origin.y, visibleSize.height);
		m->setPosition(x1, y1);
		this->addChild(m, 3);
	}
    return true;
}
Example #3
0
void prep::testTMX()
{
    Size visibleSize = Director::getInstance()->getVisibleSize();

    //创建游戏
    TMXTiledMap* tmx = TMXTiledMap::create("map.tmx");
    tmx->setPosition(visibleSize.width / 2, visibleSize.height / 2);
    tmx->setAnchorPoint(Vec2(0.5, 0.5));
    tmx->setName("paopaot");
    this->addChild(tmx);

    //获取对象层
    TMXObjectGroup* objects = tmx->getObjectGroup("bubble");
    //对应对象层的对象数组
    ValueVector container = objects->getObjects();

    //遍历
    for (auto obj : container) {
        ValueMap values = obj.asValueMap();
        //获取坐标(cocos2dx坐标)
        int x = values.at("x").asInt();
        int y = values.at("y").asInt();
        auto buble = Sprite::create("bubble.png");
        buble->setPosition(Vec2(x, y + 64));
        buble->ignoreAnchorPointForPosition(true);
        tmx->addChild(buble, 2);
        prep::bubbles.pushBack(buble);
    }

    objects = tmx->getObjectGroup("wall");
    container = objects->getObjects();
    for (auto obj : container) {
        ValueMap values = obj.asValueMap();
        int x = values.at("x").asInt();
        int y = values.at("y").asInt();
        auto wall = Sprite::create("wall.png");
        wall->setPosition(Vec2(x, y + 64));
        wall->ignoreAnchorPointForPosition(true);
        tmx->addChild(wall, 1);
        prep::walls.pushBack(wall);
    }

    objects = tmx->getObjectGroup("money");
    container = objects->getObjects();
    for (auto obj : container) {
        ValueMap values = obj.asValueMap();
        int x = values.at("x").asInt();
        int y = values.at("y").asInt();
        auto goal = Sprite::create("money.png");
        goal->setPosition(Vec2(x, y + 64));
        goal->ignoreAnchorPointForPosition(true);
        tmx->addChild(goal, 1);
        prep::money.pushBack(goal);
    }

    objects = tmx->getObjectGroup("player");
    container = objects->getObjects();
    for (auto obj : container) {
        ValueMap values = obj.asValueMap();
        int x = values.at("x").asInt();
        int y = values.at("y").asInt();
        auto player = Sprite::create("player.png");
        player->setPosition(Vec2(x, y + 64));
        player->ignoreAnchorPointForPosition(true);
        player->setName("player");
        tmx->addChild(player, 1);
    }

}