将这两份代码复制到HelloCpp中,把cocos2d-x/templates/template-lua的Reource复制到HelloCpp中。
//HelloWorldScene.cpp
#include "HelloWorldScene.h"
#include "AppMacros.h"
#include "cocos-ext.h"
USING_NS_CC;
USING_NS_CC_EXT;
CCSprite* HelloWorld::createDog(){
auto tmp = 2;
auto frameWidth = 105/tmp, frameHeight = 95/tmp;
auto visibleSize = CCDirector::sharedDirector()->getVisibleSize();
auto origin = CCDirector::sharedDirector()->getVisibleOrigin();
auto textureDog = CCTextureCache::sharedTextureCache()->addImage("dog.png");
auto rect = CCRectMake(0, 0, frameWidth, frameHeight);
auto frame0 = CCSpriteFrame::createWithTexture(textureDog, rect);
rect = CCRectMake(frameWidth, 0, frameWidth, frameHeight);
auto frame1 = CCSpriteFrame::createWithTexture(textureDog, rect);
// auto asmtest = CCSpriteBatchNode::create("dog.png");
// auto spriteDog = CCSprite::createWithTexture(asmtest->getTexture(), CCRectMake(0, 0, frameWidth, frameHeight));
spriteDog = CCSprite::createWithSpriteFrame(frame0);
spriteDog->setPosition( ccp(origin.x, origin.y+visibleSize.height/4*3) );
auto animFrames = CCArray::create();
animFrames->addObject(frame0);
animFrames->addObject(frame1);
auto animation = CCAnimation::createWithSpriteFrames(animFrames, 0.5);
auto animate = CCRepeatForever::create(CCAnimate::create(animation));
spriteDog->runAction(animate);
return spriteDog;
}
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);
// scene->addChild(createLayerMenu());
// scene->addChild(createLayerFarm());
return scene;
}
// on "init" you need to initialize your instance
bool HelloWorld::init(){
// 1. super init first
if ( !CCLayer::init() ){
return false;
}
CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
// 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(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 ,
origin.y + pCloseItem->getContentSize().height/2));
// create menu, it's an autorelease object
CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
pMenu->setPosition(CCPointZero);
this->addChild(pMenu, 1);
/////////////////////////// //
// 3. add your codes below
// add a label shows "Hello World"
// create and initialize a label
CCLabelTTF* pLabel = CCLabelTTF::create("Hello Mark", "Arial", TITLE_FONT_SIZE);
// position the label on the center of the screen
pLabel->setPosition(ccp(origin.x + visibleSize.width/2,
origin.y + visibleSize.height - pLabel->getContentSize().height));
// add the label as a child to this layer
this->addChild(pLabel, 1);
auto bg = CCSprite::create("farm.jpg");
CCLog("0x%x\n", bg);
bg->setPosition(ccp(origin.x + visibleSize.width /2, origin.y + visibleSize.height/2));
this->addChild(bg);
auto diff_platform = 2;
auto derta_y = 70;
for(auto i = 0; i < 4; ++i){
for(auto j = 0; j < 2; ++j){
auto spriteLand = CCSprite::create("land.png");
spriteLand->setPosition(ccp(200 + j * 180/2 - i % 2 * 90/diff_platform,
derta_y + 10 + i*95/2/diff_platform));
this->addChild(spriteLand);
}
}
auto frameCrop = CCSpriteFrame::create("crop.png", CCRectMake(0, 0, 105/diff_platform -1, 95/diff_platform));
for(auto i = 0; i < 4; ++i){
for(auto j = 0; j < 2; ++j){
auto spriteCrop = CCSprite::createWithSpriteFrame(frameCrop);
spriteCrop->setPosition(ccp(10/diff_platform+200+j*180/diff_platform - i%2*90/diff_platform,
derta_y + 30/diff_platform + 10 + i * 95 /2/diff_platform));
this->addChild(spriteCrop);
}
}
touchBeginPoint = this->getPosition();
auto spriteDog = HelloWorld::createDog();
this->addChild(spriteDog, 1);
CCDirector* pDirector = CCDirector::sharedDirector();
pDirector->getTouchDispatcher()->addTargetedDelegate((CCTargetedTouchDelegate*) this, 0, true);
schedule(schedule_selector(HelloWorld::tick), 0.0f);
return true;
}
void HelloWorld::menuCloseCallback(CCObject* pSender){
CCDirector::sharedDirector()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
}
void HelloWorld::tick( float ff){
auto visibleSize = CCDirector::sharedDirector()->getVisibleSize();
auto origin = CCDirector::sharedDirector()->getVisibleOrigin();
if( this->spriteDog == NULL){
CCLog("this == %0x\n", ( int) this);
return ;
}
auto x = this->spriteDog->getPosition().x;
if(x > origin.x + visibleSize.width){
x = origin.x;
} else{
x += 1;
}
spriteDog->setPositionX(x);
}
bool HelloWorld::ccTouchBegan(CCTouch* touch, CCEvent* event){
CCLog("ccTouchBegan-----\n");
touchBeginPoint = touch->getLocation();
spriteDog->pauseSchedulerAndActions();
return true;
}
void HelloWorld::ccTouchEnded(CCTouch* touch, CCEvent* event){
CCLog("onTouchEnded-----\n");
spriteDog->resumeSchedulerAndActions();
}
void HelloWorld::ccTouchMoved(CCTouch* touch, CCEvent* event){
// CCLog("onTouchmoved-----\n");
auto touchEndPoint = this->getPosition();
auto x = touch->getLocation().x;
auto y = touch->getLocation().y;
this->setPosition(touchEndPoint.x + x - touchBeginPoint.x,
touchEndPoint.y + y - touchBeginPoint.y);
touchBeginPoint = CCPoint(x, y);
}
#include "HelloWorldScene.h"
#include "AppMacros.h"
#include "cocos-ext.h"
USING_NS_CC;
USING_NS_CC_EXT;
CCSprite* HelloWorld::createDog(){
auto tmp = 2;
auto frameWidth = 105/tmp, frameHeight = 95/tmp;
auto visibleSize = CCDirector::sharedDirector()->getVisibleSize();
auto origin = CCDirector::sharedDirector()->getVisibleOrigin();
auto textureDog = CCTextureCache::sharedTextureCache()->addImage("dog.png");
auto rect = CCRectMake(0, 0, frameWidth, frameHeight);
auto frame0 = CCSpriteFrame::createWithTexture(textureDog, rect);
rect = CCRectMake(frameWidth, 0, frameWidth, frameHeight);
auto frame1 = CCSpriteFrame::createWithTexture(textureDog, rect);
// auto asmtest = CCSpriteBatchNode::create("dog.png");
// auto spriteDog = CCSprite::createWithTexture(asmtest->getTexture(), CCRectMake(0, 0, frameWidth, frameHeight));
spriteDog = CCSprite::createWithSpriteFrame(frame0);
spriteDog->setPosition( ccp(origin.x, origin.y+visibleSize.height/4*3) );
auto animFrames = CCArray::create();
animFrames->addObject(frame0);
animFrames->addObject(frame1);
auto animation = CCAnimation::createWithSpriteFrames(animFrames, 0.5);
auto animate = CCRepeatForever::create(CCAnimate::create(animation));
spriteDog->runAction(animate);
return spriteDog;
}
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);
// scene->addChild(createLayerMenu());
// scene->addChild(createLayerFarm());
return scene;
}
// on "init" you need to initialize your instance
bool HelloWorld::init(){
// 1. super init first
if ( !CCLayer::init() ){
return false;
}
CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
// 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(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 ,
origin.y + pCloseItem->getContentSize().height/2));
// create menu, it's an autorelease object
CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
pMenu->setPosition(CCPointZero);
this->addChild(pMenu, 1);
/////////////////////////// //
// 3. add your codes below
// add a label shows "Hello World"
// create and initialize a label
CCLabelTTF* pLabel = CCLabelTTF::create("Hello Mark", "Arial", TITLE_FONT_SIZE);
// position the label on the center of the screen
pLabel->setPosition(ccp(origin.x + visibleSize.width/2,
origin.y + visibleSize.height - pLabel->getContentSize().height));
// add the label as a child to this layer
this->addChild(pLabel, 1);
auto bg = CCSprite::create("farm.jpg");
CCLog("0x%x\n", bg);
bg->setPosition(ccp(origin.x + visibleSize.width /2, origin.y + visibleSize.height/2));
this->addChild(bg);
auto diff_platform = 2;
auto derta_y = 70;
for(auto i = 0; i < 4; ++i){
for(auto j = 0; j < 2; ++j){
auto spriteLand = CCSprite::create("land.png");
spriteLand->setPosition(ccp(200 + j * 180/2 - i % 2 * 90/diff_platform,
derta_y + 10 + i*95/2/diff_platform));
this->addChild(spriteLand);
}
}
auto frameCrop = CCSpriteFrame::create("crop.png", CCRectMake(0, 0, 105/diff_platform -1, 95/diff_platform));
for(auto i = 0; i < 4; ++i){
for(auto j = 0; j < 2; ++j){
auto spriteCrop = CCSprite::createWithSpriteFrame(frameCrop);
spriteCrop->setPosition(ccp(10/diff_platform+200+j*180/diff_platform - i%2*90/diff_platform,
derta_y + 30/diff_platform + 10 + i * 95 /2/diff_platform));
this->addChild(spriteCrop);
}
}
touchBeginPoint = this->getPosition();
auto spriteDog = HelloWorld::createDog();
this->addChild(spriteDog, 1);
CCDirector* pDirector = CCDirector::sharedDirector();
pDirector->getTouchDispatcher()->addTargetedDelegate((CCTargetedTouchDelegate*) this, 0, true);
schedule(schedule_selector(HelloWorld::tick), 0.0f);
return true;
}
void HelloWorld::menuCloseCallback(CCObject* pSender){
CCDirector::sharedDirector()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
}
void HelloWorld::tick( float ff){
auto visibleSize = CCDirector::sharedDirector()->getVisibleSize();
auto origin = CCDirector::sharedDirector()->getVisibleOrigin();
if( this->spriteDog == NULL){
CCLog("this == %0x\n", ( int) this);
return ;
}
auto x = this->spriteDog->getPosition().x;
if(x > origin.x + visibleSize.width){
x = origin.x;
} else{
x += 1;
}
spriteDog->setPositionX(x);
}
bool HelloWorld::ccTouchBegan(CCTouch* touch, CCEvent* event){
CCLog("ccTouchBegan-----\n");
touchBeginPoint = touch->getLocation();
spriteDog->pauseSchedulerAndActions();
return true;
}
void HelloWorld::ccTouchEnded(CCTouch* touch, CCEvent* event){
CCLog("onTouchEnded-----\n");
spriteDog->resumeSchedulerAndActions();
}
void HelloWorld::ccTouchMoved(CCTouch* touch, CCEvent* event){
// CCLog("onTouchmoved-----\n");
auto touchEndPoint = this->getPosition();
auto x = touch->getLocation().x;
auto y = touch->getLocation().y;
this->setPosition(touchEndPoint.x + x - touchBeginPoint.x,
touchEndPoint.y + y - touchBeginPoint.y);
touchBeginPoint = CCPoint(x, y);
}
//
HelloWorldScene.h
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
class HelloWorld : public cocos2d::CCLayer, public cocos2d::CCTargetedTouchDelegate
{
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 recommend returning the class instance pointer
static cocos2d::CCScene* scene();
void tick( float);
cocos2d::CCSprite* createDog();
// a selector callback
void menuCloseCallback(CCObject* pSender);
// implement the "static node()" method manually
CREATE_FUNC(HelloWorld);
virtual bool ccTouchBegan(cocos2d::CCTouch* touch, cocos2d::CCEvent* event);
virtual void ccTouchEnded(cocos2d::CCTouch* touch, cocos2d::CCEvent* event);
virtual void ccTouchMoved(cocos2d::CCTouch* touch, cocos2d::CCEvent* event);
private:
cocos2d::CCSprite *spriteDog;
cocos2d::CCPoint touchBeginPoint;
};
#endif // __HELLOWORLD_SCENE_H__
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
class HelloWorld : public cocos2d::CCLayer, public cocos2d::CCTargetedTouchDelegate
{
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 recommend returning the class instance pointer
static cocos2d::CCScene* scene();
void tick( float);
cocos2d::CCSprite* createDog();
// a selector callback
void menuCloseCallback(CCObject* pSender);
// implement the "static node()" method manually
CREATE_FUNC(HelloWorld);
virtual bool ccTouchBegan(cocos2d::CCTouch* touch, cocos2d::CCEvent* event);
virtual void ccTouchEnded(cocos2d::CCTouch* touch, cocos2d::CCEvent* event);
virtual void ccTouchMoved(cocos2d::CCTouch* touch, cocos2d::CCEvent* event);
private:
cocos2d::CCSprite *spriteDog;
cocos2d::CCPoint touchBeginPoint;
};
#endif // __HELLOWORLD_SCENE_H__