#include "HelloWorldScene.h"
#include "SimpleAudioEngine.h"
using namespace cocos2d;
using namespace CocosDenshion;
CCScene* HelloWorld::scene()
{
// 'scene' is an autorelease object
CCScene *scene = CCScene::create();
// 'layer' is an autorelease object
HelloWorld *layer = HelloWorld::create();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
//
// 1. super init first
if ( !CCLayer::init() )
{
return false;
}
/
// 2. add a menu item with "X" image, which is clicked to quit the program
// you may modify it.
// add a "close" icon to exit the progress. it's an autorelease object
CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
this,
menu_selector(HelloWorld::menuCloseCallback) );
pCloseItem->setPosition( ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20) );
// create menu, it's an autorelease object
CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
pMenu->setPosition( CCPointZero );
this->addChild(pMenu, 1);
CHECK_GL_ERROR_DEBUG();
glLineWidth(25);
ccDrawColor4B(255, 0, 0, 255);
ccDrawLine(ccp(0, 0), ccp(CCDirector::sharedDirector()->getWinSize().width,CCDirector::sharedDirector()->getWinSize().height));
/
// 3. add your codes below...
// add a label shows "Hello World"
// create and initialize a label
CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Thonburi", 34);
// ask director the window size
CCSize size = CCDirector::sharedDirector()->getWinSize();
// position the label on the center of the screen
pLabel->setPosition( ccp(size.width / 2, size.height - 20) );
// add the label as a child to this layer
this->addChild(pLabel, 1);
// add "HelloWorld" splash screen"
CCSprite* pSprite = CCSprite::create("HelloWorld.png");
// position the sprite on the center of the screen
pSprite->setPosition( ccp(size.width/2, size.height/2) );
// add the sprite as a child to this layer
this->addChild(pSprite, 0);
//简单过程是,使用CCTexture2D加载图片 ,用CCTexture2D生成对应的CCSpriteFrame(对应的就是帧),将CCSpriteFrame添加到CCAnimation生成动画数据,用CCAnimation生成CCAnimate(就是最终的动画动作),最后用CCSprite执行这个动作//把当前图片加入缓存中
// manually add frames to the frame cache
CCSpriteFrame *frame0 = CCSpriteFrame::createWithTexture(texture, CCRectMake(132*0, 132*0, 132, 132)); //从缓存中读取精灵
CCSpriteFrame *frame1 = CCSpriteFrame::createWithTexture(texture, CCRectMake(132*1, 132*0, 132, 132));
CCSpriteFrame *frame2 = CCSpriteFrame::createWithTexture(texture, CCRectMake(132*2, 132*0, 132, 132));
CCSpriteFrame *frame3 = CCSpriteFrame::createWithTexture(texture, CCRectMake(132*3, 132*0, 132, 132));
CCSpriteFrame *frame4 = CCSpriteFrame::createWithTexture(texture, CCRectMake(132*0, 132*1, 132, 132));
CCSpriteFrame *frame5 = CCSpriteFrame::createWithTexture(texture, CCRectMake(132*1, 132*1, 132, 132));
//
// Animation using Sprite BatchNode
//
CCSprite* sprite = CCSprite::createWithSpriteFrame(frame0); //创建动画开始精灵
sprite->setPosition( ccp( size.width/2-80, size.height/2) );
addChild(sprite);
CCArray* animFrames = CCArray::createWithCapacity(6); //创建大小为6的集合
animFrames->addObject(frame0); //将没一针精灵动画添加到集合中
animFrames->addObject(frame1);
animFrames->addObject(frame2);
animFrames->addObject(frame3);
animFrames->addObject(frame4);
animFrames->addObject(frame5);
CCAnimation *animation = CCAnimation::createWithSpriteFrames(animFrames, 0.2f); //通过集合创建动画,设置为0.2秒切换
CCAnimate *animate1 = CCAnimate::create(animation);
CCActionInterval* seq = (CCActionInterval *)CCSequence::create( animate1, //设置动作集合
CCFlipX::create(true),
animate1->copy()->autorelease(),
CCFlipX::create(false),
NULL);
sprite->runAction(CCRepeatForever::create( seq ) );
CCMenuItemFont *testtest=CCMenuItemFont::create("0",this,menu_selector(HelloWorld::click));
CCMenu *clicktestt=CCMenu::create(testtest,NULL);
this->addChild(clicktestt,1);
clicktestt->setPosition(ccp(size.width/2+10, size.height/2));
setTouchEnabled(true);
hero=CCSprite::create("grossini_dance_06.png");
hero->setPosition(ccp(size.width/4*3, size.height/2));
this->addChild(hero);
isControl=true;
deltax=0;
deltay=0;
//
return true;
}
//用于点击的优先级设置
void HelloWorld::registerWithTouchDispatcher()
{
CCDirector *pDirector = CCDirector::sharedDirector();
pDirector->getTouchDispatcher()->addTargetedDelegate(this, 0, true);
}
void HelloWorld::ccTouchCancelled(CCTouch *touch, CCEvent *event)
{
isControl=false;
}
bool HelloWorld::ccTouchBegan(CCTouch *touch, CCEvent *event)
{
CCPoint heropos =hero->getPosition();
// 获取点在视图中的坐标(左上角为原点)
CCPoint location=touch->locationInView(); //location.x和location.y就是点击的坐标点
// 把点的坐标转换成OpenGL坐标(左下角为原点)
location=CCDirector::sharedDirector()->convertToGL(location);
if (location.x>=heropos.x-2.5 &&location.x<=heropos.x+2.5&&location.y>=heropos.y-3.5&&location.y<=heropos.y+3.5)
{
deltax=location.x-heropos.x;
deltay=location.y-heropos.y;
isControl=true;
}
}
void HelloWorld::ccTouchMoved(CCTouch *touch, CCEvent *event)
{
if (isControl) {
CCPoint location=touch->locationInView();
location =CCDirector::sharedDirector()->convertToGL(location);
float x=location.x-deltax;
float y=location.y-deltay;
hero->setPosition(ccp(x,y));
}
}
void HelloWorld::ccTouchEnded(CCTouch *touch, CCEvent *event)
{
this->ccTouchBegan(touch, event);
}
void HelloWorld::click(CCObject *pSender)
{
CCMenuItemFont *testtest=(CCMenuItemFont*)pSender;
int a=strstr+200;
char str[30]={0};
sprintf(str, "%d",a);
testtest->setString(str);
strstr+=200;
}
void HelloWorld::menuCloseCallback(CCObject* pSender)
{
CCDirector::sharedDirector()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
}
如果对触控不是很了解,可以看这里:http://blog.csdn.net/somestill/article/details/9988565