コード例 #1
0
bool BuildingSprite::init(int index)
{
    if ( !Sprite::init() ) {
        return false;
    }
    
    _isSelected = false;
    _isTouched = false;
    _canTouched = true;
    
    _index = index;
    
    loadData(index);
    
    showUI();
    showUpgradePro();
    showBubble();
    
    addTouch();
    scheduleUpdate();
    
    // 设置名称
    this->setName("BuildingSprite");
    
    return true;
}
コード例 #2
0
ファイル: PauseLayer.cpp プロジェクト: evamango/democpp36
bool PauseLayer::init()
{
	if (!LayerColor::init())
		return false;

	initWithColor(Color4B(162, 162, 162, 128));

	addUI();
	addTouch();
	return true;
}
コード例 #3
0
ファイル: SDLTouch.hpp プロジェクト: RinWorld/minko
 inline
 void updateTouch(int identifier, float x, float y)
 {
     if (_touches.find(identifier) == _touches.end())
     {
         addTouch(identifier, x, y);
     }
     else
     {
         _touches[identifier]->x(x);
         _touches[identifier]->y(y);
     }
 }
コード例 #4
0
bool PowerPlantTower::init()
{
	if (!Sprite::init()) //如果忘记了这句话则会在runApplication报错
	{
		return false;
	}

	
	setTowerType(5);
	setIsSelected(false);
	setGrade(0);//开始没有等级
	setPower(0);//塔消耗电力
	setMove(false);//开始不处于移动状态
	setIsPowerEnough(true);//开始状态电力足够
	setIsPowerConsumption(false);//该塔不耗电
	setTowerSize(Size(3, 3));//占地面积3*3

	towerSprite = Sprite::create("towerItem/Item6.png");
	towerSprite->setScale(0.4);
	addChild(towerSprite, 5);

	//血量条背景图片
	towerHpSprite = Sprite::create("manaBarBg.png");
	towerHpSprite->setPosition(towerSprite->getPosition()+Point(0,50));
	towerHpSprite->setScale(0.3);
	addChild(towerHpSprite, 10);
	//炮塔血量
	hp = 4;
	//炮塔血量进度条
	towerHp = ProgressTimer::create(Sprite::create("soldierProduceTimeBar.png")); //参数是一个图片sprite
	towerHp->setScaleX(2);
	towerHp->setScaleY(5.2);
	towerHp->setType(ProgressTimer::Type::BAR);
	towerHp->setMidpoint(Point(0, 0.5f));
	towerHp->setBarChangeRate(Point(1, 0));
	towerHp->setPercentage(100);
	towerHp->setPosition(Point(towerHpSprite->getContentSize().width / 2, towerHpSprite->getContentSize().height / 3 * 2 - 10));
	towerHpSprite->addChild(towerHp, 5);
	//初始化不可见
	towerHp->setVisible(false);
	towerHpSprite->setVisible(false);

	addTouch();//添加触摸事件

	return true;
}
コード例 #5
0
ファイル: GameLayer.cpp プロジェクト: mengtest/git
// on "init" you need to initialize your instance
bool GameLayer::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !Layer::init())
    {
        return false;
    }
    addTouch();
    loadUI();
    addPig();
    objManager = ObjManager::getInstance();
    test();
    addTopLayer();
    this->schedule( schedule_selector(GameLayer::generateUpdate), speep );
    return true;
}
コード例 #6
0
bool ForetellingTower::init()
{
	if (!Sprite::init()) //如果忘记了这句话则会在runApplication报错
	{
		return false;
	}

	setTowerType(6);
	setIsSelected(false);
	setGrade(0);//开始没有等级
	setPower(60);//塔消耗电力
	setMove(false);//开始不处于移动状态
	setIsPowerEnough(true);//开始状态电力足够
	setIsPowerConsumption(false);//该塔不耗电
	setTowerSize(Size(3, 3));//占地面积3*3

	towerSprite = Sprite::create("towerItem/Item7.png");
	towerSprite->setScale(0.4);
	addChild(towerSprite, 5);

	addTouch();//添加触摸事件

	return true;
}
コード例 #7
0
bool ArtilleryTower::init()
{
	if (!Sprite::init()) //如果忘记了这句话则会在runApplication报错
	{
		return false;
	}
	
	setScope(300);
	setAttack(2);
	setRate(2);
	setIsSelected(false);
	setTowerType(1);
	setGrade(0);//开始没有等级
	setPower(0);//塔消耗电力
	setMove(false);//开始不处于移动状态
	setIsPowerEnough(true);//开始状态电力足够
	setIsPowerConsumption(false);//该塔耗电

	basePlate = Sprite::createWithSpriteFrameName("fort1.png"); //塔的基座
	addChild(basePlate,2);
	//log("111111111111111");
	rotateArtillery = Sprite::createWithSpriteFrameName("fort2.png");  //塔的弓箭
	rotateArtillery->setPosition(0, 0);
	rotateArtillery->setRotation(270);
	addChild(rotateArtillery,3);

	//这个不能少,虽然不显示,这是因为这个炮塔比较特殊,是由2个sprite拼接而成的
	towerSprite = Sprite::create("towerItem/Item2.png");
	addChild(towerSprite);
	towerSprite->setVisible(false);

	gradeSprite = Sprite::create("level1.png");
	gradeSprite->setScale(2);
	gradeSprite->setAnchorPoint(Point(0, 0));
	gradeSprite->setPosition(this->getPosition().x + 60, -towerSprite->getBoundingBox().size.height / 2-10);
	gradeSprite->setOpacity(0);//开始让其不可见
	addChild(gradeSprite, 6);

	//沉默图片
	noAttackSprite = Sprite::create("noSpeak.png");
	noAttackSprite->setScale(0.9);
	noAttackSprite->setAnchorPoint(Point(0.5, 0));
	noAttackSprite->setPosition(towerSprite->getBoundingBox().size.width / 2 - 70, towerSprite->getBoundingBox().size.height - 60);
	addChild(noAttackSprite, 8);
	noAttackSprite->setVisible(false);

	//血量条背景图片
	towerHpSprite = Sprite::create("manaBarBg.png");
	towerHpSprite->setPosition(towerSprite->getPosition() + Point(0, 100));
	towerHpSprite->setScale(0.8);
	addChild(towerHpSprite, 10);
	//炮塔血量
	hp = 2;
	maxHp = 2;
	//炮塔血量进度条
	towerHp = ProgressTimer::create(Sprite::create("soldierProduceTimeBar.png")); //参数是一个图片sprite
	towerHp->setScaleX(2);
	towerHp->setScaleY(5.2);
	towerHp->setType(ProgressTimer::Type::BAR);
	towerHp->setMidpoint(Point(0, 0.5f));
	towerHp->setBarChangeRate(Point(1, 0));
	towerHp->setPercentage(100);
	towerHp->setPosition(Point(towerHpSprite->getContentSize().width / 2, towerHpSprite->getContentSize().height / 3 * 2 - 10));
	towerHpSprite->addChild(towerHp, 5);
	//初始化不可见
	towerHp->setVisible(false);
	towerHpSprite->setVisible(false);

    schedule(schedule_selector(ArtilleryTower::rotateAndShoot), 0.5f); //别忘了调用函数,时间间隔越小,炮塔旋转越平滑
	schedule(schedule_selector(ArtilleryTower::shoot), 2.5f);
	
	addTouch();

	return true;
}
コード例 #8
0
bool AntiVisibleTower::init()
{
	if (!Sprite::init()) //如果忘记了这句话则会在runApplication报错
	{
		return false;
	}
	
	instance = GameManager::getInstance();   

	setScope(300);
	setAttack(0);
	setRate(5);//4秒钟开火一次
	setTowerType(3);
	setIsSelected(false);
	setGrade(0);//开始没有等级
	setPower(40);//塔消耗电力
	setMove(false);//开始不处于移动状态
	setIsPowerEnough(true);//开始状态电力足够
	setIsPowerConsumption(true);//该塔耗电
	nearestEnemy = nullptr;

	towerSprite = Sprite::create("towerItem/Item99.png");
	addChild(towerSprite, 5);

	gradeSprite = Sprite::create("level1.png");
	gradeSprite->setAnchorPoint(Point(0, 0));
	gradeSprite->setPosition(this->getPosition().x + 10, -towerSprite->getBoundingBox().size.height / 2);
	gradeSprite->setOpacity(0);//开始让其不可见
	addChild(gradeSprite, 6);

	noPowerSprite = Sprite::create("noPower.png");
	noPowerSprite->setAnchorPoint(Point(0.5, 0));
	noPowerSprite->setScale(1.4);
	noPowerSprite->setPosition(towerSprite->getBoundingBox().size.width / 2 - 70, towerSprite->getBoundingBox().size.height - 40);
	noPowerSprite->setVisible(false);
	addChild(noPowerSprite, 7);

	//血量条背景图片
	towerHpSprite = Sprite::create("manaBarBg.png");
	towerHpSprite->setPosition(noPowerSprite->getPosition()-Point(0,25));
	towerHpSprite->setScale(0.8);
	addChild(towerHpSprite, 10);
	//炮塔血量
	hp = 4;
	//炮塔血量进度条
	towerHp = ProgressTimer::create(Sprite::create("soldierProduceTimeBar.png")); //参数是一个图片sprite
	towerHp->setScaleX(2);
	towerHp->setScaleY(5.2);
	towerHp->setType(ProgressTimer::Type::BAR);
	towerHp->setMidpoint(Point(0, 0.5f));
	towerHp->setBarChangeRate(Point(1, 0));
	towerHp->setPercentage(100);
	towerHp->setPosition(Point(towerHpSprite->getContentSize().width / 2, towerHpSprite->getContentSize().height / 3 * 2 - 10));
	towerHpSprite->addChild(towerHp, 5);
	//初始化不可见
	towerHp->setVisible(false);
	towerHpSprite->setVisible(false);

	schedule(schedule_selector(AntiVisibleTower::shoot), 0.1f);
	schedule(schedule_selector(AntiVisibleTower::checkNearestEnemy), 0.2);
	addTouch();//添加触摸事件 

	return true;
}
コード例 #9
0
bool Recognizer::onTouchBegan(cocos2d::Touch *pTouch) {
	return addTouch(pTouch);
}
コード例 #10
0
bool TrainingTower::init()
{
	if (!Sprite::init()) //如果忘记了这句话则会在runApplication报错
	{
		return false;
	}

	instance = GameManager::getInstance();

	soldierToProduceCount = 0;
	isTrainingSpriteSelected = false;
	instance->totalSoldierNum = 0;

	setTowerType(7);
	setIsSelected(false);
	setGrade(0);//开始没有等级
	setPower(40);//塔消耗电力
	setMove(false);//开始不处于移动状态
	setIsPowerEnough(true);//开始状态电力足够
	setIsPowerConsumption(false);//该塔不耗电

	towerSprite = Sprite::create("towerItem/Item8.png");
	towerSprite->setScale(0.4);
	addChild(towerSprite, 5);

	trainingSprite = Sprite::create("traingButton.png"); //训练button,开始不显示,点击该建筑才显示
	trainingSprite->setScale(0.25);
	auto sb = ScaleBy::create(1, 1.2);
	auto seq = Sequence::create(sb, sb->reverse(), NULL);
	auto rp = RepeatForever::create(seq);
	trainingSprite->runAction(rp);
	trainingSprite->setPosition(towerSprite->getBoundingBox().size.width-10,0);
	addChild(trainingSprite, 5);
	trainingSprite->setVisible(false);

	produceTime = 0;//初始化生产时间为0
	produceTimeBarBg = Sprite::create("manaBarBg.png");
	produceTimeBarBg->setScale(0.3); //0.45
	produceTimeBarBg->setPosition(towerSprite->getPosition() + Point(0, 65));
	addChild(produceTimeBarBg, 11);

	//待生产士兵的数目
	soldierNum = Label::createWithTTF("", "b.ttf", 10);
	soldierNum->setPosition(produceTimeBarBg->getPosition());
	addChild(soldierNum, 12);


	//生产时间条初始化
	timeBar = ProgressTimer::create(Sprite::create("time.png"));
	timeBar->setScaleX(0.3);
	timeBar->setScaleY(2.2);
	timeBar->setType(ProgressTimer::Type::BAR);
	timeBar->setMidpoint(Point(0, 0.5));
	timeBar->setBarChangeRate(Point(1, 0));
	timeBar->setPercentage(produceTime);
	timeBar->setAnchorPoint(Point(0, 0.5));
	timeBar->setPosition(produceTimeBarBg->getPosition() - Point(produceTimeBarBg->getBoundingBox().size.width / 2 - 5, 0));
	addChild(timeBar, 10);

	//生产时间属性条不可见
	timeBar->setVisible(false);
	produceTimeBarBg->setVisible(false);
	soldierNum->setVisible(false);

	//血量条背景图片
	towerHpSprite = Sprite::create("manaBarBg.png");
	towerHpSprite->setPosition(towerSprite->getPosition() + Point(0, 50));
	towerHpSprite->setScale(0.3);
	addChild(towerHpSprite, 10);
	//炮塔血量
	hp = 4;
	//炮塔血量进度条
	towerHp = ProgressTimer::create(Sprite::create("soldierProduceTimeBar.png")); //参数是一个图片sprite
	towerHp->setScaleX(2);
	towerHp->setScaleY(5.2);
	towerHp->setType(ProgressTimer::Type::BAR);
	towerHp->setMidpoint(Point(0, 0.5f));
	towerHp->setBarChangeRate(Point(1, 0));
	towerHp->setPercentage(100);
	towerHp->setPosition(Point(towerHpSprite->getContentSize().width / 2, towerHpSprite->getContentSize().height / 3 * 2 - 10));
	towerHpSprite->addChild(towerHp, 5);
	//初始化不可见
	towerHp->setVisible(false);
	towerHpSprite->setVisible(false);

	addTouch();//添加触摸事件

	listener->onTouchBegan = [=](Touch* pTouch, Event* pEvent){
		
		
		auto target = static_cast<Sprite*>(pEvent->getCurrentTarget());
		
		Point locationInNode = target->convertTouchToNodeSpace(pTouch);
		
		Size size = target->getContentSize();
		Rect rect = Rect(0, 0, size.width, size.height);
	
		//log("rect size %f %f", size.width, size.height);
		if (rect.containsPoint(locationInNode)) 
		{
			timeBar->setVisible(true);
			produceTimeBarBg->setVisible(true);
			trainingSprite->setVisible(true);
			soldierNum->setVisible(true);

			towerHp->setVisible(true);
			towerHpSprite->setVisible(true);
			return true;
		}
		else if (!isTrainingSpriteSelected)//这样就可以实现点击其他地方隐藏属性条的功能了
		{
			timeBar->setVisible(false);
			produceTimeBarBg->setVisible(false);
			trainingSprite->setVisible(false);
			soldierNum->setVisible(false);
			towerHp->setVisible(false);
			towerHpSprite->setVisible(false);
		}
		
		return false;
	};

	

	trainingButtonListener = EventListenerTouchOneByOne::create();
	trainingButtonListener->onTouchBegan = [=](Touch* pTouch, Event* pEvent){ //这个是特殊声明 用来检测训练士兵按钮的点击,这种用法就是在要点击的图片上加listener

		auto target = static_cast<Sprite*>(pEvent->getCurrentTarget());

		Point locationInNode = target->convertTouchToNodeSpace(pTouch);

		Size size = target->getContentSize();
		Rect rect = Rect(0, 0, size.width, size.height);

		//log("rect size %f %f", size.width, size.height);
		if (rect.containsPoint(locationInNode) && trainingSprite->isVisible() && instance->totalSoldierNum<MAX_SOLDIER_NUM )
		{
			if (instance->getMoney() >= 100)
			{
				isTrainingSpriteSelected = true;
				soldierToProduceCount++;
				instance->totalSoldierNum++;//士兵总数最多10个
				char c[10];
				sprintf(c, "%d", soldierToProduceCount);
				std::string str = c;
				soldierNum->setString(str);

				//金钱减少
				instance->setMoney(instance->getMoney() - 100);
				char c1[10];
				sprintf(c1, "%d", instance->getMoney());
				std::string str1 = c1;
				instance->moneyLabel->setString(str1);
			}
			else //金钱不足
			{
				auto sb = ScaleBy::create(0.3, 1.2);
				auto seq = Sequence::create(sb, sb->reverse(), NULL);
				instance->moneyLabel->runAction(seq);
			}

			return true;
		}
		else
		{
			isTrainingSpriteSelected = false;
			trainingSprite->setVisible(false);
		}
		
		return false;
	};
	_eventDispatcher->addEventListenerWithSceneGraphPriority(trainingButtonListener, this->trainingSprite);

	this->schedule(schedule_selector(TrainingTower::produceSoldier), 0.1);

	return true;
}
コード例 #11
0
// this is where new touches are born
void TouchTracker::findTouches()
{
	for(int i=0; i<mNumKeys; ++i)
	{
		float z = mKeyStates[i].zOut;	
		bool threshTest = (z > mOnThreshold);
		if(threshTest)
		{			
			float kdt = mKeyStates[i].dtOut;
			float kdz = mKeyStates[i].dzOut * 50.f;
			kdz = clamp(kdz, 0.f, 1.f);
			kdz = sqrtf(kdz);
			float kCoeff = mKeyStates[i].mK;
			Vec2 pos = mKeyStates[i].posOut;
			float inhibit = getInhibitThreshold(pos);			
			bool inhibitTest = (z > inhibit);
			bool templateTest = (kdt < mTemplateThresh);
			bool overrideTest = (z > mOverrideThresh);
			bool kCoeffTest = (kCoeff > 0.001f);
			bool ageTest = mKeyStates[i].age > 10;
			bool dzTest = kdz > 0.001f;
            
			if (ageTest && inhibitTest && kCoeffTest && dzTest)
			{	
			
				// if difference of peak neighborhood from template at subpixel position 
				// is under threshold, we may have a new touch.		
				if (templateTest || overrideTest) 
				{
					if(!keyIsOccupied(i))
					{
 debug() << "NEW touch:  key:" << i << " z:" << z << " dz:" << kdz << " template:" << templateTest << " inhibit:" << inhibitTest << "\n";

                        //	Touch t(pos.x(), pos.y(), z, kCoeff);
						Touch t(pos.x(), pos.y(), z, kdz);
						t.key = i;
						t.tDist = kdt;
						int newIdx = addTouch(t); 	
						if(newIdx >= 0)
						{		
							mKeyStates[i].age = 0;										
							

/*
												
debug() << newIdx << " ON z:" << z << " kdt:" << kdt << " at " << pos << "\n";	
debug() << "          template: " << templateTest << " overrideTest: " << overrideTest << "\n";
			debug() << "********\n";
			debug() << "KC " << kCoeff << "\n";
			debug() << "KDZ " << kdz << "\n";
			debug() << "********\n";
*/
			
			// set age for key state to inhibit retrigger
			
						}
					}

				}
			}
		}
	}
}