void UnlockPortals::AddParticle(int count)
{
	if (IsUnlocked())
		return;

	m_numParticlesLeft -= count;
	m_curParticleTotal += count;
	if (IsUnlocked())
	{
		setSpriteFrame(m_unlockImage);
		setPhysicsBody(m_unlockCollision);
		removeAllChildrenWithCleanup(true);
		if (m_boundaryImg != "none")
		{
			auto boundary = cocos2d::Sprite::create();
			boundary->setAnchorPoint(cocos2d::Vec2(0, 0));
			boundary->setTexture(m_boundaryImg);
			boundary->setTextureRect(m_unlockRect);
			cocos2d::Texture2D::TexParams params = cocos2d::Texture2D::TexParams();
			params.magFilter = GL_LINEAR;
			params.minFilter = GL_LINEAR;
			params.wrapS = GL_REPEAT;
			params.wrapT = GL_REPEAT;
			boundary->getTexture()->setTexParameters(params);
			boundary->setCameraMask(4);
			addChild(boundary,22);
			boundary->setPosition(m_unlockRect.origin + cocos2d::Vec2(m_imgXOffset, m_imgYOffset));
		}
	}
	else
	{
		setSpriteFrame(cocos2d::SpriteFrameCache::getInstance()->spriteFrameByName(m_particleBaseImg + std::to_string(m_curParticleTotal) + m_particleBaseImgExt));
	}
}
Exemple #2
0
void FDirSprite::chgDir(float dx, float dy)
{
    const cocos2d::Vec2& pos = getPosition();
    bool left = (m_curDir == FDIR_LEFT_UP || m_curDir == FDIR_LEFT_DOWN);
    bool up = (m_curDir == FDIR_RIGHT_UP || m_curDir == FDIR_LEFT_UP);
    
    if (pos.x > dx) {
        left = true;
    }
    else if (pos.x < dx) {
        left = false;
    }
    
    if (pos.y > dy) {
        up = false;
    }
    else if (pos.y < dy) {
        up = true;
    }
    
    int dir = left ? (up ? FDIR_LEFT_UP : FDIR_LEFT_DOWN) : (up ? FDIR_RIGHT_UP : FDIR_RIGHT_DOWN);
    
    if (dir != m_curDir) {
        m_curDir = dir;
        
        setScaleX(left ? -1 : 1);
        setSpriteFrame(m_lstFrames[up ? (m_frameNums + m_curIndex) : m_curIndex]);
    }
}
Exemple #3
0
void SGSHero::updataSprite()
{
  faceTo(__direction);
  setColor(Color3B::WHITE);

  if (!__active) {
    stopAllActions();
    SpriteFrame* frame;
    switch (__direction)
    {
    case HERO_DIRECTION_SOUTH:
      frame = __sprite_frames.at(0);
      break;
    case HERO_DIRECTION_NORTH:
      frame = __sprite_frames.at(2);
      break;
    case HERO_DIRECTION_EAST:
    case HERO_DIRECTION_WEST:
      frame = __sprite_frames.at(4);
      break;
    default:
      break;
    }
    setSpriteFrame(frame);
    setColor(Color3B::GRAY);
  } 
}
Exemple #4
0
bool Card::initCard(Vec2 pos, Node *parent) {
	this->setPosition(pos);

	auto rootNode = CSLoader::createNode(CARD_CSB_NAME);
	this->addChild(rootNode);
	auto action = CSLoader::createTimeline(CARD_CSB_NAME);
	rootNode->runAction(action);
	action->gotoFrameAndPlay(0);

	auto image = rootNode->getChildByName<Sprite *>(CARDIMAGE);
	SpriteFrameCache::getInstance()->addSpriteFramesWithFile(CARD_PLIST_NAME);
	auto cardID = (int)(CCRANDOM_0_1() * 2);
	auto cardName = CsvUtil::getInstance()->getValueForCard(cardID, CARD_CSV_NAME).asString();
	image->setSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(StringUtils::format("%s.png", cardName.c_str())));

	auto button = rootNode->getChildByName<Button *>(CARDTOUCH_BUTTON);
	button->addClickEventListener([=](Ref *ref) {
		if (DATAOPERATOR->getEffectEnable())
			AUDIOENGINE->playEffect(CLICKEFFECT_NAME);
		this->removeFromParent();
		auto click = ParticleSystemQuad::create(CARD_PARTICLE_NAME);
		click->setPosition(pos);
		parent->addChild(click);

		NotificationCenter::getInstance()->postNotification(cardName);
	});

	return true;
}
Exemple #5
0
bool FDirSprite::onIdle(float dt)
{
    bool ischg = false;
    m_lastTime -= dt;
    
    while (m_lastTime <= 0) {
        m_curIndex++;
        if (m_curIndex >= m_frameNums) {
            m_curIndex = 0;
        }
        
        m_lastTime += m_frameTime;
        
        ischg = true;
    }
    
    if (ischg) {
        bool up = (m_curDir == FDIR_RIGHT_UP || m_curDir == FDIR_LEFT_UP);
        
        //CCLOG("%d", up ? (m_frameNums + m_curIndex) : m_curIndex);
        
        setSpriteFrame(m_lstFrames[up ? (m_frameNums + m_curIndex) : m_curIndex]);
    }
    
    return false;
}
Exemple #6
0
bool AniSprite::onIdle(float dt)
{
    if (m_anitime > 0 && m_anitime - dt <= 0) {
        return true;
    }
    
    m_anitime -= dt;
    
    bool ischg = false;
    m_lastTime -= dt;
    
    while (m_lastTime <= 0) {
        m_curIndex++;
        if (m_curIndex >= m_lstFrames.size()) {
            if (!m_isloop) {
                return true;
            }
            
            m_curIndex = 0;
        }
        
        m_lastTime += m_frameTime;
        
        ischg = true;
    }
    
    if (ischg) {
        setSpriteFrame(m_lstFrames[m_curIndex]);
    }
    
    return false;
}
void MonsterTortoise::tileJumpHandle(TileSprite * pTile)
{
	//状态校验
	CCASSERT(pTile != nullptr, "jump tile can't be null");
	if (getMonsterStatus() == MonsterStatus_Invalid)
		return;
	if (updateFixStatus == UpdateTortoise_TorKillR || updateFixStatus == UpdateTortoise_TorKillL)
		return;

	Vec2 mePos = getPosition();
	Vec2 tilePos = pTile->getPosition();

	//响应弹起土块碰撞
	stopAllActions();
	Action * action = nullptr;
	if (mePos.x < tilePos.x)
		action = Sequence::create(
		JumpBy::create(1.0f, Vec2(-100.0f, 10.0f), 200.0f, 1),
		//CallFunc::create(CC_CALLBACK_0(MonsterTortoise::resetVelocityX, this)),
		nullptr);
	else
		action = Sequence::create(JumpBy::create(1.0f, Vec2(100.0f, 10.0f), 200.0f, 1),
		//CallFunc::create(CC_CALLBACK_0(MonsterTortoise::resetVelocityX, this)),
		nullptr);
	runAction(action);

	getBody()->SetLinearVelocity(b2Vec2(0.0f, 0.0f));
	m_bIsDefend = true;
	m_bIsRoll = false;

	SpriteFrame * frame = SpriteFrameCache::getInstance()->getSpriteFrameByName(
		"monster/tortoise_defend/tortoise_defend1.png");
	setSpriteFrame(frame);
	updateFixStatus = UpdateTortoise_Defend;
}
Exemple #8
0
void HeroSprite::jump() {
    Vect pulse;

	if (mState == STATE_JUMP2)
		return;
	else if (mState == STATE_IDLE || mState == STATE_DEAD)
		return;
    else if (mState == STATE_JUMP1) {
        mState = STATE_JUMP2;
        pulse = Vect(0, 6000);
    } else {
        mState = STATE_JUMP1;
        pulse = Vect(0, 8000);
    }

	setSpriteFrame("heroJump_0001.png");

    stopAllActions();

    getPhysicsBody()->applyImpulse(pulse);

	auto delay1 = DelayTime::create(0.06f);
	auto callback1 = CallFunc::create([&]() {
			getPhysicsBody()->applyForce(Vect(0, 1500));
			});
	auto delay2 = DelayTime::create(0.8f);
	auto callback2 = CallFunc::create([&]() {
			getPhysicsBody()->resetForces();
			});
	auto seq = Sequence::create(delay1, callback1, delay2, callback2, nullptr);
	seq->setTag(10);
	runAction(seq);

	getPhysicsBody()->setContactTestBitmask((1<<0) | (1<<1));
}
void TileSprite::setInvalid()
{
	this->stopAllActions();
	m_eTileType = TSType_Invalid;

	SpriteFrame * frame =  SpriteFrameCache::getInstance()->getSpriteFrameByName("tile/static_tile.png");
	setSpriteFrame(frame);
}
bool AnimationFrame::initWithSpriteFrame(SpriteFrame* spriteFrame, float delayUnits, const ValueMap& userInfo)
{
    setSpriteFrame(spriteFrame);
    setDelayUnits(delayUnits);
    setUserInfo(userInfo);

    return true;
}
Exemple #11
0
bool CCAnimationFrame::initWithSpriteFrame(CCSpriteFrame* spriteFrame, float delayUnits, CCDictionary* userInfo)
{
    setSpriteFrame(spriteFrame);
    setDelayUnits(delayUnits);
    setUserInfo(userInfo);

    return true;
}
void WolfWithPlayerState::setGraphics(Wolf* wolf)
{
    auto sprite = dynamic_cast<LHSprite*>(wolf->getChildNodeWithName(wolfDefs::SPRITE_NAME));
    
    if (sprite)
    {
        if (wolf->containsTag("WORLD_5"))
        {
            sprite->setSpriteFrame("lupo_GROTTA_w5.png");
        }
        else
        {
            sprite->setSpriteFrame("lupo_Pan.png");
        }
    }
    
}
Exemple #13
0
void Sprite::setSpriteFrame(const std::string &spriteFrameName)
{
    SpriteFrameCache *cache = SpriteFrameCache::getInstance();
    SpriteFrame *spriteFrame = cache->getSpriteFrameByName(spriteFrameName);

    CCASSERT(spriteFrame, "Invalid spriteFrameName");

    setSpriteFrame(spriteFrame);
}
Exemple #14
0
void BlockSprite::setType(int type)
{
    _type = type;
    if (type >= HEXGAME::TYPE_FIRST && type <= HEXGAME::TYPE_LAST)
    {
        std::string name = StringUtils::format("block_%d.png", type);
        setSpriteFrame(DTool::setSpriteFrameWithName(name));
    }
}
Exemple #15
0
bool Sprite::initWithSpriteFrame(SpriteFrame *spriteFrame)
{
    CCASSERT(spriteFrame != nullptr, "");

    bool bRet = initWithTexture(spriteFrame->getTexture(), spriteFrame->getRect());
    setSpriteFrame(spriteFrame);

    return bRet;
}
Exemple #16
0
void Sprite::setSpriteFrame(const std::string &spriteFrameName)
{
    SpriteFrameCache *cache = SpriteFrameCache::getInstance();
    SpriteFrame *spriteFrame = cache->getSpriteFrameByName(spriteFrameName);

    CCASSERT(spriteFrame, std::string("Invalid spriteFrameName :").append(spriteFrameName).c_str());

    setSpriteFrame(spriteFrame);
}
Exemple #17
0
void Icon::setType(const IconType &type) {
	if (m_type == type) return;
	m_type = type;
	switch (type)
	{
	case HorizontalIcon:
		setSpriteFrame(StringUtils::format("icon%d_h.png", m_imageIndex));
		m_canMark = false;
		m_score *= 3;
		break;
	case VerticalIcon:
		setSpriteFrame(StringUtils::format("icon%d_z.png", m_imageIndex));
		m_canMark = false;
		m_score *= 3;
		break;
	default:
		break;
	}
}
void UIParserChangeTextureDelegate::constraintWithParent(const pugi::xml_node& node, const pugi::xml_node& prev_node, cocos2d::Node* parent, cocos2d::Node* recent)
{
    if (parent)
    {
        std::string src;
        std::string atlas;
        std::string frame;
        cocos2d::Node* target = nullptr;
        
        for (auto it = node.attributes_begin(); it != node.attributes_end(); it++)
        {
            if (strcmp(it->name(), "src") == 0)
            {
                src = it->value();
            }
            else if (strcmp(it->name(), "atlas") == 0)
            {
                atlas = it->value();
            }
            else if (strcmp(it->name(), "frame") == 0)
            {
                frame = it->value();
            }
            else if (strcmp(it->name(), "target") == 0)
            {
                std::vector<std::string> ids;
                ui_parser_utils::split(it->value(), ':', ids);
                
                target = parent;
                for (int i = 0; i < ids.size(); i++)
                {
                    if (ids[i].compare("this") != 0 && target)
                    {
                        target = target->getChildByName(ids[i]);
                    }
                }
            }
        }
        
        auto imageview = dynamic_cast<CustomUIImageView*>(target);
        if (imageview)
        {
            if (!src.empty())
            {
                imageview->loadTexture(src);
            }
            else if(!atlas.empty() && !frame.empty())
            {
                imageview->setSpriteFrame(atlas_cache::getSpriteFrame(atlas, frame));
            }
        }
    }
}
Exemple #19
0
void CheckBox::setSpriteWithFile(const std::string& filename)
{
    CCAssert(filename.size() > 0, "file is not exists");
    
    auto frame = SpriteFrameCache::getInstance()->getSpriteFrameByName(filename);
    if (frame)
    {
        setSpriteFrame(filename);
    } else
    {
        setTexture(filename);
    }
}
Exemple #20
0
bool Sprite::initWithSpriteFrame(SpriteFrame *spriteFrame)
{
    CCASSERT(spriteFrame != nullptr, "spriteFrame can't be nullptr!");

    if (spriteFrame == nullptr) {
        return false;
    }

    bool bRet = initWithTexture(spriteFrame->getTexture(), spriteFrame->getRect());
    setSpriteFrame(spriteFrame);

    return bRet;
}
Exemple #21
0
bool ButtonB::onTouchBegan(Touch* touch, Event* event){
	Point location = touch->getLocation();
	//log("%f, %f", touch->getLocation().x, touch->getLocation().y);

	float distanceSQ = ccpDistanceSQ(location, this->getPosition());
	if (distanceSQ <= 32 * 32){
		setSpriteFrame("*****@*****.**");
		m_hero->jump();
	//	log("B");
		return true;
	}
	return false;
}
Exemple #22
0
void Sprite::setDisplayFrameWithAnimationName(const std::string& animationName, ssize_t frameIndex)
{
    CCASSERT(animationName.size()>0, "CCSprite#setDisplayFrameWithAnimationName. animationName must not be nullptr");

    Animation *a = AnimationCache::getInstance()->getAnimation(animationName);

    CCASSERT(a, "CCSprite#setDisplayFrameWithAnimationName: Frame not found");

    AnimationFrame* frame = a->getFrames().at(frameIndex);

    CCASSERT(frame, "CCSprite#setDisplayFrame. Invalid frame");

    setSpriteFrame(frame->getSpriteFrame());
}
Exemple #23
0
void Sprite::setSpriteFrame(const std::string &spriteFrameName)
{
    CCASSERT(!spriteFrameName.empty(), "Sprite::setSpriteFrame-->spriteFrameName should not be empty!");
    if (spriteFrameName.empty())
    {
        return;
    }

    SpriteFrameCache *cache = SpriteFrameCache::getInstance();
    SpriteFrame *spriteFrame = cache->getSpriteFrameByName(spriteFrameName);

    CCASSERT(spriteFrame, std::string("Invalid spriteFrameName :").append(spriteFrameName).c_str());

    setSpriteFrame(spriteFrame);
}
void MonsterTortoise::update(float dt)
{
	//处理当前框架状态
	if (updateFixStatus != UpdateTortoise_Invalid)
	{
		if (updateFixStatus == UpdateTortoise_TorKillL || updateFixStatus == UpdateTortoise_TorKillR)
		{
			//执行杀死动作
			Vec2 pos = getPosition();
			Spawn * action = nullptr;
			float fPosLevel = (pos.y + 100) / 100.0f;	//高度等级

			//设置动作、显示
			if (updateFixStatus == UpdateTortoise_TorKillL)
			{
				action = Spawn::create(JumpTo::create(fPosLevel * 1.0f, Vec2(pos.x + 400 * fPosLevel, -100.0f), 800.0f, 1),
					RotateBy::create(fPosLevel * 1.0f, 720 * fPosLevel), nullptr);
			}
			else if (updateFixStatus == UpdateTortoise_TorKillR)
			{
				action = Spawn::create(JumpTo::create(fPosLevel * 1.0f, Vec2(pos.x - 400 * fPosLevel, -100.0f), 800.0f, 1),
					RotateBy::create(fPosLevel * 1.0f, -720 * fPosLevel), nullptr);
			}

			SpriteFrame * frame = SpriteFrameCache::getInstance()->getSpriteFrameByName(
				"monster/tortoise_defend/tortoise_defend1.png");
			setSpriteFrame(frame);

			//复位各信息
			stopAllActions();
			getBody()->SetLinearVelocity(b2Vec2(0.0f, 0.0f));
			runAction(Sequence::create(action, CallFunc::create(CC_CALLBACK_0(MonsterTortoise::removeSelf, this)), nullptr));
		}

		changeFixture();
		updateFixStatus = UpdateTortoise_Invalid;
	}

	if (++m_ntimes >= 2)
	{
		checkPosSchedule();
		m_ntimes = 0;
	}
}
UnlockPortals::UnlockPortals(std::string lockImg, std::string unlockImg, std::string particleBaseImg, std::string particleBaseImgExt, int numRedParticlesNeeded, std::string boundaryImg, int imgXOffset, int imgYoffset)
	:	m_numParticlesLeft(numRedParticlesNeeded),
		m_curParticleTotal(0),
		m_unlockImage(unlockImg),
		m_lockImage(lockImg),
		m_particleBaseImg(particleBaseImg),
		m_particleBaseImgExt(particleBaseImgExt),
		m_lockCollision(nullptr),
		m_unlockCollision(nullptr),
		m_boundaryImg(boundaryImg),
		m_imgXOffset(imgXOffset),
		m_imgYOffset(imgYoffset)
{
	m_unlockRect = cocos2d::Rect(0,0,0,0);
	initWithSpriteFrameName(lockImg);
	setSpriteFrame(lockImg);
	
	this->setCameraMask(4);
}
Exemple #26
0
void TileScript::LoadTile(tinyxml2::XMLElement * xmlElement)
{
	//////////////////////////////////////////////////////////////////////////
	//Load and set the tile data.
	auto tileDataID = xmlElement->IntAttribute("TileDataID");
	const auto resourceLoader = SingletonContainer::getInstance()->get<ResourceLoader>();
	auto tileData = resourceLoader->getTileData(tileDataID);
	assert(tileData && "TileScript::setTileData() with nullptr.");

	auto ownerActor = m_OwnerActor.lock();
	//#TODO: This only shows the first first frame of the animation. Update the code and resources to show the whole animation.
	auto sceneNode = static_cast<cocos2d::Sprite*>(pimpl->m_RenderComponent->getSceneNode());
	sceneNode->setSpriteFrame(tileData->getAnimation()->getFrames().at(0)->getSpriteFrame());

	pimpl->m_TileData = std::move(tileData);

	//////////////////////////////////////////////////////////////////////////
	//#TODO: Load more data, such as the hp, level and so on, from the xml.
}
bool Character::setCurrentFrame(int animationId, int animationIndex){
	int frameIndex = this->_animationFrameSequences[animationId][animationIndex];
	if (this->_frames[frameIndex] != nullptr){
		this->_currentFrameIndex = frameIndex;
		this->_currentFrame = this->_frames[frameIndex];
		this->_currentAnimationId = animationId;
		this->_currentAnimationIndex = animationIndex;
		this->_animationCounter = 0;

		setSpriteFrame(this->_currentFrame);
		
		setAnchorPoint(Point(_framesAnchorPoint[_currentFrameIndex]));
		/*if (hpbar_visible){
		hpbar->setPosition(sprite->getAnchorPointInPoints().x + HPBAR_OFFSET_X,
		sprite->getAnchorPointInPoints().y + HPBAR_OFFSET_Y);
		}*/

		return true;
	}
	else{
		return false;
	}
}
Exemple #28
0
void UnitScript::loadUnit(tinyxml2::XMLElement * xmlElement)
{
	//////////////////////////////////////////////////////////////////////////
	//Load and set the unit data.
	auto unitDataID = xmlElement->IntAttribute("UnitDataID");
	const auto resourceLoader = SingletonContainer::getInstance()->get<ResourceLoader>();
	auto unitData = resourceLoader->getUnitData(unitDataID);
	assert(unitData && "UnitScript::setUnitData() with nullptr.");

	auto ownerActor = m_OwnerActor.lock();
	//#TODO: This only shows the first frame of the animation. Update the code to show the whole animation.
	auto sceneNode = static_cast<cocos2d::Sprite*>(pimpl->m_RenderComponent->getSceneNode());
	sceneNode->setSpriteFrame(unitData->getAnimation()->getFrames().at(0)->getSpriteFrame());

	//Scale the sprite so that it meets the real game grid size.
	pimpl->m_TransformComponent->setScaleToSize(resourceLoader->getDesignGridSize());

	pimpl->m_UnitData = std::move(unitData);

	//////////////////////////////////////////////////////////////////////////
	//#TODO: Load more data, such as the hp, level and so on, from the xml.
	pimpl->m_State = utilities::createUnitState(UnitStateTypeCode::Idle);
}
Exemple #29
0
void Wall::_refreshWall()
{
    static const std::string wall_list[] = {
        "rWall_0000",
        "rWall_0001",
        "rWall_0002",
        "rWall_0003",
    };
    std::string wall_name;
    if (_is_top_wall_exist)
    {
        if (_is_right_wall_exist)
        {
            wall_name = wall_list[0];
            this->setAnchorPoint(Vec2(0.5f, 0.5f));
        }
        else
        {
            wall_name = wall_list[2];
            this->setAnchorPoint(Vec2(0.6f, 0.5f));
        }
    }
    else if (_is_right_wall_exist)
    {
        wall_name = wall_list[1];
        this->setAnchorPoint(Vec2(0.4f, 0.5f));
    }
    else
    {
        wall_name = wall_list[3];
        this->setAnchorPoint(Vec2(0.44f, 0.48f));
    }
    
    auto frame = SpriteFrameCache::getInstance()->getSpriteFrameByName(wall_name);
    setSpriteFrame(frame);

}
void BWTileSprite::updateWithIndex(int time_sequence_index)
{
	setSpriteFrame((*(pimpl->m_tile_frames))[time_sequence_index]);
}