cocos2dx中怎么调取通过setTag做过标记的精灵?

cocos2dx中怎么调取通过setTag做过标记的精灵?,第1张

void HelloWorld::addTarget(){

CCSize winSize = CCDirector::sharedDirector()->getWinSize();

CCSprite target = CCSprite::create(Targetpng);

//随机位置

int minY = target->getContentSize()height/20;

int maxY = winSizeheight - target->getContentSize()height/20;

int rangeY = maxY - minY;

int actualY = rand()%rangeY + minY;

target->setPosition(ccp(winSizewidth - target->getContentSize()width/20, actualY));

target->setTag(1);

this->addChild(target);

aarayTarget->addObject(target);

//随机速度

float minDuration = 20;

float maxDuration = 40;

int rangeDuration = maxDuration - minDuration;

float actualDuration = rand()%rangeDuration + minDuration;

CCFiniteTimeAction actionMove = CCMoveTo::create(actualDuration, ccp(0 - target->getContentSize()width/20, actualY));//0代表屏幕外,这句表示在3秒内从初始位置移动到屏幕外

//增加一个回调函数,回收移动到屏幕外的精灵

CCFiniteTimeAction actionMoveDone = CCCallFuncN::create(this, callfuncN_selector(HelloWorld::spriteMoveFinished));

target->runAction(CCSequence::create(actionMove,actionMoveDone,NULL));

}

void HelloWorld::spriteMoveFinished(cocos2d::CCNode sender){

CCSprite sprite = (CCSprite )sender;

// this->removeChild(sprite, true);

if (sprite->getTag() == 1) {

aarayTarget->removeObject(sprite);//清除敌人

}else if(sprite->getTag() == 2){

arrayProjectile->removeObject(sprite);//清除飞镖

}

}

//发射飞镖

void HelloWorld::ccTouchesEnded(cocos2d::CCSet touches, cocos2d::CCEvent event){

CCTouch touch = (CCTouch )touches->anyObject();

CCPoint location = touch->getLocationInView();

location = this->convertTouchToNodeSpace(touch);

CCSize winSize = CCDirector::sharedDirector()->getWinSize();

CCSprite projectile = CCSprite::create(Projectilepng);

projectile->setPosition(ccp(20, winSizeheight/2));

float offX = locationx - projectile->getPositionX();

float offY = locationy - projectile->getPositionY();

if (offX <= 0) {

return;

}

projectile->setTag(2);

this->addChild(projectile);

arrayProjectile->addObject(projectile);

float angle = offY/offX;

float realX= winSizewidth + projectile->getContentSize()width/2;

float realY = realX angle + projectile->getPositionY();

CCPoint finalPosition = ccp(realX, realY);

//获取飞镖飞行时间

float length = sqrtf(realX realX + realYrealY);

float velocity = 480;

float realMoveDuration = length/velocity;

projectile->runAction(CCSequence::create(CCMoveTo::create(realMoveDuration, finalPosition),CCCallFuncN::create(this, callfuncN_selector(HelloWorld::spriteMoveFinished)),NULL));

}

//碰撞检测,消除飞镖和敌人

void HelloWorld::update(cocos2d::CCTime dt){

for (int i = 0; i < aarayTarget->count(); i++) {

CCSprite target = (CCSprite )aarayTarget->objectAtIndex(i);

for (int j = 0; j < arrayProjectile->count(); j++) {

CCSprite projectile = (CCSprite )arrayProjectile->objectAtIndex(j);

if (target->boundingBox()intersectsRect(projectile->boundingBox())) {

aarayTarget->removeObjectAtIndex(i);

arrayProjectile->removeObjectAtIndex(j);

this->removeChild(target);

this->removeChild(projectile);

break;

}

}

}

}

void HelloWorld::menuCloseCallback(CCObject pSender)

{

CCDirector::sharedDirector()->end();

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)

exit(0);

#endif

}

你打印的是创建时候的角度,想要获得转弯之后的角度,必须是在转弯之后获取角度并打印,你可以将转动角度和获取角度放在CCSequence,获取角度放在CCCallfuncN回调函数里面;当然你也可以直接通过一个定时器来获取

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/13494430.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2025-09-01
下一篇2025-09-01

发表评论

登录后才能评论

评论列表(0条)

    保存