这个帖子是笔记,有想用C或C++语言开发游戏的可以加这个
掌柜的创的Cocos2d-x 学习群:153412944
图片资源:
C++代码:
//HelloWorldScene.h
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
class HelloWorld : public cocos2d:ayerColor
{
public:
// there's no 'id' in cpp, so we recommend returning the class instance pointer
static cocos2d::Scene* createScene();
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init();
virtual void HelloWorld::update(float dt);
// implement the "static create()" method manually
CREATE_FUNC(HelloWorld);
private:
cocos2d::Sprite * ball;
};
#endif // __HELLOWORLD_SCENE_H__
//HelloWorldScene.cpp
#include "HelloWorldScene.h"
#include "cocostudio/CocoStudio.h"
#include "ui/CocosGUI.h"
USING_NS_CC;
using namespace cocostudio::timeline;
Scene* HelloWorld::createScene()
{
// 'scene' is an autorelease object
auto scene = Scene::create();
// 'layer' is an autorelease object
auto 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()
{
Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 orign = Director::getInstance()->getVisibleOrigin();
//////////////////////////////
// 1. super init first
if ( !LayerColor::initWithColor(Color4B(255,255,255,255)))
{
return false;
}
ball = Sprite::create("ball.jpg");
ball->setPosition(200,400);
addChild(ball);
scheduleUpdate();
return true;
}
void HelloWorld::update(float dt)
{
Size visibleSize = Director::getInstance()->getVisibleSize();
ball->setPositionX (ball->getPositionX() + 3);
if(ball->getPositionX() > visibleSize.width + ball->getContentSize().width / 2 )
{
//ball->setPositionX(-ball->getContentSize().width / 2);
ball->setPositionX(getPositionX()+3);
}
}
|