I want to implemente a game.The ball disappeared by touched with the sound.
(Android platform + cocos2d-2.1beta3-x-2.1.0)
TouchSprite.h
#ifndef __TOUCHABLESPRITE_H__
#define __TOUCHABLESPRITE_H__
#include "cocos2d.h"
class TouchableSprite : public cocos2d::CCSprite ,public cocos2d::CCTargetedTouchDelegate
{
public:
TouchableSprite();
virtual ~TouchableSprite();
static TouchableSprite* touchSpriteWithFile(const char *file);
bool initWithFile(const char *file);
virtual void onEnter();
virtual void onExit();
cocos2d::CCRect rect();
bool containsTouchLocation( cocos2d::CCTouch *touch );
virtual bool ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent);
virtual void ccTouchMoved(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent);
virtual void ccTouchEnded(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent);
};
#endif // __TOUCHABLESPRITE_H__
TouchableSprite.cpp
#include "TouchableSprite.h"
#include "SimpleAudioEngine.h"
using namespace CocosDenshion;
USING_NS_CC;
TouchableSprite::TouchableSprite()
{
}
TouchableSprite::~TouchableSprite()
{
}
void TouchableSprite::onEnter()
{
CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate (this, 0, true);
CCSprite::onEnter();
}
void TouchableSprite::onExit()
{
CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);
CCSprite::onExit();
}
TouchableSprite* TouchableSprite::touchSpriteWithFile(const char *file)
{
CCSprite::spriteWithFile(file);
}
bool TouchableSprite::initWithFile(const char *file)
{
CCSprite::initWithFile(file);
}
CCRect TouchableSprite::rect()
{
CCSize size = getContentSize();
CCPoint pos = getPosition();
return CCRectMake(pos.x-size.width/2, pos.y-size.height/2, size.width, size.height);
}
bool TouchableSprite::containsTouchLocation( cocos2d::CCTouch *touch )
{
CCPoint touchPoint = touch->getLocation();
return CCRect::CCRectContainsPoint(TouchableSprite::rect(), touchPoint);
}
void TouchableSprite::ccTouchEnded(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
{
SimpleAudioEngine::sharedEngine()->playEffect("ball_broke.wav");
}
bool TouchableSprite::ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
{
if(containsTouchLocation(pTouch))
{
SimpleAudioEngine::sharedEngine()->playEffect("ball_broke.wav");
return true;
}
else
return false;
}
void TouchableSprite::ccTouchMoved(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
{
}
In HelloWorld.cpp..i create a ball.
I can compile,but touch the ball without any reaction.