搜索
查看: 576|回复: 0
打印 上一主题 下一主题

【cocos2d-x】一个运动的小球

[复制链接]
跳转到指定楼层
楼主
发表于 2016-5-24 14:36:08 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
这个帖子是笔记,有想用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);
    }
}





您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

广播台
特别关注
快速回复 返回顶部 返回列表