cocos2d-x学习六aircraft2


场景添加子Layer

bool GamingLayer::init()
{
	if(!CCLayer::init())
	{
		return false;
	}

	setUpView();

	//schedule
	this->schedule(schedule_selector(GamingLayer::backgroundScrool),0.1f);

	//score layer
	ScoreLayer *scoreLy = ScoreLayer::create();

	//layer的anchorpoint对position无影响
	scoreLy->setPosition(ccp(10.0,CCDirector::sharedDirector()->getWinSize().height-27));
	this->addChild(scoreLy);

	//time layer
	TimeLayer *timeLy = TimeLayer::create();
	timeLy->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width - 109,CCDirector::sharedDirector()->getWinSize().height-27));
	this->addChild(timeLy);

	return true;
}


schedule实现背景滚动

void GamingLayer::backgroundScrool(float val)
{
	CCSprite *bk1 = (CCSprite *)getChildByTag(11);
	CCSprite *bk2 = (CCSprite *)getChildByTag(12);

	bk1->setPositionY(bk1->getPositionY() - 10.0);
	bk2->setPositionY(bk1->getPositionY() + 576);

	if(bk2->getPositionY() <= 0)
	{
		bk1->setPositionY(0.0);
	}

}



menu callback 获取父Layer、隐藏自身、启动pause menu、恢复音乐

void PauseLayer::MenuresumeCallBack(CCObject* pSender)
{
	CCLayer *farther = (CCLayer *)this->getParent();
	//hide pause layer
	farther->getChildByTag(99)->setVisible(false);
	//enable pause menu
	((CCMenu*)farther->getChildByTag(156))->setEnabled(true);
	//resume
	CCDirector::sharedDirector()->resume();
	//background music
	if(CocosDenshion::SimpleAudioEngine::sharedEngine()->isBackgroundMusicPlaying())
	{
		CocosDenshion::SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
	}
}


切换图片动作

void WarriorLayer::setUpView()
{
    CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
	//warrior
	m_pWarrior = CCSprite::create("ship01.png",CCRectMake(60.0,0.0,60.0,43.0));
	m_pWarrior->setPosition(ccp(visibleSize.width/2,visibleSize.height/2));
	this->addChild(m_pWarrior);

	//animation
	CCAnimation *animation = CCAnimation::create();
	animation->setDelayPerUnit(0.3f);

	animation->addSpriteFrame(CCSpriteFrame::create("ship01.png",CCRectMake(60.0,0.0,60.0,43.0)));
	animation->addSpriteFrame(CCSpriteFrame::create("ship01.png",CCRectMake(0.0,0.0,60.0,43.0)));

	CCAnimate *animate = CCAnimate::create(animation);
	m_pWarrior->runAction(CCRepeatForever::create(animate));
}

CCSpriteBatchNode的使用:

1、CCSpriteBatchNode 中的所有CCSprite只会被渲染1次,因此可以提高游戏的FPS。

2、加入到 CCSpriteBatchNode 中的CCSprite必须使用同一张纹理图。


两种Cocos2d-x触摸事件:

两者的区别:CCSet与CCTouch,一个事件集合一个单个事件。

事件分发的顺序: CCTargetedTouchDelegate->CCStandardTouchDelegate。

CCTargetedTouchDelegate的使用:

(1) 启用触摸事件

(2) 使用addTargetedDelegate方法注册到触摸事件分发器。

(3) 重载事件回调函数。注意,我们必须在触摸开始事件中针对需要接受的事件返回true以捕捉事件。

(4) 当不再需要接受触摸事件时,使用removeDelegate方法来注销触摸事件的接收。与标准触摸事件相比,不同之处主要在于开始触摸事件需要返回一个代表是否捕捉事件的值。

ccTouchBegan方法返回一个布尔值,表示声明是否要捕捉这个触摸点,只有在此方法中捕捉到的触摸点才会继续引发其他3个事件,否则此触摸点的其他事件都会被忽略。

	CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0, true);
第三个参数表明了是否"吞噬"一个触摸,如果设置为true,一个触摸一旦被捕捉,那么所有优先级更低的接收对象都无法接收到触摸。CCMenu就是一个会"吞噬"且优先级为-128的触摸接收器,由于它的优先级很高,所以菜单按钮总能获得触摸响应。

void WarriorLayer::registerWithTouchDispatcher(void)
{
	CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0, true);
}




bool WarriorLayer::ccTouchBegan(CCTouch  *pTouches, CCEvent *pEvent)
{
	m_pWarrior->stopAllActions();
	CCMoveTo *move = CCMoveTo::create(0.5f,pTouches->getLocation());
	m_pWarrior->runAction(move);	
	return true;
}









  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MyObject-C

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值