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);



转载于:https://my.oschina.net/ffs/blog/286632

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值