Exemplo n.º 1
0
Box2DTestLayer::Box2DTestLayer()
: m_pSpriteTexture(NULL)
{
    setTouchEnabled( true );
    setAccelerometerEnabled( true );

    // init physics
    this->initPhysics();
    // create reset button
    this->createResetButton();

    //Set up sprite
#if 1
    // Use batch node. Faster
    CCSpriteBatchNode *parent = CCSpriteBatchNode::create("Images/blocks.png", 100);
    m_pSpriteTexture = parent->getTexture();
#else
    // doesn't use batch node. Slower
    m_pSpriteTexture = CCTextureCache::sharedTextureCache()->addImage("Images/blocks.png");
    CCNode *parent = CCNode::create();
#endif
    addChild(parent, 0, kTagParentNode);


    addNewSpriteAtPosition(VisibleRect::center());

    CCLabelTTF *label = CCLabelTTF::create("Tap screen", "Marker Felt", 32);
    addChild(label, 0);
    label->setColor(ccc3(0,0,255));
    label->setPosition(ccp( VisibleRect::center().x, VisibleRect::top().y-50));
    
    scheduleUpdate();
}
Exemplo n.º 2
0
ChipmunkTestLayer::ChipmunkTestLayer()
{
#if CC_ENABLE_CHIPMUNK_INTEGRATION      
    // enable events

    auto touchListener = EventListenerTouchAllAtOnce::create();
    touchListener->onTouchesEnded = CC_CALLBACK_2(ChipmunkTestLayer::onTouchesEnded, this);
    _eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, this);
    
    Device::setAccelerometerEnabled(true);
    auto accListener = EventListenerAcceleration::create(CC_CALLBACK_2(ChipmunkTestLayer::onAcceleration, this));
    _eventDispatcher->addEventListenerWithSceneGraphPriority(accListener, this);
    
    // title
    auto label = Label::createWithTTF("Multi touch the screen", "fonts/Marker Felt.ttf", 36.0f);
    label->setPosition(cocos2d::Point( VisibleRect::center().x, VisibleRect::top().y - 30));
    this->addChild(label, -1);

    // reset button
    createResetButton();

    // init physics
    initPhysics();

#if 1
    // Use batch node. Faster
    auto parent = SpriteBatchNode::create("Images/grossini_dance_atlas.png", 100);
    _spriteTexture = parent->getTexture();
#else
    // doesn't use batch node. Slower
    _spriteTexture = Director::getInstance()->getTextureCache()->addImage("Images/grossini_dance_atlas.png");
    auto parent = Node::create();
#endif
    addChild(parent, 0, kTagParentNode);

    addNewSpriteAtPosition(cocos2d::Point(200,200));

    // menu for debug layer
    MenuItemFont::setFontSize(18);
    auto item = MenuItemFont::create("Toggle debug", CC_CALLBACK_1(ChipmunkTestLayer::toggleDebugCallback, this));

    auto menu = Menu::create(item, NULL);
    this->addChild(menu);
    menu->setPosition(cocos2d::Point(VisibleRect::right().x-100, VisibleRect::top().y-60));

    scheduleUpdate();
#else
    auto label = Label::createWithTTF("Should define CC_ENABLE_CHIPMUNK_INTEGRATION=1\n to run this test case",
                                            "fonts/arial.ttf",
                                            18);
    auto size = Director::getInstance()->getWinSize();
    label->setPosition(Point(size.width/2, size.height/2));
    
    addChild(label);
    
