LoginSignup
58
63

More than 5 years have passed since last update.

Cocos2d-x 3.0でスプライトの生成、移動、削除、イベント

Last updated at Posted at 2014-04-15

現状の最新版、cocos2d-x 3.0のrc1の基本操作と入門用サンプルです。
*新しくなったらまた編集します。

画面

画面の幅、原点取得

// 画面の幅を取得
Size visibleSize = Director::getInstance()->getVisibleSize();

// 原点を取得
Point origin = Director::getInstance()->getVisibleOrigin();

スプライト(画像)

スプライトの生成、追加、削除、変形

// スプライトの作成
Sprite* sprite = Sprite::create("sprite.png");

// スプライトの位置を指定(この場合は画面の中央)
sprite->setPosition(Point(visibleSize.width / 2, visibleSize.height / 2));

// 拡大、縮小
sprite->setScale(0.5f); // x, y
sprite->setScaleX(0.5f); // x
sprite->setScaleY(0.5f); // y

アクション(移動)

アクションの作成、実行

// アクションの作成
// 第一引数 アクションに要する時間
// 第二引数 目的の座標
MoveTo* move = MoveTo::create(10.0f, Point(100, 100));

// アクションの実行
sprite->runAction(move);

イベント

イベントハンドリング
cppファイル(例:HelloWorld.cpp)

// イベントリスナー作成
auto listener = EventListenerTouchOneByOne::create();

// イベントに対するメソッドの設定
listener->onTouchBegan = CC_CALLBACK_2(HelloWorld::onTouchBegan, this);
listener->onTouchMoved = CC_CALLBACK_2(HelloWorld::onTouchMoved, this);
listener->onTouchEnded = CC_CALLBACK_2(HelloWorld::onTouchEnded, this);
listener->onTouchCancelled = CC_CALLBACK_2(HelloWorld::onTouchCancelled, this);

// ディスパッチャーに登録
auto dispatcher = Director::getInstance()->getEventDispatcher();
dispatcher->addEventListenerWithSceneGraphPriority(listener, this);

hファイル(例:HelloWorld.h)

bool onTouchBegan(cocos2d::Touch* touch, cocos2d::Event* event);
void onTouchMoved(cocos2d::Touch* touch, cocos2d::Event* event);
void onTouchEnded(cocos2d::Touch* touch, cocos2d::Event* event);
void onTouchCancelled(cocos2d::Touch* touch, cocos2d::Event* event);

コールバック(CallFunc)

コールバックの実行

// callbackの定義
CallFunc* callback = CallFunc::create([](){
  printf("callback");
});

// callbackの実行
callback->execute(); // callback

サンプル

画像をタップして動かし、離すと中央に戻りspriteが消去します。
デフォルトのプロジェクト「HelloWorld」のままです。
*あくまで簡単に見せるサンプルなので、複数回タップには対応していません。

サンプル動画

hファイル(例:HelloWorld.h)

bool onTouchBegan(cocos2d::Touch* touch, cocos2d::Event* event);
void onTouchMoved(cocos2d::Touch* touch, cocos2d::Event* event);
void onTouchEnded(cocos2d::Touch* touch, cocos2d::Event* event);

cppファイル(例:HelloWorld.cpp)


bool HelloWorld::init()
{
    // 画面幅取得
    auto visibleSize = Director::getInstance()->getVisibleSize();

    // sprite
    auto sprite = Sprite::create("sprite.png");
    sprite->setPosition(Point(visibleSize.width / 2, visibleSize.height / 2));
    sprite->setTag(1);

    // イベント
    auto listener = EventListenerTouchOneByOne::create();
    listener->onTouchBegan = CC_CALLBACK_2(HelloWorld::onTouchBegan, this);
    listener->onTouchMoved = CC_CALLBACK_2(HelloWorld::onTouchMoved, this);
    listener->onTouchEnded = CC_CALLBACK_2(HelloWorld::onTouchEnded, this);

    auto dispatcher = Director::getInstance()->getEventDispatcher();
    dispatcher->addEventListenerWithSceneGraphPriority(listener, this);

    this->addChild(sprite);

    return true;
}

// タップした箇所に移動
bool HelloWorld::onTouchBegan(cocos2d::Touch* touch, cocos2d::Event* event)
{
    auto location = touch->getLocation();
    auto sprite = this->getChildByTag(1);

    sprite->setPosition(location);

    return true;
}

// タップし続けている間、その箇所に移動
void HelloWorld::onTouchMoved(cocos2d::Touch* touch, cocos2d::Event* event)
{
    auto location = touch->getLocation();
    auto sprite = this->getChildByTag(1);

    sprite->setPosition(location);
}

// タップを離したときに、スプライトを中央に移動し消去
void HelloWorld::onTouchEnded(cocos2d::Touch *touch, cocos2d::Event *event)
{
    // 目的地へのアクションの生成
    auto visibleSize = Director::getInstance()->getVisibleSize();
    auto point = Point(visibleSize.width/2, visibleSize.height/2);
    auto move = MoveTo::create(2.0f, point);

    // spriteの取得
    auto sprite = this->getChildByTag(1);

    //callbackでの消去処理
    auto removeSprite = CallFunc::create([this, sprite](){
        this->removeChild(sprite);
    });

    // 順次実行 move ➡ removeSprite
    auto sequence = Sequence::create(move, removeSprite, NULL);
    sprite->runAction(sequence);
}

外部アカウント

技術情報のみつぶやくアカウント作成しました。Cocos2d-x情報も追っていきます。
Twitterはこちら
Feedlyのフォローはこちら

58
63
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
58
63