Hi, I’m following a tutorial about that, but the code is relative to a little old version of cocos2d-x so, something is not working. Here is part of the code:
void HelloWorld::ccTouchesBegan(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent)
{
CCTouch *touch = (CCTouch *) pTouches->anyObject();
CCPoint location = touch->getLocationInView();
location = CCDirector::sharedDirector()->convertToGL(location);
if(CCRect::CCRectContainsPoint(mySprite->boundingBox(), location)
{
mySprite->runAction(CCScaleTo::create(1, 1.1));
}
}
CCRectContainsPoint has been replaced with *containsPoint()*. So, How can I use it?
I am doing something like this:
if (field.getFieldSprite()->boundingBox().containsPoint(location))
{
// we touched a field. Lets see what we can do with it in current state.
Utils::LogTextWithInt("touched field #: ", field.getFieldNumber());
...[snip]....
getFieldSprite() … What the hell is it? I have not found any method like that. I’m using cocos2d-x 2.2 api. Is working for you?
`getFieldSprite() ` is one of my functions that returns the sprite I am doing `->boundingBox().containsPoint(location))` on.
Ok Jason! Now Works all right! Thank you so much!
@Sandro Italiano
You can use it like this
if (mySprite->boundingBox().containsPoint(location))
{
mySprite->runAction(CCScaleTo::create(1, 1.1));
}
@Minggo Zhang
Yes I did it just like that!
@Ken Linak,
I tested your codes on cocos2d-x3.0beta. It works perfectly. And cocos2d-x2.x should work too.
My test codes:
void ActionManual::onEnter()
{
ActionsDemo::onEnter();
auto s = Director::getInstance()->getWinSize();
_tamara->setScaleX( 2.5f);
_tamara->setScaleY( -1.0f);
_tamara->setPosition( Point(100,70) );
_tamara->setOpacity( 128);
/// ADDED
auto dispatcher = Director::getInstance()->getEventDispatcher();
auto listener = EventListenerTouchOneByOne::create();
listener->onTouchBegan = [](Touch* touch, Event* event){
auto sprite = event->getCurrentTarget();
Point pt = touch->getLocation();
Rect recTemp= sprite->boundingBox();
if(recTemp.containsPoint(pt )) {
//TOUCHED
log("touched");
}
// log("not touched");
return false;
};
/////////
dispatcher->addEventListenerWithSceneGraphPriority(listener, _tamara);
_grossini->setRotation( 120);
_grossini->setPosition( Point(s.width/2, s.height/2));
_grossini->setColor( Color3B( 255,0,0));
_kathia->setPosition( Point(s.width-100, s.height/2));
_kathia->setColor( Color3B::BLUE);
}
2 Likes