2dx sprite 设置大小等

#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(00200200));//设置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被打断事件.

{

    

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值