我想实现一个游戏。屏幕上有个气球,触摸到后会消失,并且会发出声音。
我根据oneRain(http://blog.csdn.net/onerain88/article/details/7550009)中的单点触屏,实现了一个TouchableSprite类。
TouchableSprite.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)
{
}
然后在HelloWorld.cpp中生成一个TouchableSprite的实例ball.可是怎么程序正常编译,但是怎么点击屏幕都没有声音。