#endif
    
}
Exemplo n.º 3
0
void ChipmunkTestLayer::onTouchesEnded(const std::vector<Touch*>& touches, Event* event)
{
    //Add a new body/atlas sprite at the touched location

    for( auto &touch: touches)
    {
        auto location = touch->getLocation();

        addNewSpriteAtPosition( location );
    }
}
Exemplo n.º 4
0
ChipmunkTestLayer::ChipmunkTestLayer()
{
#if CC_ENABLE_CHIPMUNK_INTEGRATION      
    // enable events
    setTouchEnabled(true);
    setAccelerometerEnabled(true);

    // title
    auto label = LabelTTF::create("Multi touch the screen", "Marker Felt", 36);
    label->setPosition(Point( VisibleRect::center().x, VisibleRect::top().y - 30));
    this->addChild(label, -1);

    // reset button
    createResetButton();

    // init physics
    initPhysics();

#if 1
    // Use batch node. Faster
    auto parent = SpriteBatchNode::create("Images/grossini_dance_atlas.png", 100);
    _spriteTexture = parent->getTexture();
#else
    // doesn't use batch node. Slower
    _spriteTexture = TextureCache::getInstance()->addImage("Images/grossini_dance_atlas.png");
    auto parent = Node::create();
#endif
    addChild(parent, 0, kTagParentNode);

    addNewSpriteAtPosition(Point(200,200));

    // menu for debug layer
    MenuItemFont::setFontSize(18);
    auto item = MenuItemFont::create("Toggle debug", CC_CALLBACK_1(ChipmunkTestLayer::toggleDebugCallback, this));

    auto menu = Menu::create(item, NULL);
    this->addChild(menu);
    menu->setPosition(Point(VisibleRect::right().x-100, VisibleRect::top().y-60));

    scheduleUpdate();
#else
    auto label = LabelTTF::create("Should define CC_ENABLE_CHIPMUNK_INTEGRATION=1\n to run this test case",
                                            "Arial",
                                            18);
    auto size = Director::getInstance()->getWinSize();
    label->setPosition(Point(size.width/2, size.height/2));
    
    addChild(label);
    
#endif
    
}
Exemplo n.º 5
0
void Box2DTest::onTouchesEnded(const std::vector<Touch*>& touches, Event* event)
{
    //Add a new body/atlas sprite at the touched location

    for (auto& touch : touches)
    {
        if(!touch)
            break;

        auto location = touch->getLocation();
    
        addNewSpriteAtPosition( location );
    }
}
Exemplo n.º 6
0
Box2DTestLayer::Box2DTestLayer()
: _spriteTexture(nullptr)
, world(nullptr)
{
#if CC_ENABLE_BOX2D_INTEGRATION
    auto dispatcher = Director::getInstance()->getEventDispatcher();
    
    auto touchListener = EventListenerTouchAllAtOnce::create();
    touchListener->onTouchesEnded = CC_CALLBACK_2(Box2DTestLayer::onTouchesEnded, this);
    dispatcher->addEventListenerWithSceneGraphPriority(touchListener, this);
    
    // init physics
    this->initPhysics();
    // create reset button
    this->createResetButton();

    //Set up sprite
#if 1
    // Use batch node. Faster
    auto parent = SpriteBatchNode::create("Images/blocks.png", 100);
    _spriteTexture = parent->getTexture();
#else
    // doesn't use batch node. Slower
    _spriteTexture = Director::getInstance()->getTextureCache()->addImage("Images/blocks.png");
    auto parent = Node::create();
#endif
    addChild(parent, 0, kTagParentNode);


    addNewSpriteAtPosition(VisibleRect::center());

    auto label = Label::createWithTTF("Tap screen", "fonts/Marker Felt.ttf", 32.0f);
    addChild(label, 0);
    label->setColor(Color3B(0,0,255));
    label->setPosition(Vec2( VisibleRect::center().x, VisibleRect::top().y-50));
    
    scheduleUpdate();
#else
    auto label = Label::createWithTTF("Should define CC_ENABLE_BOX2D_INTEGRATION=1\n to run this test case",
                                            "fonts/arial.ttf",
                                            18);
    auto size = Director::getInstance()->getWinSize();
    label->setPosition(Vec2(size.width/2, size.height/2));
    
    addChild(label);
#endif
}
Exemplo n.º 7
0
void ChipmunkTestLayer::ccTouchesEnded(CCSet* touches, CCEvent* event)
{
    //Add a new body/atlas sprite at the touched location
    CCSetIterator it;
    CCTouch* touch;

    for( it = touches->begin(); it != touches->end(); it++) 
    {
        touch = (CCTouch*)(*it);

        if(!touch)
            break;

        CCPoint location = touch->getLocation();

        addNewSpriteAtPosition( location );
    }
}
Exemplo n.º 8
0
void Box2DTestLayer::ccTouchesEnded(Set* touches, Event* event)
{
    //Add a new body/atlas sprite at the touched location
    SetIterator it;
    Touch* touch;

    for( it = touches->begin(); it != touches->end(); it++) 
    {
        touch = static_cast<Touch*>(*it);

        if(!touch)
            break;

        Point location = touch->getLocation();
    
        addNewSpriteAtPosition( location );
    }
}
Exemplo n.º 9
0
PhysicsTestLayer::PhysicsTestLayer()
: _spriteTexture(nullptr)
, _scene(nullptr)
{
#ifdef CC_USE_PHYSICS
    setTouchEnabled(true);
    setAccelerometerEnabled(true);
    
    // title
    auto label = LabelTTF::create("Multi touch the screen", "Marker Felt", 36);
    label->setPosition(Point( VisibleRect::center().x, VisibleRect::top().y - 30));
    this->addChild(label, -1);
    
    // menu for debug layer
    MenuItemFont::setFontSize(18);
    auto item = MenuItemFont::create("Toggle debug", CC_CALLBACK_1(PhysicsTestLayer::toggleDebugCallback, this));
    
    auto menu = Menu::create(item, NULL);
    this->addChild(menu);
    menu->setPosition(Point(VisibleRect::right().x-100, VisibleRect::top().y-60));
    
    auto sp = Sprite::create();
    auto body = PhysicsBody::createEdgeBox(VisibleRect::getVisibleRect().size);
    sp->setPhysicsBody(body);
    this->addChild(sp);
    sp->setPosition(VisibleRect::center());
    
    auto parent = SpriteBatchNode::create("Images/grossini_dance_atlas.png", 100);
    _spriteTexture = parent->getTexture();
    
    addNewSpriteAtPosition(VisibleRect::center());
    
    createResetButton();
    
#else
    auto label = LabelTTF::create("Should define CC_USE_BOX2D or CC_USE_CHIPMUNK\n to run this test case",
                                  "Arial",
                                  18);
    auto size = Director::getInstance()->getWinSize();
    label->setPosition(Point(size.width/2, size.height/2));
    
    addChild(label);
#endif
}
Exemplo n.º 10
0
Box2DTestLayer::Box2DTestLayer()
: _spriteTexture(NULL)
, world(NULL)
{
#if CC_ENABLE_BOX2D_INTEGRATION
    setTouchEnabled( true );
    setAccelerometerEnabled( true );

    // init physics
    this->initPhysics();
    // create reset button
    this->createResetButton();

    //Set up sprite
#if 1
    // Use batch node. Faster
    SpriteBatchNode *parent = SpriteBatchNode::create("Images/blocks.png", 100);
    _spriteTexture = parent->getTexture();
#else
    // doesn't use batch node. Slower
    _spriteTexture = TextureCache::getInstance()->addImage("Images/blocks.png");
    Node *parent = Node::create();
#endif
    addChild(parent, 0, kTagParentNode);


    addNewSpriteAtPosition(VisibleRect::center());

    LabelTTF *label = LabelTTF::create("Tap screen", "Marker Felt", 32);
    addChild(label, 0);
    label->setColor(Color3B(0,0,255));
    label->setPosition(Point( VisibleRect::center().x, VisibleRect::top().y-50));
    
    scheduleUpdate();
#else
    LabelTTF *label = LabelTTF::create("Should define CC_ENABLE_BOX2D_INTEGRATION=1\n to run this test case",
                                            "Arial",
                                            18);
    Size size = Director::getInstance()->getWinSize();
    label->setPosition(Point(size.width/2, size.height/2));
    
    addChild(label);
#endif
}
Exemplo n.º 11
0
void Box2DTestLayer::ccTouchesEnded(CCSet* touches, CCEvent* event)
{
    //Add a new body/atlas sprite at the touched location
    CCSetIterator it;
    CCTouch* touch;

    for( it = touches->begin(); it != touches->end(); it++) 
    {
        touch = (CCTouch*)(*it);

        if(!touch)
            break;

        CCPoint location = touch->locationInView();
        
        location = CCDirector::sharedDirector()->convertToGL(location);
    
        addNewSpriteAtPosition( location );
    }
}
Exemplo n.º 12
0
bool HelloWorld::init()
{
    if ( !Layer::init() )
    {
        return false;
    }
    
    Size visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();
    EventListenerTouchOneByOne *one=EventListenerTouchOneByOne::create();
    one->onTouchBegan=[this](Touch *touch, Event *unused_event){
        log("onTouchBegan");
        return true;
    };
    one->onTouchEnded=[this](Touch *touch, Event *unused_event){
        log("onTouchEnded");
        addNewSpriteAtPosition(touch->getLocation());
    
    };
    auto dispather=Director::getInstance()->getEventDispatcher();
    dispather->addEventListenerWithSceneGraphPriority(one, this);
    
    //小恒在成此修改了下
    auto contactListener=EventListenerPhysicsContact::create();
    contactListener->onContactBegin=[this](const PhysicsContact &contact){
        log("contact");
        return true;
    };
    //分开时调用的 设置物理世界中各种物体碰撞的监听对象
    contactListener->onContactSeperate=[this](PhysicsContact &contact){
        auto node1=contact.getShapeA()->getBody()->getNode();
        auto node2=contact.getShapeA()->getBody()->getNode();
        log("seprated");
    };
    _eventDispatcher->addEventListenerWithSceneGraphPriority(contactListener, this);
 
    return true;
}
Exemplo n.º 13
0
Box2DTestLayer::Box2DTestLayer()
: _spriteTexture(NULL)
, world(NULL)
{

    auto touchListener = EventListenerTouchAllAtOnce::create();
    touchListener->onTouchesEnded = CC_CALLBACK_2(Box2DTestLayer::onTouchesEnded, this);
    m_pEventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, this);

    // init physics
    this->initPhysics();
    // create reset button
    this->createResetButton();

    //Set up sprite
#if 1
    // Use batch node. Faster
    auto parent = SpriteBatchNode::create("Images/blocks.png", 100);
    _spriteTexture = parent->getTexture();
#else
    // doesn't use batch node. Slower
    _spriteTexture = Director::getInstance()->getTextureCache()->addImage("Images/blocks.png");
    auto parent = Node::create();
#endif
    addChild(parent, 0, kTagParentNode);


    addNewSpriteAtPosition(VisibleRect::center());

    auto label = LabelTTF::create("Tap screen", "Marker Felt", 32);
    addChild(label, 0);
    label->setColor(Color3B(0,0,255));
    label->setPosition(Point( VisibleRect::center().x, VisibleRect::top().y-50));
    
    scheduleUpdate();
}
bool PhysicalsScene::onTouchBegan(cocos2d::Touch *touch, cocos2d::Event *unused_event)
{
    auto location = touch->getLocation();
    addNewSpriteAtPosition(location);
    return true;
}
void HelloWorld::onTouchEnded(Touch *touch, Event *unused_event) {
	log("onTouchEnded");
    Vec2 location = touch->getLocation();
    addNewSpriteAtPosition(location);
}