天天看點

Sprite3D

//建立第一個3D對象

Sprite3D * sp = Sprite3D::create("boss.obj","boss.png");

    sp->setScale(17.f);

    sp->setPosition(visibleSize/2);

    sp->runAction(RepeatForever::create(RotateBy::create(0.1, 10)));

    addChild(sp);

//建立第二個對象

    Sprite3D * sp_1 = Sprite3D::create("boss.obj","boss.png");

    sp_1->setScale(17.f);

    sp_1->setPosition(visibleSize/2);

    sp_1->setAnchorPoint(Vec2(0.5,0.5));

    sp_1->runAction(RepeatForever::create(RotateBy::create(0.1, 10)));

    addChild(sp_1);

//建立觸摸監聽    

    auto lis = EventListenerTouchOneByOne::create();

    lis->setSwallowTouches(true);

//用lambda表達式對回調函數的實作

    lis->onTouchBegan=[](Touch * touch,Event * e){

        auto target = (Sprite3D*)(e->getCurrentTarget());

        Rect rect = target->getBoundingBox();

        if (rect.containsPoint(touch->getLocation())) {

            CCLOG("sprite3d began... x = %f, y = %f", touch->getLocation().x, touch->getLocation().y);

            target->setOpacity(100);

            return true;

        }

        return false;

    };

    lis->onTouchMoved=[](Touch * touch,Event * e){

        auto target = (Sprite3D*)(e->getCurrentTarget());

        target->setPosition(target->getPosition()+touch->getDelta());

    };

    lis->onTouchEnded=[](Touch * touch,Event * e){

        auto target = (Sprite3D*)(e->getCurrentTarget());

        target->setOpacity(255);

    };

    //事件分發器 分别分發給兩個不同的對象

    _eventDispatcher->addEventListenerWithSceneGraphPriority(lis, sp);

    _eventDispatcher->addEventListenerWithSceneGraphPriority(lis->clone(), sp_1);