cocos2d-x学习三触摸抬起响应喷发子弹



bool HelloWorld::init()
{
    //
    // 1. super init first
    if ( !CCLayerColor::initWithColor(ccc4(255,255,255,255)))
    {
        return false;
    } 
	
	CCSize screenSize = CCDirector::sharedDirector()->getVisibleSize();

	CCSprite *pSprite = CCSprite::create("Player.png");
	pSprite->setPosition(ccp(20.0,screenSize.height/2));
	this->addChild(pSprite);   

	this->schedule(schedule_selector(HelloWorld::time),2);
	this->setTouchEnabled(true);

    return true;
}


1.固定子弹速度,固定距离D,也就是固定了move动作的时间间隔。

2.通过相似三角形获得子弹终点。

void HelloWorld::ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent) 
{
	CCTouch *touch = (CCTouch *)pTouches->anyObject();
	CCPoint locInVw = touch->getLocationInView();
	CCPoint locInGl = CCDirector::sharedDirector()->convertToGL(locInVw);

	CCSize screenSize = CCDirector::sharedDirector()->getVisibleSize();
	CCSprite *proj = CCSprite::create("Projectile.png");
	proj->setPosition(ccp(20.0,screenSize.height/2.0));
	this->addChild(proj);

	double dx = locInGl.x - 20;
	double dy = locInGl.y - screenSize.height/2.0;
	double d = sqrt(dx*dx + dy*dy);

	double D = sqrt(screenSize.width * screenSize.width + screenSize.height * screenSize.height);
	double ratio = D/d;
	double endx = ratio * dx + 20;
	double endy = ratio *dy + screenSize.height/2.0;

	CCMoveTo *move = CCMoveTo::create(D/320,ccp(endx,endy));
	CCCallFuncN *sd = CCCallFuncN::create(this,callfuncN_selector(HelloWorld::selfdefinedAc));
	CCSequence *actions = CCSequence::create(move,sd,NULL);
	proj->runAction(actions);
}	




void HelloWorld::selfdefinedAc(CCNode* pSender)
{
//	m_pSprite->setPosition(ccp(0.0,0.0));
//	m_pSprite->setScale(2.0);
	pSender->removeFromParentAndCleanup(true);
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

MyObject-C

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

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

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

打赏作者

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

抵扣说明:

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

余额充值