Cocos2dx3.1.1基本控件操作

增加菜单按钮

MenuItemSprite *menuItemPlay = MenuItemSprite::create(
	 Sprite::create("restart_n.png"),//normal状态图片
	 Sprite::create("restart_s.png"),//select状态图片
	 CC_CALLBACK_1(GameOverLayer::onRestart, this));//回调方法以及对应的对象  
	// create menu, it's an autorelease object
	Menu *menu = Menu::create(menuItemPlay, NULL);
	menu->setPosition(centerPos);
	this->addChild(menu); 

void GameOverLayer::onRestart(Ref* pSender)
{
	Scene *playScene = PlayScene::scene();
    //加载PlayScene  
    Director::getInstance()->replaceScene(playScene);
}

增加按钮

Button* buttonPlay = Button::create();  
		buttonPlay->setTouchEnabled(true);  
		buttonPlay->loadTextures("GUIXuanguanGuanka01.png", "GUIXuanguanGuanka00.png", "");  
		buttonPlay->setPosition(LvPanel->getPosition());  
		buttonPlay->addTouchEventListener(this, toucheventselector(LevelChoose::onGame));  
		this->addChild(buttonPlay,15,i);  



void LevelChoose::onGame(Ref *pSender, TouchEventType type)  
{
        Button* butten = (Button*)pSender;
	unsigned int tag = butten->getTag();
	switch (type)
	{
	case gui::TOUCH_EVENT_BEGAN:
		butten->setPositionY(butten->getPositionY()-10);
		break;

	case gui::TOUCH_EVENT_MOVED:
		
		break;

	case gui::TOUCH_EVENT_ENDED:
		butten->setPositionY(butten->getPositionY()+10);

		if (tag == PLAYBUTTON)
		{
			startGame();
		}
		else
		{

		}
		break;

	case gui::TOUCH_EVENT_CANCELED:
		butten->setPositionY(butten->getPositionY()+10);
		break;

	default:
		break;
	}
}

文件动画

auto sprite = Sprite::create(path.c_str());
Animation* animation = Animation::create();
		    // num equal to spriteSheet
			for (int i = 1; i < 3; i++)  
			{  
				char szImageFileName[128] = {0};  
				sprintf(szImageFileName, "GUIGameZhangai17_0%d.png", i);  
				animation->addSpriteFrameWithFileName(szImageFileName);    
			}  
			animation->setDelayPerUnit(0.2f);
			animation->setRestoreOriginalFrame(true);
			Animate *action = Animate::create(animation);  
			sprite->runAction(RepeatForever::create(action)); 



缓存
for (unsigned int i = 0; i < 5; i++) {
		char str[100] = {0};
		sprintf(str, "GUIGameTiaoAnim0%d.png", i);
		frame1 = SpriteFrameCache::getInstance()->getSpriteFrameByName(str);
		animation1->addSpriteFrame(frame1);
	}

创建逐帧动画

//使用SpriteFrameCache获取指定的精灵帧,创建精灵对象
	auto spriteCache2 = SpriteFrameCache::getInstance();
	spriteCache2->addSpriteFramesWithFile("image.plist", "image.png");
	
	
	//添加一个LayerColor层
	//auto layer1 = LayerColor::create(Color4B(255, 0, 0, 255), 85, 121);
	//layer1->setPosition(Point(350,350));
	//this->addChild(layer1);

	
	auto mSprite = Sprite::createWithSpriteFrameName("1.png");
	mSprite->setPosition(Vec2(400, 400));
	this->addChild(mSprite, 1);
	
	Vector<SpriteFrame*> vsp;
	
	for (int i = 1; i <= 3; i++) {
		
		String *string = String::createWithFormat("%d.png", i);
		SpriteFrame *spfr = spriteCache2->getSpriteFrameByName(string->getCString());
		
		vsp.pushBack(spfr);
	}

	Animation *animation = Animation::createWithSpriteFrames(vsp, 0.1);
	Animate *animate = Animate::create(animation);

	auto *ac1 = RepeatForever::create(animate);

	mSprite->runAction(ac1);



你可能感兴趣的:(Cocos2dx3.1.1基本控件操作)