#ifndef StudyOne1_Fly_h
#define StudyOne1_Fly_h
#include "cocos2d.h"
using namespace cocos2d;
class Fly : public cocos2d::CCLayer
{
CCSprite *sprite;
public:
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init();
// there's no 'id' in cpp, so we recommand to return the exactly class pointer
//static cocos2d::CCScene* scene();
// a selector callback
//virtual void menuCloseCallback(CCObject* pSender);
// implement the "static node()" method manually
LAYER_NODE_FUNC(Fly);
virtual bool ccTouchBegan(cocos2d::CCTouch *ptouch,cocos2d::CCEvent *pEvent);//处理用户按下事件,若用户需要继续处理该按下对应的后续事件,则return true, 否则,return false.
virtual void ccTouchMoved(cocos2d::CCTouch *pTouch,cocos2d::CCEvent *pEvent); // 处理Touch Move 事件
virtual void ccTouchEnded(cocos2d::CCTouch *pTouch,cocos2d::CCEvent *pEvent);// 处理用户放开事件
virtual void ccTouchCancelled(cocos2d::CCTouch *pTouch,cocos2d::CCEvent *pEvent); //处理Touch被打断事件
};
#endif // __TestLayer_SCENE_H__
#include <iostream>
#include "Fly.h"
//using namespace cocos2d;//命名空间
using namespace std;
bool Fly::init(){
if(!CCLayer::init())
{
return false;
}
cout<<"hhh"<<endl;
sprite=CCSprite::spriteWithFile("Icon.png");
//sprite->setTextureRect(CCRectMake(-200, 300, 40, 80));
//设置精灵的大小但不影响图片大小;
CCSize size=sprite->getContentSize();
float contentSizeWidth=size.width;//改变图片大小
float contentSizeHeight = size.height;
sprite->setTextureRect(CCRectMake(0, 0, 200, 200));//设置sprite的可变区域
//sprite->setScale(400/contentSizeWidth);//设置图片的宽高(用缩放控制)。
//sprite->setScale(400/contentSizeWidth);
//sprite->setScale(0.5);
sprite->setPosition(ccp(100,100));
this->addChild(sprite);
//sprite->setRotation(30);
CCTouchDispatcher::sharedDispatcher()->addTargetedDelegate(this, 0, true);
return true;//表示接下来还相应后边的事件
}
//鼠标点击事件
bool Fly:: ccTouchBegan(cocos2d::CCTouch *ptouch,cocos2d::CCEvent *pEvent)
{
cout<<"点"<<endl;
CCPoint touchPoint = ptouch->locationInView(ptouch->view()); //获取触摸坐标
// touchPoint = CCDirector:: sharedDirector()->convertToGL(touchPoint);
cout<<touchPoint.x<<endl;
cout<<touchPoint.y<<endl;
return true;
}
void Fly::ccTouchMoved(cocos2d::CCTouch *pTouch,cocos2d::CCEvent *pEvent) // 处理Touch Move 事件
{
CCPoint touchPoint = pTouch->locationInView(pTouch->view());
sprite->setPosition(ccp(touchPoint.x , 320-touchPoint.y));//拖动精灵
}
void Fly::ccTouchEnded(cocos2d::CCTouch *pTouch,cocos2d::CCEvent *pEvent)// 处理用户放开事件
{
}
void Fly::ccTouchCancelled(cocos2d::CCTouch *pTouch,cocos2d::CCEvent *pEvent) //处理Touch被打断事件.
{
}