确定是否触摸/点击了对象 [英] Determine if object is touched/tapped

查看:72
本文介绍了确定是否触摸/点击了对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始在Xcode中使用Cocos 2D-X.

I've just started using Cocos 2D-X in Xcode.

我正在尝试制作一个气球弹出游戏,以学习Cocos 2D-X库.到目前为止,我已经能够展示精灵并使它们移动.至于触摸,我可以获取触摸坐标(并将其打印在控制台上).

I'm trying to make a balloon popping game in order to learn the Cocos 2D-X library. So far, I'm able to show sprites and make them move. As for touch, I'm able to get the touch coordinates (and print it out on the console).

现在,我要执行的操作是使气球( CCSprite 对象)弹出"(从图层中删除).我正在寻找解决方案,其中之一是检查触摸位置是否在 CCSprite rect中.但是我发现的所有东西要么已经过时,要么是用目标C编写的.

Now, what I want to do is to have the balloons (a CCSprite object) "pop" (be removed from the layer). I'm looking around for solutions and one of them is to check if the touch location is in the CCSprite rect. But all the things that I found are either outdated or written in Objective C.

如何确定触摸位置是否在气球的直肠内?除了这种方法,还有其他方法吗?

How do I determine if the touch location is within the rect of the balloon? Are there other ways aside from this method?

非常感谢.

编辑:我通过将气球放置在阵列中并检查触摸位置是否击中了该阵列中的气球之一来做到这一点.现在,我正在尝试制作Balloon类并从那里进行处理.感谢所有回答.

EDIT: I did it by putting the balloons in an array and checking if the touch location hits one of the balloons in that array. Now, I'm trying to make a Balloon class and handle it from there. Thanks to all who answered.

推荐答案

您很幸运,因为我有一款使用气球的游戏,下面是我的代码,您可以完成Balloon Class,并且可以将其与CCSprite相同地使用

You are lucky enough because I have a game that use balloons, Below is my code, you can finish the Balloon Class and you can use it the same as CCSprite

示例:

Balloon* blueBalloon = Balloon::spriteWithFile("balloon_blue.png");
this->addChild(blueBalloon);

h个文件:

#include "cocos2d.h"
using namespace cocos2d;

class Balloon : public cocos2d::CCSprite, public CCTargetedTouchDelegate {
public:
virtual void onEnter();
virtual void onExit();
virtual bool ccTouchBegan(CCTouch* touch, CCEvent* event);
virtual void ccTouchMoved(CCTouch* touch, CCEvent* event);
virtual void ccTouchEnded(CCTouch* touch, CCEvent* event);
}; 

cpp文件:

void Balloon::onEnter(){
    CCTouchDispatcher::sharedDispatcher()->addTargetedDelegate(this, 0, true);
    CCSprite::onEnter();
}
void Balloon::onExit(){
    CCTouchDispatcher::sharedDispatcher()->removeDelegate(this);
    CCSprite::onExit();
}

void Balloon::ccTouchMoved(CCTouch* touch, CCEvent* event){
    //do what you want
}
void Balloon::ccTouchEnded(CCTouch* touch, CCEvent* event){
    //do your job here
}

bool Balloon::ccTouchBegan(CCTouch* touch, CCEvent* event){
    CCPoint touchLocation = this->getParent()->convertTouchToNodeSpace(touch);
    if (CCRect::CCRectContainsPoint(this->boundingBox(), touchLocation)) {
        this->playBalloonSound();
        this->removeFromParentAndCleanup(true);
    }

    return true;
}

或者您可以在这篇文章中引用我的代码 cocos2d子类化精灵可以处理触摸?

or you can refer to my code in this post cocos2d subclassing sprite to handle touch?

这篇关于确定是否触摸/点击了对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